Content-Length: 3153108 | pFad | https://www.scribd.com/document/791362722/LAB-2-OS
6Lab 2 Os
Lab 2 Os
Lab 2 Os
Objective: Write a C++ program to simulate the following CPU scheduling algorithms to find turnaround time
and waiting time for the above problem. a) FCFS b) SJF c) Round Robin d) Priority
a) FCFS CPU SCHEDULING ALGORITHM
CODE:
#include<iostream>
using namespace std;
void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])
{
for (int i = 0; i < n; i++)
tat[i] = bt[i] + wt[i]; }
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
int processes[n];
int burst_time[n];
cout << "Enter the process No and burst times:" << endl;
for (int i = 0; i < n; i++) {
cout << "Process " << (i + 1) << "ID : ";
cin >> processes[i];
cout << "Burst time for Process " << processes[i] << ": ";
cin >> burst_time[i];
}
findavgTime(processes, n, burst_time);
return 0; }
OUTPUT:
arrangeArrival(num, mat);
completionTime(num, mat);
cout << "Final Result...\n";
cout << "Process ID\tArrival Time\tBurst Time\tWaiting "
"Time\tTurnaround Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"
<< mat[i][5] << "\n";
}
}
OUTPUT:
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
if (check == false) {
t++;
continue; }
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++; // Increment time
}
}
findWaitingTime(proc, n, wt);
Process proc[n];
// Call the function to calculate average waiting time and turnaround time
findavgTime(proc, n);
return 0;
}
OUTPUT:
d) PRIORITY CPU SCHEDULING ALGORITHM
CODE:
#include<iostream>;
using namespace std;
// Function to find the waiting time for all
// processes
void findWaitingTime(int processes[], int n,
int bt[], int wt[], int quantum)
{
// Make a copy of burst times bt[] to store remaining
// burst times.
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
// Keep traversing processes in round robin manner
// until all of them are not done.
while (1)
{
bool done = true;
// Traverse all processes one by one repeatedly
for (int i = 0 ; i < n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] > 0)
{
done = false; // There is a pending process
if (rem_bt[i] > quantum)
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t += quantum;
// Decrease the burst_time of current process
// by quantum
rem_bt[i] -= quantum;
}
// If burst time is smaller than or equal to
// quantum. Last cycle for this process
else
{
// Increase the value of t i.e. shows
// how much time a process has been processed
t = t + rem_bt[i];
// Waiting time is current time minus time
// used by this process
wt[i] = t - bt[i];
// As the process gets fully executed
// make its remaining burst time = 0
rem_bt[i] = 0;
}
}
}
// If all processes are done
if (done == true)
break;
}
}
// Function to calculate turn around time
void findTurnAroundTime(int processes[], int n,
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n ; i++)
tat[i] = bt[i] + wt[i];
}
// Function to calculate average time
void findavgTime(int processes[], int n, int bt[],
int quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt, quantum);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
// Display processes along with all details
cout << "Processes "<< " Burst time "
<< " Waiting time " << " Turn around time\n";
// Calculate total waiting time and total turn
// around time
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i]
<<"\t "
<< wt[i] <<"\t\t " << tat[i] <<endl;
}
cout << "Average waiting time = "
<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}
// Driver code
int main() {
int n;
int burst_time[n];
int quantum;
return 0;
}
OUTPUT:
NED University of Engineering and
Technology
Department of Computer Science & IT
Bachelors in ___CS&IT_______________
1 2 3 4 5
Remarks
Objective: Write a C++ program to illustrate simultaneous execution of two or more threads
Q1: Implement the above code and paste the screen shot of the output
Code:
#include <iostream>
#include <thread>
int main() {
std::thread thread1(print_message_function, "Thread 1");
std::thread thread2(print_message_function, "Thread 2");
thread1.join();
thread2.join();
return 0;
}
Output:
Q2: Describe the following line of code: iret1 = pthread_create( &thread1, NULL,
print_message_function, (void*) message1);
The given line of code is written in C and involves the use of the POSIX thread library for creating a new
thread. Let's break down the components of the code:
1. pthread_create:
pthread_create is a function from the POSIX threads library, used for creating a new
thread.
The second parameter (NULL) is used for specifying thread attributes. In this
case, the default attributes are used.
The fourth parameter ((void*) message1) is a pointer to the data that will be
passed as an argument to the thread function. It's cast to void* because
pthread_create expects a void* type.
2. iret1:
The return value of pthread_create is an integer that indicates whether the thread creation was
successful. A value of 0 typically indicates success, and other values indicate an error.
NED University of Engineering and
Technology
Department of Computer Science & IT
Bachelors in ___CS&IT_______________
1 2 3 4 5
Remarks
Lab#04
Objective: Write a C++ program to simulate producer-consumer problem using semaphores.
Program:
#include <iostream>
using namespace std;
int main() {
int buffer[10], bufsize, in, out, produce, consume, choice = 0;
in = 0;
out = 0;
bufsize = 10;
while (choice != 3) {
cout << "\n1. Produce \t 2. Consume \t3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
if ((in + 1) % bufsize == out)
cout << "\nBuffer is Full" << endl;
else {
cout << "Enter the value: ";
cin >> produce;
buffer[in] = produce;
in = (in + 1) % bufsize;
}
break;
case 2:
if (in == out)
cout << "\nBuffer is Empty" << endl;
else {
consume = buffer[out];
cout << "The consumed value is " << consume <<
endl;
out = (out + 1) % bufsize;
}
break;
}
}
return 0;
}
Q1: Implement the above code and paste the screen shot of the output.
Output:
Q2: Solve the producer-consumer problem using linked list. (You can perform this task using any
programming language) Note: Keep the buffer size to 10 places.
Code:
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <thread>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedListBuffer {
private:
Node* head;
Node* tail;
int count;
mutex mtx;
condition_variable cv_produce, cv_consume;
public:
LinkedListBuffer() : head(nullptr), tail(nullptr), count(0) {}
if (head == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
count++;
int consume() {
unique_lock<mutex> lock(mtx);
int consumedValue;
if (head != nullptr) {
Node* temp = head;
head = head->next;
consumedValue = temp->data;
delete temp;
count--;
return consumedValue;
}
};
int main() {
LinkedListBuffer buffer;
producer1.join();
producer2.join();
consumer1.join();
consumer2.join();
return 0;
}
Output:
Q3: In producer-consumer problem what difference will it make if we utilize stack for the buffer rather
than an array?
Using a stack as the buffer in the producer-consumer problem instead of an array or linked list can have
some implications:
1. LIFO Behavior:
A stack follows the Last In, First Out (LIFO) principle. The most recently produced item is the
first one to be consumed.
This behavior might be suitable for certain applications where the order of consumption is
critical.
2. Simplicity:
Implementing a stack is often simpler than managing an array or linked list.
You only need to keep track of the top of the stack and push/pop elements.
3. Limited Access:
A stack typically allows access only to the top element.
If consumers need to access elements in the middle of the buffer, a stack might not be the best
choice.
4. No Random Access:
Unlike an array, where you can access elements randomly, a stack provides access only to the top
element.
This limitation might affect certain scenarios where random access is required.
5. Space Efficiency:
A stack might be more space-efficient compared to a linked list since you only need to store the
data and a reference to the next element.
However, the fixed size of the stack may lead to inefficiencies if the buffer needs dynamic
resizing.
6. Performance:
The stack operations (push and pop) are generally faster than dynamic memory allocation (as in
linked lists) or array resizing.
This can lead to better performance in scenarios where high throughput is crucial.
NED University of Engineering and
Technology
Department of Computer Science & IT
Bachelors in ___CS&IT_______________
1 2 3 4 5
Remarks
Lab#05
Objective: Write a C++ program to simulate readers writers problem using semaphores.
Prgram:
#include<stdio.h>
#define n 4
int compltedPhilo = 0,i;
struct fork{
int taken;
}ForkAvil[n];
struct philosp{
int left;
int right;
}
Philostatus[n];
void goForDinner(int philID){
if(Philostatus[philID].left==10 &&
Philostatus[philID].right==10)
printf("Philosopher %d completed his dinner\n",philID+1);
Philostatus[philID].left =
Philostatus[philID].right = 10;
int otherFork = philID-1;
if(otherFork== -1)
otherFork=(n-1);
ForkAvil[philID].taken =
ForkAvil[otherFork].taken = 0;
printf("Philosopher %d released fork %d and fork %d\
n",philID+1,philID+1,otherFork+1);
compltedPhilo++;
}
else if(Philostatus[philID].left==1 &&
Philostatus[philID].right==0){
if(philID==(n-1)){
if(ForkAvil[philID].taken==0){
ForkAvil[philID].taken =
Philostatus[philID].right = 1;
printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
}
}else{
int dupphilID = philID;
philID-=1;
if(philID== -1)
philID=(n-1);
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken =
Philostatus[dupphilID].right = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
}else{
printf("Philosopher %d is waiting for Fork %d\
n",dupphilID+1,philID+1);
}
}
}
else if(Philostatus[philID].left==0){
if(philID==(n-1)){
if(ForkAvil[philID-1].taken==0){
ForkAvil[philID-1].taken =
Philostatus[philID].left = 1;
printf("Fork %d taken by philosopher %d\n",philID,philID+1);
}else{
printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
}
}else{
if(ForkAvil[philID].taken == 0){
ForkAvil[philID].taken =
Philostatus[philID].left = 1;
printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
}else{
}
}
}else{}
}
int main(){
for(i=0;i<n;i++)
ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right
=0;
while(compltedPhilo<n){
for(i=0;i<n;i++)
goForDinner(i);
printf("\nTill now num of philosophers completed dinner are %d\n\
n",compltedPhilo);
}
return 0;
}
Q1: Implement the above code and paste the screen shot of the output
NED University of Engineering and
Technology
Department of Computer Science & IT
Bachelors in ___CS&IT_______________
1 2 3 4 5
Remarks
Lab#06
PROCESS SYNCHRONIZATION
Objective: Write a C program to simulate the concept of Dining Philosopher’s Problem.
Exercise:
1) Implement the above code and paste the screen shot of the output.
CODE:
#include <iostream>
#include <vector>
class Fork {
public:
int taken;
Fork() : taken(0) {}
};
class Philosopher {
public:
int left;
int right;
Philosopher() : left(0), right(0) {}
};
int n = 4;
int completed_philo = 0;
std::vector<Fork> forks(n);
std::vector<Philosopher> philosophers(n);
forks[philID].taken = forks[otherFork].taken = 0;
std::cout << "Philosopher " << philID + 1 << " released fork "
<< philID + 1
<< " and fork " << otherFork + 1 << "\n";
completed_philo++;
}
else if (philosophers[philID].left == 1 &&
philosophers[philID].right == 0) {
if (philID == n - 1) {
if (forks[philID].taken == 0) {
forks[philID].taken = philosophers[philID].right = 1;
std::cout << "Fork " << philID + 1 << " taken by
philosopher " << philID + 1 << "\n";
} else {
std::cout << "Philosopher " << philID + 1 << " is
waiting for fork " << philID + 1 << "\n";
}
} else {
int dup_philID = philID;
philID--;
if (philID == -1) philID = n - 1;
if (forks[philID].taken == 0) {
forks[philID].taken = philosophers[dup_philID].right =
1;
std::cout << "Fork " << philID + 1 << " taken by
philosopher " << dup_philID + 1 << "\n";
} else {
std::cout << "Philosopher " << dup_philID + 1 << " is
waiting for Fork " << philID + 1 << "\n";
}
}
}
else if (philosophers[philID].left == 0) {
if (philID == n - 1) {
if (forks[philID - 1].taken == 0) {
forks[philID - 1].taken = philosophers[philID].left =
1;
std::cout << "Fork " << philID << " taken by
philosopher " << philID + 1 << "\n";
} else {
std::cout << "Philosopher " << philID + 1 << " is
waiting for fork " << philID << "\n";
}
} else {
if (forks[philID].taken == 0) {
forks[philID].taken = philosophers[philID].left = 1;
std::cout << "Fork " << philID + 1 << " taken by
philosopher " << philID + 1 << "\n";
} else {
std::cout << "Philosopher " << philID + 1 << " is
waiting for fork " << philID + 1 << "\n";
}
}
}
}
int main() {
while (completed_philo < n) {
for (int i = 0; i < n; i++) {
go_for_dinner(i);
}
std::cout << "\nTill now number of philosophers who completed
dinner: " << completed_philo << "\n\n";
}
return 0;
}
OUTPUT:
Bachelors in ___CS&IT_______________
1 2 3 4 5
Remarks
Fetched URL: https://www.scribd.com/document/791362722/LAB-2-OS
Alternative Proxies: