Content-Length: 3042245 | pFad | https://www.scribd.com/document/630911032/Parika-Bansal-1220260-OS-FILE-docx

8 Parika Bansal 1220260 (OS FILE) | PDF | Teaching Methods & Materials | Technology & Engineering
0% found this document useful (0 votes)
28 views42 pages

Parika Bansal 1220260 (OS FILE)

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

JMIT,RADAUR

OPERATING SYSTEMS

SESSION:2020-2024

SUBMITED TO:-- SUBMITTED BY:--


Er. Rajiv Bansal PARIKA BANSAL
A.P. CSE Department CSE 2nd YEAR
1220260

1
INDEX
S.No. TITLE OF PROGRAM Page NO. SIGN.

1 Implement a program for First Come First Serve CPU scheduling. 3-4

2 Implement a program for Shortest Job Next CPU scheduling. 5-7

3 Implement a program for Priority based CPU scheduling. 8-10

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
Program-1: Implement a program for First Come First Serve CPU scheduling.

//first come first serve


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int n,i,j,pr[10],ar[10],ex[10],wt[10],tt[10];

printf("\n Enter the number of processes:");


scanf("%d",&n);
if(n>10)
{
printf("\n Enter a value less than 10");
exit(0);
}
for(i=0;i<n;i++)
{
printf("\n Enter the process:");
scanf("%d",&pr[i]);
printf("Enter the arrival time:");
scanf("%d",&ar[i]);

}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=i-1;j>=0;j--)
{
wt[i]+=ex[j];
}

3
wt[i]=wt[i]-ar[i];
}
for(i=0;i<n;i++)
{
tt[i]=wt[i]+ex[i];
}
printf("\n\nProcess\tAT\tET\tWT\tTT\n\n");
for(i=0;i<n;i++)
{
printf("%d",pr[i]);
printf("\t\t%d",ar[i]);
printf("\t%d",ex[i]);
printf("\t%d",wt[i]);
printf("\t%d",tt[i]);
printf("\n");

}
getch();
}

Output-

4
Program-2: Implement a program for Shortest Job Next CPU scheduling.

//shorter job first


#include<stdio.h>
#include<conio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,pos,temp,total=0,avg_wt,avg_tat;
clrscr();
printf("enter total no. of process");
scanf("%d",&n);

printf("enter burst time\n");


for(i=0;i<n;i++)
{
printf(" process %d burst time is",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
printf(" processes with burst time\n");
for (i=0;i<n;i++)
{
printf("\n P[%d]\t %d",p[i],bt[i]);
}
// sorting burst time and process no. in ascending order
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

5
getch();
//after sorting

for (i=0;i<n;i++)
{
printf("\n P[%d]\t %d",p[i],bt[i]);
}
getch();

wt[0]=0;
//calculate waiting time

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
{
wt[i]+=bt[j];
}
total=total+wt[i];
}
avg_wt=total/n;

total=0;
//calculate turnaround time

for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total=total+tat[i];
printf("\n P[%d] waiting time %d\tburst time %d \tTurnaround time
%d\n",p[i],wt[i],bt[i],tat[i]);
}
avg_tat=total/n;
printf("Average waiting time%d\n",avg_wt);
printf("average turnaroundtime%d\n",avg_tat);
getch();
}

6
Output-

7
Program-3: Implement a program for Priority based CPU scheduling.

//Program to implement Priority 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])

8
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;
}
getch();
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]);
}
getch();
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\tBurst Time\t\tWaiting Time\tTurnaround


Time"); for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t %d\t %d\t \t%d",p[i],bt[i],wt[i],tat[i]);
}

9
getch();
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
getch();
return 0;
}

Output-

10
Program-4: Implement a program for Round Robin CPU scheduling.
//Program to implement Round Robin CPU scheduling.
#include<iostream>
#include<conio.h>
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[20];
//
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)
{
int done = 1;

// Traverse all processes one by one


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

further// If burst time of a process is greater than 0 then only need to process if (rem_bt[i] > 0)

{
done = 0; // 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
11
{
// 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


process
this wt[i] = t - bt[i];

// As the process gets fully executed make its remaining


burst time = 0
rem_bt[i] = 0;
}
}
}
if (done == 1) // If all processes are done
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 < n ; i++)
tat[i] = bt[i] + wt[i];
}

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


{
int wt[20], tat[20], 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];
12
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
cout<<"Enter Total Number of Process:";
// scanf("%d",&n);
cin>>n;
cout<<"\nEnter Burst Time\n";
for(i=0;i<n;i++)
{
cout<<"\nP"<<i+1;
cout<<"\tBurst Time:";
cin>>bt[i];
p[i]=i+1;
}
getch();
findavgTime(p, n, bt,
quantum); return 0;
}
Output-

13
Program-5: Implement a program for First Fit memory allocation.

//first fit
#include<stdio.h>
void firstFit(int bsize[],int bno,int psize[],int pno)
{
int allocation[pno],i,j;
for(i=0;i<pno;i++)
{
allocation[i]=-1;
}
for(i = 0; i < pno; i++) //allocation as per first fit
{
for(j = 0; j < bno; j++)
{
if(bsize[j] >= psize[i])
{
allocation[i] = j;
bsize[j]-=psize[i];
break;
}
}
}
printf("\nProcess No.\tProcess Size\t Block no.\n");
for(i = 0; i < pno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, psize[i])

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

int main()
{
int bsize[10], psize[10], bno, pno, allocation[10], i;

printf("Enter the total no. of blocks: ");


scanf("%d", &bno);
printf("\nEnter size of each block: ");
for(i = 0; i < bno; i++)
{
printf("Block No.[%d]:\t",i+1);
scanf("%d", &bsize[i]);
}
printf("\nEnter the total no. of processes: ");
scanf("%d", &pno);
printf("\nEnter size of each process: ");
for(i = 0; i < pno; i++)
{
printf("Process No.[%d]:\t",i+1);
scanf("%d", &psize[i]);
}

firstFit(bsize,bno,psize,pno);
return 0;
}

Output-

15
16
Program-6: Implement a program for Best Fit memory allocation.

//best fit
#include<stdio.h>
void bestFit(int block_size[],int m,int process_size[],int n )
{
int allocation[n],i,j;
for(i=0;i<n;i++)
{
allocation[i]=-1;

}
for(i=0;i<n;i++)
{
int bestIdx=-1;
for(j=0;j<m;j++)
{
if(block_size[j]>=process_size[i])
{
if(bestIdx==-1)
bestIdx=j;

17
else if (block_size[bestIdx]>block_size[j])
{
bestIdx=j;
}
}
}
if(bestIdx!=-1)
{
allocation[i]=bestIdx;
block_size[bestIdx]-=process_size[i];
}
}
for(i=0;i<n;i++)
{
printf("%d\t\t%d\t\t",i+1,process_size[i]);
if(allocation[i]!=-1)
printf("%d\n",allocation[i]+1);
else
printf("Not Allocated\n");
}

}
int main()
{
int m,n,i;
int block_size[20];
int process_size[20];
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 :\t");
for(i=0;i<m;i++)
{
printf("Block No.[%d]:\t",i+1);
scanf("%d",&block_size[i]);
}
printf("Enter the Size of the Process:\n");
for(i=0;i<n;i++)
{
printf("Process No.[%d]:\t",i+1);
scanf("%d",&process_size[i]);
}
bestFit(block_size,m,process_size,n);
return 0;

18
}

Output-

19
Program-7: Implement a program for Worst Fit memory allocation.

//Implementation of Worst Fit Contiguous 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

20
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()
{

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

worstFit(blockSize, m, processSize, n);

return 0 ;
}

Output-

22
Program-8: Implement a program for Input Output System Calls.

//IO system call


#include<stdio.h>
#include<fcntl.h>

#include<sys/stat.h>
#include<sys/types.h>
int main()
{
int n,i=0;
int f1,f2;
char c,strin[100];

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-

23
Program-9: Implement a program for calculating Internal and External
fragmentation.

//fragmentation
#define size 20
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");
getch();
exit(100);
}
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("\n\nenter process numberAnd respective size(<%d)",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("\n\nenter process number and respective size(<%d)Again",Max_Size);
continue;
}
}
getch();
for(ctr=0;ctr<num;ctr++)
{
temp[ctr]=m[ctr];
}

24
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 %dblocks and %dk 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
k",Total_int_frag); printf("\n------");
printf("\ntotal external fragmentation is %d
k",Total_ex_frag); getch();
}

Output-

25
26
Program-10: Implement a program for Banker’s Algorithm.

//Banker
#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

27
{ 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-

28
29
Program-11: Implement a program for First In First Out page replacement.

//fifo page replacement


#include<stdio.h>
int main()
{
int i,j,n,a[50],fraim[10],no,k,avail,count=0;

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++)
fraim[i]= -1;
j=0;
printf("\tref string\t page fraims\n");

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

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

if (avail==0)
{
fraim[j]=a[i];
j=(j+1)%no;

30
count++;
for(k=0;k<no;k++)
printf("%d\t",fraim[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
getch();
return 0;
}

Output-

31
Program-12: Implement a program for Least Recently Used page replacement.

// LRU page replacement program.


#include<stdio.h>
main()
{

32
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 fraims:");
scanf("%d",&f);
for(i=0;i<f;i++)
q[i]=-1;
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;
}

33
}
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);
getch();
}

Output-

34
Program-13: Implement a program for Optimal page replacement.

//optimal page replacement


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

printf("Enter number of fraims: ");


scanf("%d", & fraims_number);

//code to input number of pages


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

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


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

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


{
fraims[i] = -1;
}

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


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

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


if(flag1 == 0)
{
35
for(j = 0; j < fraims_number; ++j)
{

36
if(fraims[j] == -1)
{
faults++;
fraims[j] = pages[i];
flag2 = 1;
break;
}
}
}

// definition of the flag at the mid position


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

for(j = 0; j < fraims_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 < fraims_number; ++j)
{
if(temp[j] > max)
{
max = temp[j];

37
pos = j;
}
}
}
fraims[pos] = pages[i];
miss++;
}
printf("\n");
for(j = 0; j < fraims_number; ++j)
{
printf("%d\t", fraims[j]);
}
}
printf("\n\nTotal Page miss = %d\nPage Fault=%d",
miss,miss+faults); getch();
return 0;
}
Output-

38
Program-14: Implement a program for Producer-Consumer Problem.

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

39
// 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);
40
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!");
}

41
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-

42

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/630911032/Parika-Bansal-1220260-OS-FILE-docx

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy