Content-Length: 3153108 | pFad | https://www.scribd.com/document/791362722/LAB-2-OS

6 Lab 2 Os | PDF | Namespace | Software Engineering
0% found this document useful (0 votes)
17 views27 pages

Lab 2 Os

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 27

LAB#2

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 findWaitingTime(int processes[], int n, int bt[], int wt[]) {


wt[0] = 0;
for (int i = 1; i < n; i++)
wt[i] = bt[i-1] + wt[i-1]; }

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]; }

void findavgTime(int processes[], int n, int bt[]) {


int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);


findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes \t"<< " Burst time \t"
<< " Waiting time \t" << " Turnaround time\n";

for (int i=0; i<n; i++) {


total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << "\t"<< 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 turnaround time = "<< (float)total_tat / (float)n;
}

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:

b) SJF CPU SCHEDULING ALGORITHM


CODE:
#include <iostream>
using namespace std;
int mat[10][6];
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void arrangeArrival(int num, int mat[][6]) {
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - i - 1; j++) {
if (mat[j][1] > mat[j + 1][1]) {
for (int k = 0; k < 5; k++) {
swap(mat[j][k], mat[j + 1][k]);
}
}
}
}
}
void completionTime(int num, int mat[][6]) {
int temp, val;
mat[0][3] = mat[0][1] + mat[0][2];
mat[0][5] = mat[0][3] - mat[0][1];
mat[0][4] = mat[0][5] - mat[0][2];

for (int i = 1; i < num; i++) {


temp = mat[i - 1][3];
int low = mat[i][2];
for (int j = i; j < num; j++) {
if (temp >= mat[j][1] && low >= mat[j][2]) {
low = mat[j][2];
val = j;
}
}
mat[val][3] = temp + mat[val][2];
mat[val][5] = mat[val][3] - mat[val][1];
mat[val][4] = mat[val][5] - mat[val][2];
for (int k = 0; k < 6; k++) {
swap(mat[val][k], mat[i][k]);
}
}
}
int main() {
int num, temp;

cout << "Enter number of Process: ";


cin >> num;

cout << "...Enter the process ID...\n";


for (int i = 0; i < num; i++) {
cout << "...Process " << i + 1 << "...\n";
cout << "Enter Process Id: ";
cin >> mat[i][0];
cout << "Enter Arrival Time: ";
cin >> mat[i][1];
cout << "Enter Burst Time: ";
cin >> mat[i][2];
}

cout << "Before Arrange...\n";


cout << "Process ID\tArrival Time\tBurst Time\n";
for (int i = 0; i < num; i++) {
cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"
<< mat[i][2] << "\n";
}

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:

c) ROUND ROBIN CPU SCHEDULING ALGORITHM


CODE:
#include <bits/stdc++.h>;
using namespace std;

struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};

// Function to find the waiting time for all processes


void findWaitingTime(Process proc[], int n, int wt[]) {
int rt[n];
// Copy the burst time into rt[]
for (int i = 0; i &lt; n; i++)
rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;


int shortest = 0, finish_time;
bool check = false;

// Process until all processes gets completed


while (complete != n) {
// Find process with minimum remaining time among the
// processes that arrives till the current time`
for (int j = 0; j &lt; n; j++) {

if ((proc[j].art &lt;= t) &amp;&amp;


(rt[j] &lt; minm) &amp;&amp; rt[j] &gt; 0) {
minm = rt[j];
shortest = j;
check = true;
}
}

if (check == false) {
t++;
continue; }

rt[shortest]--; // Reduce remaining time by one


minm = rt[shortest]; // Update minimum
if (minm == 0)
minm = INT_MAX;

// If a process gets completely executed


if (rt[shortest] == 0) {
complete++; // Increment complete
check = false;

// Find finish time of current process


finish_time = t + 1;

// Calculate waiting time


wt[shortest] = finish_time-proc[shortest].bt-
proc[shortest].art;

if (wt[shortest] &lt; 0)
wt[shortest] = 0;
}
t++; // Increment time
}
}

// Function to calculate turnaround time


void findTurnAroundTime(Process proc[], int n, int wt[], int
tat[]) {
// calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i &lt; n; i++)
tat[i] = proc[i].bt + wt[i];
}
// Function to calculate average time
void findavgTime(Process proc[], int n) {
int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes

findWaitingTime(proc, n, wt);

// Function to find turnaround time for all processes


findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all details


cout&lt;&lt;&quot;Processes &quot;&lt;&lt;&quot; Burst time &quot;&lt;&lt;&quot;
Waiting time &quot;&lt;&lt;&quot; Turnaround
time\n&quot;;

// Calculate total waiting time and total turnaround time


for (int i = 0; i &lt; n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout &lt;&lt; &quot; &quot; &lt;&lt; proc[i].pid &lt;&lt; &quot;\t\t&quot;
&lt;&lt; proc[i].bt &lt;&lt; &quot;\t\t &quot; &lt;&lt; wt[i]
&lt;&lt; &quot;\t\t &quot; &lt;&lt; tat[i] &lt;&lt; endl;
}
cout &lt;&lt; &quot;\nAverage waiting time = &quot;&lt;&lt; (float)total_wt /
(float)n;
cout &lt;&lt; &quot;\nAverage turn around time = &quot;&lt;&lt; (float)total_tat /
(float)n;
}

int main() { // Driver code


int n;

// Input the number of processes


cout << "Enter the number of processes: ";
cin >> n;

Process proc[n];

// Input details for each process


cout << "Enter process details (Process ID, Burst Time, Arrival Time):\n";
for (int i = 0; i < n; i++) {
cout << "Process " << i + 1 << ": ";
cin >> proc[i].pid >> proc[i].bt >> proc[i].art;
}

// 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 &lt; 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 &lt; n; i++)
{
// If burst time of a process is greater than 0
// then only need to process further
if (rem_bt[i] &gt; 0)
{
done = false; // There is a pending process
if (rem_bt[i] &gt; 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,

int bt[], int wt[], int tat[])

{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i &lt; 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 &lt;&lt; &quot;Processes &quot;&lt;&lt; &quot; Burst time &quot;
&lt;&lt; &quot; Waiting time &quot; &lt;&lt; &quot; Turn around time\n&quot;;
// Calculate total waiting time and total turn
// around time
for (int i=0; i&lt;n; i++)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout &lt;&lt; &quot; &quot; &lt;&lt; i+1 &lt;&lt; &quot;\t\t&quot; &lt;&lt; bt[i]
&lt;&lt;&quot;\t &quot;
&lt;&lt; wt[i] &lt;&lt;&quot;\t\t &quot; &lt;&lt; tat[i] &lt;&lt;endl;

}
cout &lt;&lt; &quot;Average waiting time = &quot;
&lt;&lt; (float)total_wt / (float)n;
cout &lt;&lt; &quot;\nAverage turn around time = &quot;
&lt;&lt; (float)total_tat / (float)n;

}
// Driver code
int main() {
int n;

cout << "Enter the number of processes: ";


cin >> n;
int processes[n];

cout << "Enter process IDs: ";


for (int i = 0; i < n; i++)
cin >> processes[i];

int burst_time[n];

cout << "Enter burst times for each process: ";


for (int i = 0; i < n; i++)
cin >> burst_time[i];

int quantum;

cout << "Enter the time quantum: ";


cin >> quantum;

findavgTime(processes, n, burst_time, quantum);

return 0;
}

OUTPUT:
NED University of Engineering and
Technology
Department of Computer Science & IT

Bachelors in ___CS&IT_______________

Course Code and Title: ____CT-353 Operating system________________________________

Laboratory Session No: ________ Date: ______________

Cognitive Domain Assessment Rubric – Level C3 (for Laboratory Work)

Criterion Level of Attainment

1 2 3 4 5

Problem Not able to Able to Able to Able to


Identification correctly Able to partially partially correctly correctly
identify the identify the identify the identify the identify the
problem(s) problem(s) but problem(s) problem(s) problem(s)
and all cannot identify and the but not all and all the
required data required data required the required data
(input and (input and data (input required data (input and
output). output). and output). (input and output).
output).
Problem Uses an Uses an Uses an Uses an Uses an
Solving Skills ineffective inadequate adequate effective effective
strategy to strategy to strategy to strategy to strategy to
solve the solve the solve the solve the solve the
problem(s). problem(s). problem(s). problem(s). problem(s).
Implementation Incomplete Inadequate Adequate Efficient Outstanding
implementation. implementation. implementation. implementation. implementation.

Deliverable(s) Cannot generate Generates Generates Generates Generates


the desired partially partially correct correct but correct and
output. correct and and partially complete
incomplete partially complete output.
output. complete output.
output.
Depth of Does not Limited grasp Fair grasp of the Good grasp Thorough
Knowledge grasp the of the concepts of the grasp of the
concepts concepts covered in the concepts concepts
covered in the covered in the lab and can covered in the covered in the
lab and cannot lab and cannot vaguely identify lab but cannot lab and can
identify any identify any a real-life effectively apply apply it in real
real-life real-life application. it in real life life situations.
application. application. situations.

Weighted CLO (Cognitive) Score

Remarks

Instructor’s Signature with Date


Lab#03

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>

void print_message_function(const char *message);

int main() {
std::thread thread1(print_message_function, "Thread 1");
std::thread thread2(print_message_function, "Thread 2");

thread1.join();
thread2.join();

return 0;
}

void print_message_function(const char *message) {


std::cout << message << std::endl;
}

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.

 It takes four parameters:

 The first parameter (&thread1) is a pointer to a pthread_t variable where the


thread ID will be stored.

 The second parameter (NULL) is used for specifying thread attributes. In this
case, the default attributes are used.

 The third parameter (print_message_function) is the function that will be


executed by the newly created thread.

 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:

 iret1 is likely a variable used to store the return value of pthread_create.

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_______________

Course Code and Title: ____CT-353 Operating system________________________________

Laboratory Session No: ________ Date: ______________

Cognitive Domain Assessment Rubric – Level C3 (for Laboratory Work)

Criterion Level of Attainment

1 2 3 4 5

Problem Not able to Able to Able to Able to


Identification correctly Able to partially partially correctly correctly
identify the identify the identify the identify the identify the
problem(s) problem(s) but problem(s) problem(s) problem(s)
and all cannot identify and the but not all and all the
required data required data required the required data
(input and (input and data (input required data (input and
output). output). and output). (input and output).
output).
Problem Uses an Uses an Uses an Uses an Uses an
Solving Skills ineffective inadequate adequate effective effective
strategy to strategy to strategy to strategy to strategy to
solve the solve the solve the solve the solve the
problem(s). problem(s). problem(s). problem(s). problem(s).
Implementation Incomplete Inadequate Adequate Efficient Outstanding
implementation. implementation. implementation. implementation. implementation.

Deliverable(s) Cannot generate Generates Generates Generates Generates


the desired partially partially correct correct but correct and
output. correct and and partially complete
incomplete partially complete output.
output. complete output.
output.
Depth of Does not Limited grasp Fair grasp of the Good grasp Thorough
Knowledge grasp the of the concepts of the grasp of the
concepts concepts covered in the concepts concepts
covered in the covered in the lab and can covered in the covered in the
lab and cannot lab and cannot vaguely identify lab but cannot lab and can
identify any identify any a real-life effectively apply apply it in real
real-life real-life application. it in real life life situations.
application. application. situations.

Weighted CLO (Cognitive) Score

Remarks

Instructor’s Signature with Date

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;

const int BUFFER_SIZE = 10;

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) {}

void produce(int value) {


unique_lock<mutex> lock(mtx);

// Wait if buffer is full


cv_produce.wait(lock, [this]() { return count <
BUFFER_SIZE; });

Node* newNode = new Node{value, nullptr};

if (head == nullptr) {
head = tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}

count++;

// Notify consumers that a new item is available


cv_consume.notify_all();
}

int consume() {
unique_lock<mutex> lock(mtx);

// Wait if buffer is empty


cv_consume.wait(lock, [this]() { return count > 0; });

int consumedValue;

if (head != nullptr) {
Node* temp = head;
head = head->next;
consumedValue = temp->data;
delete temp;
count--;

// Notify producers that a space is available


cv_produce.notify_all();
}

return consumedValue;
}
};

