Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Index

S.No. Title of Program Page


No.
1. Implement a program for First Come First Serve CPU
scheduling.
2. Implement a program for Shortest Job Next CPU scheduling.
3. Implement a program for Priority Based CPU scheduling.
4. Implement a program for Round Robin CPU scheduling.
5. Implement a program for First Fit memory allocation.
6. Implement a program for Best Fit memory allocation.
7. Implement a program for Worst Fit memory allocation.
8. Implement a program for Input Output System Calls.
9. Implement a program for calculating Internal and External
fragmentation
10. Implement a program for Banker’s Algorithm.
11. Implement a program for First In First Out page replacement.
12. Implement a program for Least Recently Used page
replacement.
13. Implement a program for Optimal page replacement.
14. Implement a program for Producer-Consumer Problem.
2. Implement a program for First Come First Serve
CPU Scheduling

#include<stdio.h>

int main()

int P,bt[20],wt[20],tat[20],avg_wt=0,avg_tat=0,i,j;

printf("Enter total number of processes:");

scanf("%d",&P);

printf("\nEnter Process Burst Time\n");

for(i=0;i<P;i++)

printf("P[%d]:",i+1);

scanf("%d",&bt[i]);

wt[0]=0; //waiting time for first process is 0

//calculating waiting time

for(i=1;i<P;i++)

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

} printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time

for(i=0;i<P;i++){
tat[i]=bt[i]+wt[i];

avg_wt+=wt[i];

avg_tat+=tat[i];

printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);

avg_wt/=i;

avg_tat/=i;

printf("\n\nAverage Waiting Time:%d",avg_wt);

printf("\nAverage Turnaround Time:%d",avg_tat);

return 0;

OUTPUT:

2. Implement a program for Shortest Job Next


CPU Scheduling.
#include<iostream>
#include<iostream>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void arrangeArrival(int num, int mat[][3]) {
for(int i=0; i<num; i++) {
for(int j=0; j<num-i-1; j++) {
if(mat[1][j] > mat[1][j+1]) {
for(int k=0; k<5; k++) {
swap(mat[k][j], mat[k][j+1]);
}
}
}
}
}
void completionTime(int num, int mat[][3]) {
int temp, val;
mat[3][0] = mat[1][0] + mat[2][0];
mat[5][0] = mat[3][0] - mat[1][0];
mat[4][0] = mat[5][0] - mat[2][0];
for(int i=1; i<num; i++) {
temp = mat[3][i-1];
int low = mat[2][i];
for(int j=i; j<num; j++) {
if(temp >= mat[1][j] && low >= mat[2][j]) {
low = mat[2][j];
val = j;
}
}
mat[3][val] = temp + mat[2][val];
mat[5][val] = mat[3][val] - mat[1][val];
mat[4][val] = mat[5][val] - mat[2][val];
for(int k=0; k<6; k++) {
swap(mat[k][val], mat[k][i]);
}
}
}
int main() {
int num = 3, temp;
int mat[6][3] = {1, 2, 3, 3, 6, 4, 2, 3, 4};
cout<<"Before Arrange...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<num; i++) {
cout<<mat[0][i]<<"\t\t"<<mat[1][i]<<"\t\t"<<mat[2][i]<<"\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[0][i]<<"\t\t"<<mat[1][i]<<"\t\t"<<mat[2][i]<<"\t\t"<<mat[4][i]<<"\t\
t"<<mat[5][i]<<"\n";
}
}
Output:
3. Implement a program for Priority based
CPU Scheduling.

#include<stdio.h>;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf(“Enter Total Number of Process:”);
scanf(“%d”,&n);
printf(“\nEnter Burst Time and Priority\n”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\n”,i+1);
printf(“Burst Time:”);
scanf(“%d”,&bt[i]);
printf(“Priority:”);
scanf(“%d”,&pr[i]);
p[i]=i+1;
}
printf(“\nEntered Processes with their Burst Times and Priorities are:\n”);
printf(“\nProcess\t\t Burst Time\t\t\t Priority”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\t\t\t %d\t\t\t \t%d”,p[i],bt[i], pr[i]”);
}
//sorting burst time, priority and process number in ascending order using selection
sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
printf(“\n\nAfter Sorting…”);
printf(“n\nProcess\t\t Burst Time\t\t\t Priority”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\t\t\t %d\t\t\t \t%d”,p[i],bt[i], pr[i]);
}
wt[0]=0; //waiting time for first process is zero
for(i=1;i<n;i++) //calculate waiting time
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n; //average waiting time
total=0;
printf(“\n\nProcess\t\t Burst Time\t\t \tWaiting Time\t\t\tTurnaround Time”);
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf(“\nP[%d]\t\t\t %d\t\t\t %d\t\t\t \t%d”,p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf(“\n\nAverage Waiting Time=%d”,avg_wt);
printf(“\nAverage Turnaround Time=%d\n”,avg_tat);
return 0;
}

OUTPUT:
4. Implement a program for Round Robin CPU
Scheduling.

#include<iostream>;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{
int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
int t = 0; // Current time
while (1)
{
bool done = true;
for (int i = 0 ; i < n; i++)
{
if (rem_bt[i] < 0)
{
done = false; // There is a pending process
if (rem_bt[i] < quantum)
{
t += quantum;
rem_bt[i] -= quantum;
}
else
{

t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == true) // If all processes are done
break;
}
}
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 quantum)
{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt, quantum);


findTurnAroundTime(processes, n, bt, wt, tat);
cout <<”Processes”<< “ Burst time”<<”Waiting time “<< “ Turn around time\n”;
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;
}
int main()
{
int bt[20],p[20],i,n;
int quantum = 2; // Time quantum
printf(“Enter Total Number of Process:”);
scanf(“%d”,&n);
printf(“\nEnter Burst Time\n”);
for(i=0;i<n;i++)
{
printf(“\nP[%d]\n”,i+1);
printf(“Burst Time:”);
scanf(“%d”,&bt[i]);
p[i]=i+1;
}
findavgTime(p, n, bt, quantum);
return 0;
}

OUTPUT:
5. Implement a program for First Fit Memory
Allocation.

#include<stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n)
{
int allocation[n]; // Stores block id of the block allocated to a process
for (int i=0; i<n; i++)
{
allocation[i]=-1; // Initially no block is assigned to any process
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{
allocation[i] = j; // allocate block j to p[i] process
// Reduce available memory in this block.
blockSize[j] -= processSize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\tBlock no.\n");
for (int i = 0; i < n; i++)
{
printf("%d\t\t%d\t\t",i+1,processSize[i]);

if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

int main()
{
int blockSize[20],processSize[20],m,n,i;
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &m);
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &n);
printf("\nEnter the Size of the Blocks:\n");
for(i = 0; i < m; i++)
{
printf("Block No.[%d]:\t", i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter the Size of the Process:\n");
for(i = 0; i < n; i++)
{
printf("Process No.[%d]:\t", i + 1);
scanf("%d", &processSize[i]);
}
firstFit(blockSize, m, processSize, n);
return 0 ;
}

Output:

6. Implement a program for Best Fit Memory


Allocation.

#include<stdio.h>
using namespace std;
void bestfit(int blockSize[], int m, int processSize[], int n);
{
int allocation[n];
for(int i=0; i<n; i++)
{
allocation[i]=-1;
}
for(int i=0; i<n; i++)
{
int bestIdx = -1;
for(int j=0; j<m; j++)
{
if(blockSize[j] >= processSize[i])
{
if(bestIdx == -1)
bestIdx = j;
else if(blockSize[bestIdx] > blockSize[j])
bestIdx = j;
}
}
if(bestIdx != -1)
{
allocation[i] = bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}
printf("\n Process No.\t Process Size\t Block No.\n ");
for(int i=0; i<n; i++)
{
printf("%d\t\t%d\t\t", i+1, processSize[i]);
if(allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated \n");
}

}
int main()
{
int blockSize[20], processSize[20], m,n,i;
printf("\n Enter Total No. of Blocks: \t");
scanf("%d", &m);
printf("\n Enter Total No. of Process: \t");
scanf("%d", &n);
printf("\n Enter Size of Blocks: \t");
for(i=0; i<m; i++)
{
printf("Block No.[%d]: \t",i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter Size of Process: \n");
for(i=0; i<n; i++)
{
printf("Process No.[%d]: \t",i + 1);
scanf("%d", &processSize[i]);
}
bestfit(blockSize, m, processSize, n);
return 0;
}

Output:

7. Implement a program for Worst Fit Memory


Allocation.

#include<stdio.h>

void worstFit(int blockSize[], int m, int processSize[], int n)


{
int i,j,allocation[20]; // Stores block id of the block allocated to a process

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


{
allocation[i]=-1; // Initially no block is assigned to any process
}

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


{
int wstIdx = -1;
for (j=0; j<m; j++)
{
if (blockSize[j] >= processSize[i])
{
if (wstIdx == -1)
wstIdx = j;
else if (blockSize[wstIdx] < blockSize[j])
wstIdx = j;
}
}

// If we could find a block for current process


if (wstIdx != -1)
{
// allocate block j to p[i] process
allocation[i] = wstIdx;

// Reduce available memory in this block.


blockSize[wstIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (i = 0; i < n; i++)
{
printf("%d\t\t%d\t\t",i+1,processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");

}
}

int main()
{
int blockSize[20],processSize[20],m,n,i;
clrscr();
printf("\nEnter the Total Number of Blocks:\t");
scanf("%d", &m);
printf("\nEnter the Total Number of Processes:\t");
scanf("%d", &n);
printf("\nEnter the Size of the Blocks:\n");
for(i = 0; i < m; i++)
{
printf("Block No.[%d]:\t", i + 1);
scanf("%d", &blockSize[i]);
}
printf("Enter the Size of the Process:\n");
for(i = 0; i < n; i++)
{
printf("Process No.[%d]:\t", i + 1);
scanf("%d", &processSize[i]);
}

worstFit(blockSize, m, processSize, n);

return 0 ;
}
OUTPUT:

8. Implement a program for Input Output System


Calls.

#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];
clrscr();
f1=open("data",O_RDWR | O_CREAT | O_TRUNC);
while((c=getchar())!='\n'){
strin[i++]=c;
}
strin[i]='\0';
write(f1,strin,i);
close(f1);
f2=open("data",O_RDONLY);
read(f2,strin,0);
printf("\n%s\n",strin);
close(f2);
getch();
return 0;

}
Output:

9. Implement a program for calculating Internal


and External Fragmentation.
#define size 20
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int p[size],m[size],temp[size];
int int_frag=0,Total_int_frag=0,Total_ex_frag=0;
int Max_size,Block_Size;
int UseBlock,RemainBlock,TotalUseBlock=0;
int num=0,ctr=0;
int TotalMemBlock;
int Max_Size;

printf(“\nEnter no. of process(should be less than %d):”,size);


scanf(“%d”,&num);
if(num>size)
{
printf(“\nInvalid no. of processes”);
}
printf(“\nEnter the Block_size:”);
scanf(“%d”,&Block_Size);
printf(“\nEnter max size process can have:”);
scanf(“%d”,&Max_Size);

TotalMemBlock=(num*Max_Size)/Block_Size;
printf(“\nEnter process number and respective size(<%d)\n”,Max_Size);
for(ctr=0;ctr<num;ctr++)
{
scanf(“%d%d”,p[ctr],&m[ctr]);
if(m[ctr]>Max_Size)
{
printf(“Enter the valid size(<%d)”,Max_Size);
ctr--;
printf(“\nEnter process number and respective size(<%d) again\n”,Max_Size);
}
}
for(ctr=0;ctr<num;ctr++)
{
temp[ctr]=m[ctr];
}
for(ctr=0;ctr<num;ctr++)
{
UseBlock=0;
while(temp[ctr]>0)
{
temp[ctr]=temp[ctr]-Block_Size;
UseBlock++;
TotalUseBlock++;
}

int_frag=abs(temp[ctr]);
Total_int_frag+=int_frag;
printf(“\n\n Process p%d uses %d blocks and %d units internal fragmentation”,p[ctr],UseBlock,int_frag);
}
RemainBlock=TotalMemBlock-TotalUseBlock;
Total_ex_frag=RemainBlock*Block_Size;
printf(“\n\n\nResults are -------------------------\n\n”);
printf(“\n--------------------------------");
printf(“\nTotal memory blocks are %d”,TotalMemBlock);
printf(“\n---------------------------------");
printf(“\nTotal used blocks are %d”,TotalUseBlock);
printf(“\n-------------------------------------");
printf(“\nTotal unused blocks are %d”,RemainBlock);
printf(“\n----------------------------");
printf(“\nTotal internal fragmentation is %d units”,Total_int_frag);
printf(“\n------------------------------");
printf(“\nTotal external fragmentation is %d units”,Total_ex_frag);
getch();
}

Output:
10. Implement a program for Banker’s
Algorithm.

#include<iostream>

#include<conio.h>

using namespace std;

int main()

// P0, P1, P2, P3, P4 are the Process names here

int n, m, i, j, k;

n = 5; // Number of processes

m = 3; // Number of resources

int alloc[5][3] = { { 0, 1, 0 }, // P0 // Allocation Matrix

{ 2, 0, 0 }, // P1

{ 3, 0, 2 }, // P2

{ 2, 1, 1 }, // P3

{ 0, 0, 2 } }; // P4

int max[5][3] = { { 7, 5, 3 }, // P0 // MAX Matrix

{ 3, 2, 2 }, // P1

{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3

{ 4, 3, 3 } }; // P4

int avail[3] = { 3, 3, 2 }; // Available Resources

int f[5], ans[5], ind = 0;

for (k = 0; k < n; k++) {

f[k] = 0;

int need[5][3];

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

for (j = 0; j < m; j++) need[i][j] = max[i][j] - alloc[i][j]; }

int y = 0;

for (k = 0; k < 5; k++) {

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

if (f[i] == 0) {

int flag = 0;

for (j = 0; j < m; j++) {

if (need[i][j] > avail[j]){

flag = 1;

break;

if (flag == 0) {

ans[ind++] = i;

for (y = 0; y < m; y++)

avail[y] += alloc[i][y];

f[i] = 1;

cout << "Following is the SAFE Sequence"<<endl;


for (i = 0; i < n - 1; i++)

cout << " P" << ans[i] << " ->";

cout << " P" << ans[n - 1] <<endl;

getch();

return (0);

Output:
11. Implement a program for First in First Out
page replacement.
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
clrscr();
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE REFERENCE STRING :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");

for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;

for(k=0;k<no;k++)
{
if(frame[k]==a[i])
avail=1;
}

if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
getch();
return 0;
}

OUTPUT:
12.
Implement a program for Least Recently Used
page replacement.

#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];

printf("Enter no of pages:");
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);

Output:
13.
Implement a program for Optimal page
replacement.

#include<stdio.h>
int main()
{
//variable declaration and initialization
int frames_number, pages_number, frames[10], pages[30], temp[10], flag1, flag2,
flag3, i, j, k, pos, max, miss = 0,faults=0;
//code to input the frame number

printf("Enter number of frames: ");


scanf("%d", & frames_number);

//code to input number of pages


printf("Enter number of pages: ");
scanf("%d", &pages_number);

//code to define reference string, page numbers, and frame numbers


printf("Enter page reference string: ");
for(i = 0; i < pages_number; ++i)
{
scanf("%d", &pages[i]);
}

for(i = 0; i < frames_number; ++i)


{
frames[i] = -1;
}

for(i = 0; i < pages_number; ++i)


{
flag1 = flag2 = 0;
for(j = 0; j < frames_number; ++j)
{
if(frames[j] == pages[i])
{
flag1 = flag2 = 1;
break;
}
}

//definition of the flag at the starting of the string


if(flag1 == 0)
{
for(j = 0; j < frames_number; ++j)
{
if(frames[j] == -1)
{
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}

// definition of the flag at the mid position


if(flag2 == 0)
{
flag3 =0;
for(j = 0; j < frames_number; ++j)
{
temp[j] = -1;
for(k = i + 1; k < pages_number; ++k)
{
if(frames[j] == pages[k])
{
temp[j] = k;
break;
}
}
}

for(j = 0; j < frames_number; ++j)


{
if(temp[j] == -1)
{
pos = j;
flag3 = 1;
break;
}
}

//definition of flag at the rear position


if(flag3 ==0)
{
max = temp[0];
pos = 0;
for(j = 1; j < frames_number; ++j)
{
if(temp[j] > max)
{
max = temp[j];
pos = j;
}
}
}

frames[pos] = pages[i];
miss++;
}

printf("\n");
for(j = 0; j < frames_number; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page miss = %d\nPage Fault=%d", miss,miss+faults);

return 0;
}

Output:
14. Implement a program for Producer-Consumer
Problem.

#include <stdio.h>
#include <stdlib.h>

// Initialize a mutex to 1
int mutex = 1;

// Number of full slots as 0


int full = 0;

// Number of empty slots as size


// of buffer
int empty = 10, x = 0;

// Function to produce an item and


// add it to the buffer
void producer()
{
// Decrease mutex value by 1
--mutex;

// Increase the number of full


// slots by 1
++full;

// Decrease the number of empty


// slots by 1
--empty;

// Item produced
x++;
printf("\nProducer produces"
"item %d",
x);

// Increase mutex value by 1


++mutex;
}

// Function to consume an item and


// remove it from buffer
void consumer()
{
// Decrease mutex value by 1
--mutex;

// Decrease the number of full


// slots by 1
--full;

// Increase the number of empty


// slots by 1
++empty;
printf("\nConsumer consumes "
"item %d",
x);
x--;

// Increase mutex value by 1


++mutex;
}

// Driver Code
int main()
{
int n, i;
printf("\n1. Press 1 for Producer"
"\n2. Press 2 for Consumer"
"\n3. Press 3 for Exit");

// Using '#pragma omp parallel for'


// can give wrong value due to
// synchronisation issues.

// 'critical' specifies that code is


// executed by only one thread at a
// time i.e., only one thread enters
// the critical section at a given time
#pragma omp critical

for (i = 1; i > 0; i++) {

printf("\nEnter your choice:");


scanf("%d", &n);

// Switch Cases
switch (n) {
case 1:

// If mutex is 1 and empty


// is non-zero, then it is
// possible to produce
if ((mutex == 1)
&& (empty != 0)) {
producer();
}

// Otherwise, print buffer


// is full
else {
printf("Buffer is full!");
}
break;

case 2:

// If mutex is 1 and full


// is non-zero, then it is
// possible to consume
if ((mutex == 1)
&& (full != 0)) {
consumer();
}

// Otherwise, print Buffer


// is empty
else {
printf("Buffer is empty!");
}
break;

// Exit Condition
case 3:
exit(0);
break;
}
}
}

Output:

You might also like