void producer(LinkedListBuffer& buffer, int id) {


for (int i = 1; i <= 5; ++i) {
buffer.produce(i);
cout << "Producer " << id << " produced: " << i << endl;
this_thread::sleep_for(chrono::milliseconds(200));
}
}
void consumer(LinkedListBuffer& buffer, int id) {
for (int i = 1; i <= 5; ++i) {
int consumedValue = buffer.consume();
cout << "Consumer " << id << " consumed: " << consumedValue <<
endl;
this_thread::sleep_for(chrono::milliseconds(300));
}
}

int main() {
LinkedListBuffer buffer;

thread producer1(producer, ref(buffer), 1);


thread producer2(producer, ref(buffer), 2);
thread consumer1(consumer, ref(buffer), 1);
thread consumer2(consumer, ref(buffer), 2);

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_______________

Course Code and Title: ____CT-353 Operating system________________________________

Laboratory Session No: ________ Date: ______________

Cognitive Domain Assessment Rubric – Level C3 (for Laboratory Work)

Criterion Level of Attainment

1 2 3 4 5

Problem Not able to Able to Able to Able to


Identification correctly Able to partially partially correctly correctly
identify the identify the identify the identify the identify the
problem(s) problem(s) but problem(s) problem(s) problem(s)
and all cannot identify and the but not all and all the
required data required data required the required data
(input and (input and data (input required data (input and
output). output). and output). (input and output).
output).
Problem Uses an Uses an Uses an Uses an Uses an
Solving Skills ineffective inadequate adequate effective effective
strategy to strategy to strategy to strategy to strategy to
solve the solve the solve the solve the solve the
problem(s). problem(s). problem(s). problem(s). problem(s).
Implementation Incomplete Inadequate Adequate Efficient Outstanding
implementation. implementation. implementation. implementation. implementation.

Deliverable(s) Cannot generate Generates Generates Generates Generates


the desired partially partially correct correct but correct and
output. correct and and partially complete
incomplete partially complete output.
output. complete output.
output.
Depth of Does not Limited grasp Fair grasp of the Good grasp Thorough
Knowledge grasp the of the concepts of the grasp of the
concepts concepts covered in the concepts concepts
covered in the covered in the lab and can covered in the covered in the
lab and cannot lab and cannot vaguely identify lab but cannot lab and can
identify any identify any a real-life effectively apply apply it in real
real-life real-life application. it in real life life situations.
application. application. situations.

Weighted CLO (Cognitive) Score

Remarks

Instructor’s Signature with Date

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);

else if(Philostatus[philID].left==1 &&


Philostatus[philID].right==1){

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_______________

Course Code and Title: ____CT-353 Operating system________________________________

Laboratory Session No: ________ Date: ______________

Cognitive Domain Assessment Rubric – Level C3 (for Laboratory Work)

Criterion Level of Attainment

1 2 3 4 5

Problem Not able to Able to Able to Able to


Identification correctly Able to partially partially correctly correctly
identify the identify the identify the identify the identify the
problem(s) problem(s) but problem(s) problem(s) problem(s)
and all cannot identify and the but not all and all the
required data required data required the required data
(input and (input and data (input required data (input and
output). output). and output). (input and output).
output).
Problem Uses an Uses an Uses an Uses an Uses an
Solving Skills ineffective inadequate adequate effective effective
strategy to strategy to strategy to strategy to strategy to
solve the solve the solve the solve the solve the
problem(s). problem(s). problem(s). problem(s). problem(s).
Implementation Incomplete Inadequate Adequate Efficient Outstanding
implementation. implementation. implementation. implementation. implementation.

Deliverable(s) Cannot generate Generates Generates Generates Generates


the desired partially partially correct correct but correct and
output. correct and and partially complete
incomplete partially complete output.
output. complete output.
output.
Depth of Does not Limited grasp Fair grasp of the Good grasp Thorough
Knowledge grasp the of the concepts of the grasp of the
concepts concepts covered in the concepts concepts
covered in the covered in the lab and can covered in the covered in the
lab and cannot lab and cannot vaguely identify lab but cannot lab and can
identify any identify any a real-life effectively apply apply it in real
real-life real-life application. it in real life life situations.
application. application. situations.

Weighted CLO (Cognitive) Score

Remarks

Instructor’s Signature with Date

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);

void go_for_dinner(int philID) {


if (philosophers[philID].left == 10 && philosophers[philID].right
== 10) {
std::cout << "Philosopher " << philID + 1 << " completed his
dinner\n";
}
else if (philosophers[philID].left == 1 &&
philosophers[philID].right == 1) {
std::cout << "Philosopher " << philID + 1 << " completed his
dinner\n";
philosophers[philID].left = philosophers[philID].right = 10;
int otherFork = philID - 1;
if (otherFork == -1) otherFork = n - 1;

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:

NED University of Engineering and


Technology
Department of Computer Science & IT

Bachelors in ___CS&IT_______________

Course Code and Title: ____CT-353 Operating system________________________________

Laboratory Session No: ________ Date: ______________

Cognitive Domain Assessment Rubric – Level C3 (for Laboratory Work)


Criterion Level of Attainment

1 2 3 4 5

Problem Not able to Able to Able to Able to


Identification correctly Able to partially partially correctly correctly
identify the identify the identify the identify the identify the
problem(s) problem(s) but problem(s) problem(s) problem(s)
and all cannot identify and the but not all and all the
required data required data required the required data
(input and (input and data (input required data (input and
output). output). and output). (input and output).
output).
Problem Uses an Uses an Uses an Uses an Uses an
Solving Skills ineffective inadequate adequate effective effective
strategy to strategy to strategy to strategy to strategy to
solve the solve the solve the solve the solve the
problem(s). problem(s). problem(s). problem(s). problem(s).
Implementation Incomplete Inadequate Adequate Efficient Outstanding
implementation. implementation. implementation. implementation. implementation.

Deliverable(s) Cannot generate Generates Generates Generates Generates


the desired partially partially correct correct but correct and
output. correct and and partially complete
incomplete partially complete output.
output. complete output.
output.
Depth of Does not Limited grasp Fair grasp of the Good grasp Thorough
Knowledge grasp the of the concepts of the grasp of the
concepts concepts covered in the concepts concepts
covered in the covered in the lab and can covered in the covered in the
lab and cannot lab and cannot vaguely identify lab but cannot lab and can
identify any identify any a real-life effectively apply apply it in real
real-life real-life application. it in real life life situations.
application. application. situations.

Weighted CLO (Cognitive) Score

Remarks

Instructor’s Signature with Date

You might also like









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.scribd.com/document/791362722/LAB-2-OS

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy