You are on page 1of 34

# IES COLLEGE OF TECHNOLOGY, BHOPAL

## Department of Computer Science & Engineering

PRACTICAL FILE
OPERATING SYSTEM(CS-5002)

Name: Year:

Section: subject:

Semester:

Approved by:
EXPERIMENT NO. 1

## Write a program to implement FCFS CPU scheduling algorithm

#include<iostream>

## using namespace std;

int main()

int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;

cin>>n;

## cout<<"\nEnter Process Burst Time\n";

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

cout<<"P["<<i+1<<"]:";

cin>>bt[i];

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

wt[i]=0;

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

wt[i]+=bt[j];

## //calculating turnaround time

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

tat[i]=bt[i]+wt[i];

avwt+=wt[i];

avtat+=tat[i];

cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];

avwt/=i;

avtat/=i;

return 0;

}
EXPERIMENT NO. 2

## Write a program to implement SJF CPU scheduling algorithm.

#include<stdio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

## printf("\nEnter Burst Time:\n");

for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

## //sorting burst time in ascending order using selection sort

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

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

total+=wt[i];
}

total=0;

## printf("\nProcess\t Burst Time \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\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

## avg_tat=(float)total/n; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

Output
EXPERIMENT NO. 3

## Write a program to implement Priority CPU Scheduling algorithm

#include<iostream>

## using namespace std;

int main()

int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;

cin>>n;

## cout<<"\nEnter Burst Time and Priority\n";

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

cout<<"\nP["<<i+1<<"]\n";

cout<<"Burst Time:";

cin>>bt[i];

cout<<"Priority:";

cin>>pr[i];

## p[i]=i+1; //contains process number

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

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

wt[i]=0;
for(j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

total=0;

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

total+=tat[i];

return 0;

}
EXPERIMENT NO. 4

## Write a program to implement Round Robin CPU scheduling algorithm

#include<stdio.h>

int main()

int count,j,n,time,remain,flag=0,time_quantum;

int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];

## printf("Enter Total Process:\t ");

scanf("%d",&n);

remain=n;

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

printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);

scanf("%d",&at[count]);

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

rt[count]=bt[count];

## printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

## printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

## if(rt[count]<=time_quantum && rt[count]>0)

{
time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

## if(rt[count]==0 && flag==1)

remain--;

printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);

wait_time+=time-at[count]-bt[count];

turnaround_time+=time-at[count];

flag=0;

if(count==n-1)

count=0;

else if(at[count+1]<=time)

count++;

else

count=0;

## printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;

}
EXPERIMENT NO. 5

Write a program to compare various CPU Scheduling Algorithms over different Scheduling Criteria

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class cpuschedule
{
int n,bu[20];
float twt,awt,wt[20],tat[20];
public:
void Getdata();
void fcfs();
void sjf();
void roundrobin();
};
//Getting no of processes and Burst time
void cpuschedule::Getdata()
{
int i;
cout<<“Enter the no of processes:”;
cin>>n;
for(i=1;i<=n;i++)
{
cout<<“\nEnter The BurstTime for Process p”<<i<<“=”;
cin>>bu[i];
}
}
//First come First served Algorithm
void cpuschedule::fcfs()
{
int i,b[10];
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
}
wt[1]=0;
for(i=2;i<=n;i++)
{
wt[i]=b[i-1]+wt[i-1];
}
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage Turnaround time=”<<sum;
}
//Shortest job First Algorithm
void cpuschedule::sjf()
{
int i,j,temp,b[10];
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
}
for(i=n;i>=1;i–)
{
for(j=2;j<=n;j++)
{
if(b[j-1]>b[j])
{
temp=b[j-1];
b[j-1]=b[j];
b[j]=temp;
}
}
}
wt[1]=0;
for(i=2;i<=n;i++)
{
wt[i]=b[i-1]+wt[i-1];
}
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage turnaround time=”<<sum;
}
//Round Robin Algorithm
void cpuschedule::roundrobin()
{
int i,j,tq,k,b[10],Rrobin[10][10],count[10];
int max=0;
int m;
float sum=0.0;
twt=0.0;
for(i=1;i<=n;i++)
{
b[i]=bu[i];
cout<<“\nBurst time for process p”<<i<<“=”;
cout<<b[i];
if(max<b[i])
max=b[i];
wt[i]=0;
}
cout<<“\nEnter the Time Quantum=”;
cin>>tq;
//TO find the dimension of the Round robin array
m=max/tq+1;
//initializing Round robin array
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
Rrobin[i][j]=0;
}
}
//placing value in the Rrobin array
i=1;
while(i<=n)
{
j=1;
while(b[i]>0)
{
if(b[i]>=tq)
{
b[i]=b[i]-tq;
Rrobin[i][j]=tq;
j++;
}
else
{
Rrobin[i][j]=b[i];
b[i]=0;
j++;
}
}
count[i]=j-1;
i++;
}
cout<<“Display”;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
cout<<“\nRr[“<<i<<“,”<<j<<“]=”<<Rrobin[i][j];
cout<<” “;
}
cout<<“\ncount=”<<count[i];
}
for(j=1;j<=n;j++)
{
for(i=1;i<=count[j];i++)
{
if(i==count[j])
{
for(k=1;k<j;k++)
{
if(k!=j)
wt[j]+=Rrobin[k][i];
}
}
else
for(k=1;k<=n;k++)
{
if(k!=j)
wt[j]+=Rrobin[k][i];
}
}
}
for(i=1;i<=n;i++)
cout<<“\nWaiting Time for process P”<<i<<“=”<<wt[i];
//calculating Average Weighting Time
for(i=1;i<=n;i++)
{
twt=twt+wt[i];
tat[i]=b[i]+wt[i];
sum+=tat[i];
}
awt=twt/n;
sum=sum/n;
cout<<“\nTotal Waiting Time=”<<twt;
cout<<“\nAverage Waiting Time=”<<awt;
cout<<“\nAverage turnaround time=”<<sum;
}
void main()
{
int ch=0,cho;
cpuschedule c;
clrscr();
do
{
switch(ch)
{
case 0:
cout<<“\n1.Getting BurstTime”;
cout<<“\n2.FirstComeFirstServed”;
cout<<“\n3.ShortestJobFirst”;
cout<<“\n4.RoundRobin”;
cout<<“\n5.EXIT”;
break;
case 1:
c.Getdata();
break;
case 2:
cout<<“FIRST COME FIRST SERVED SCHEDULING”;
c.fcfs();
break;
case 3:
cout<<“SHORTEST JOB FIRST SCHEDULING”;
c.sjf();
break;
case 4:
cout<<“ROUND ROBIN SCHEDULING”;
c.roundrobin();
break;
case 5:
break;
}
cin>>ch;
getch();
}while(ch<5);
}
EXPERIMENT NO. 6

## Write a program to implement & Compare various page replacement algorithm.

#include<stdio.h>
void FIFO();
void LRU();
void OPTIMAL();

int main()
{
int ch;
do
{
printf("\n\n\t1.FIFO\n\t2.LRU\n\t3.Optimal\n\t4.Exit\n\tEnter Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
FIFO();
break;
case 2:
LRU();
break;
case 3:
OPTIMAL();
break;
}
}while(ch!=4);
}
void FIFO()
{
int frame[3]={-1,-1,-1},ref[20],cnt=0,i,j,no,flag;
float ratio,hitcnt=0.00;
printf("\n\tEnter length of reference string : ");
scanf("%d",&no);
printf("\n\tEnter reference String with giving space ....\n\t");
for(i=0;i<no;i++)
scanf("%d",&ref[i]);
//printf("\n\tExecution is started here .....");
for(i=0;i<no;i++)
{
flag=0;
for(j=0;j<3;j++)
if(frame[j]==ref[i])
{
printf("\n\tPage Hit ");
hitcnt++;
flag=1;
break;
}
if(flag==0)
{
printf("\n\tPage Miss");
printf("\tBefore :\t");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
frame[cnt]=ref[i];
cnt++;
if(cnt>=3)
cnt=0;
printf("\tAfter :\t");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
}
}
ratio=hitcnt/no;
printf("\n\n\tHit ratio = %f ",ratio);
}
void LRU()
{
int frame[3]={-1,-1,-1},used[3]={-1,-1,-1},cnt=0,ref[20],i,j,flag,no,index,value;
float ratio,hitcnt=0;
printf("\n\tEnter length of reference string : ");
scanf("%d",&no);
printf("\n\tEnter reference String with giving space \n\t");
for(i=0;i<no;i++)
scanf("%d",&ref[i]);
//printf("\n\tExecution is started here ");
for(i=0;i<no;i++)
{
flag=0;
for(j=0;j<3;j++)
if(frame[j]==ref[i])
{
printf("\n\tPage Hit ");
hitcnt++;
flag=1;
used[j]=cnt;
break;
}
if(flag==0)
{
printf("\n\tPage Miss");
printf("\tBefore :");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
//selection of victim for replacement
index=0;
value=used[0];
if(cnt!=0) {
for(j=0;j<3;j++)
if(value>used[j]&&value!=used[j])
{
index=j;
value=used[j];
}
}
//printf("\tVictim is %d ",index);
frame[index]=ref[i];
used[index]=cnt;
printf("\tAfter :");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
}
cnt++;
}
ratio=hitcnt/no;
printf("\n\n\tHit ratio = %f ",ratio);
}
void OPTIMAL()
{
int frame[3]={-1,-1,-1},used[3]={-1,-1,-1},cnt=0,ref[20],i,j,flag,no,val1,val2,val3,index;
float ratio,hitcnt=0;
printf("\n\tEnter length of reference string : ");
scanf("%d",&no);
printf("\n\tEnter reference String with giving space \n\t");
for(i=0;i<no;i++)
scanf("%d",&ref[i]);
//printf("\n\tExecution is started here ");
for(i=0;i<no;i++)
{
flag=0;
for(j=0;j<3;j++)
if(frame[j]==ref[i])
{
flag=1;
printf("\n\tPage Hit");
hitcnt++;
break;
}
if(flag==0)
{
printf("\n\tPage Miss");
if(cnt<3)
{
frame[cnt]=ref[i];
printf("\tStatus :");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
cnt++;
}
else
{
printf("\tBefore :");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
//selection of victim
val1=frame[0];
flag=0;
for(j=i;j<no;j++)
if(ref[j]==val1)
{
val1=j;
flag=1;
break;
}
if(flag==0)
val1=no;
val2=frame[1];
flag=0;
for(j=i;j<no;j++)
if(ref[j]==val2)
{
val2=j;
flag=1;
break;
}
if(flag==0)
val2=no;
val3=frame[2];
flag=0;
for(j=i;j<no;j++)
if(ref[j]==val3)
{
val3=j;
flag=1;
break;
}
if(flag==0)
val3=no;
if(val1<val2)
if(val3<val2)
index=1;
else
index=2;
else
if(val3<val1)
index=0;
else
index=2;

frame[index]=ref[i];
printf("\tAfter :");
for(j=0;j<3;j++)
printf(" %d",frame[j]);
}
}
}
ratio=hitcnt/no;
printf("\n\n\tHit ratio = %f ",ratio);
}

/* OUTPUT
prashant@prashant-OptiPlex-755:~\$ gcc pgreplacement.c
prashant@prashant-OptiPlex-755:~\$ ./a.out

1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 1
Enter length of reference string : 12
Enter reference String with giving space ....
232152453252

## Page Miss Before : -1 -1 -1 After : 2 -1 -1

Page Miss Before : 2 -1 -1 After : 2 3 -1
Page Hit
Page Miss Before : 2 3 -1 After : 2 3 1
Page Miss Before : 231 After : 5 3 1
Page Miss Before : 531 After : 5 2 1
Page Miss Before : 521 After : 5 2 4
Page Hit
Page Miss Before : 524 After : 324
Page Hit
Page Miss Before : 324 After : 354
Page Miss Before : 354 After : 352

## Hit ratio = 0.250000

1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 2
Enter length of reference string : 12

232152453252

## Page Miss Before : -1 -1 -1 After : 2 -1 -1

Page Miss Before : 2 -1 -1 After : 2 3 -1
Page Hit
Page Miss Before : 2 3 -1 After : 2 3 1
Page Miss Before : 2 3 1 After : 2 5 1
Page Hit
Page Miss Before : 2 5 1 After : 2 5 4
Page Hit
Page Miss Before : 2 5 4 After : 3 5 4
Page Miss Before : 3 5 4 After : 3 5 2
Page Hit
Page Hit

1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 3

232152453252

## Page Miss Status : 2 -1 -1

Page Miss Status : 2 3 -1
Page Hit
Page Miss Status : 2 3 1
Page Miss Before : 2 3 1 After : 2 3 5
Page Hit
Page Miss Before : 2 3 5 After : 4 3 5
Page Hit
Page Hit
Page Miss Before : 4 3 5 After : 2 3 5
Page Hit
Page Hit

1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 4
EXPERIMENT NO. 7

## Write a program to implement Banker’s algorithms.

#include<iostream.h>
#include<conio.h>
void main()
{
int alloc[10][10],max[10][10],avail[10],n,m,need[10][10],i,j,k,finish[10],work[10];
int SSeq[10];
clrscr();

## cout<<"Enter no. of processes:";

cin>>n;
cout<<"Enter no. of resources:";
cin>>m;
// cout<<"Enter allocation matrix:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter no. of instances of "<<j+1<<"allocated to "<<i+1<<":" ;
cin>>alloc[i][j];
}
}
// cout<<"\nEnter max matrix";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<"\nEnter maximum instances of"<<j+1<<"allocated to"<<i+1<<":";
cin>>max[i][j];
}
}

for(i=0;i<m;i++)
{
cout<<"\nEnter available instances of"<<i+1;
cin>>avail[i];
}
//calculate need matrix
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
cout<<"\nAllocation matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<alloc[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nMax matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<max[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\nNeed matrix is:";
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<need[i][j]<<"\t";
}
cout<<endl;
}
//initialize work and finish
for(i=0;i<n;i++)
work[i]=avail[i];
k=0;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(finish[i]==0 && need[i][j]<=work[j])
{
SSeq[k]=i+1;
k++;
work[i]=work[i]+alloc[i][j];
}
}
}
cout<<"safe sequence is:";
for(i=0;i<k;i++)
cout<<SSeq[i]<<",";
if(k==n)
cout<<"\nThe system is safe.\n";
else
getch();
}
EXPERIMENT NO. 8

## Write a program to implement classical inter process communication problem (Dining_Philosophers)

#include<stdio.h>
#include<semaphore.h>

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];

## void * philospher(void *num);

void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
int phil_num[N]={0,1,2,3,4};

int main()
{
int i;
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
printf("Philosopher %d is thinking\n",i+1);
}
for(i=0;i<N;i++)
}

{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}

## void take_fork(int ph_num)

{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungry\n",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}

## void test(int ph_num)

{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] !=
EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and
%d\n",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eating\n",ph_num+1);
sem_post(&S[ph_num]);
}
}

## void put_fork(int ph_num)

{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d
down\n",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinking\n",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
EXPERIMENT NO. 9

Write a program to implement & Compare various Disk & Drum scheduling Algorithms

#include<stdio.h>
#include<math.h>

## void fcfs(int noq, int qu[10], int st)

{
int i,s=0;
for(i=0;i<noq;i++)
{
s=s+abs(st-qu[i]);
st=qu[i];
}
printf("\n Total seek time :%d",s);
}

## void sstf(int noq, int qu[10], int st, int visit[10])

{
int min,s=0,p,i;
while(1)
{
min=999;
for(i=0;i<noq;i++)
if (visit[i] == 0)
{
if(min > abs(st - qu[i]))
{
min = abs(st-qu[i]);
p = i;
}
}
if(min == 999)
break;
visit[p]=1;
s=s + min;
st = qu[p];
}
printf("\n Total seek time is: %d",s);
}

## void scan(int noq, int qu[10], int st, int ch)

{
int i,j,s=0;
for(i=0;i<noq;i++)
{
if(st < qu[i])
{
for(j=i-1; j>= 0;j--)
{
s=s+abs(st - qu[j]);
st = qu[j];
}
if(ch == 3)
{
s = s + abs(st - 0);
st = 0;
}
for(j = 1;j < noq;j++)
{
s= s + abs(st - qu[j]);
st = qu[j];
}
break;
}
}
printf("\n Total seek time : %d",s);
}

int main()
{
int n,qu[20],st,i,j,t,noq,ch,visit[20];
printf("\n Enter the maximum number of cylinders : ");
scanf("%d",&n);
printf("enter number of queue elements");
scanf("%d",&noq);
printf("\n Enter the work queue");
for(i=0;i<noq;i++)
{
scanf("%d",&qu[i]);
visit[i] = 0;
}
printf("\n Enter the disk head starting posision: \n");
scanf("%d",&st);
while(1)
{
printf("\n\n\t\t 1. FCFS \n");
printf("\n\n\t\t 2. SSTF \n");
printf("\n\n\t\t 3. SCAN \n");
printf("\n\n\t\t 4. EXIT \n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if(ch > 2)
{
for(i=0;i<noq;i++)
for(j=i+1;j<noq;j++)
if(qu[i]>qu[j])
{
t=qu[i];
qu[i] = qu[j];
qu[j] = t;
}
}
switch(ch)
{
case 1: printf("\n FCFS \n");
printf("\n*****\n");
fcfs(noq,qu,st);
break;

## case 2: printf("\n SSTF \n");

printf("\n*****\n");
sstf(noq,qu,st,visit);
break;
case 3: printf("\n SCAN \n");
printf("\n*****\n");
scan(noq,qu,st,ch);
break;
case 4: exit(0);
}
}
}

Output

## "disksche.c" 122L, 2076C written

[anandh@localhost ~]\$ cc disksche.c
[anandh@localhost ~]\$ ./a.out

## Enter the maximum number of cylinders : 200

enter number of queue elements5

89
132
42
187

100

1. FCFS

2. SSTF

3. SCAN

4. EXIT

FCFS

*****

1. FCFS

2. SSTF

3. SCAN

4. EXIT

SSTF

*****

1. FCFS

2. SSTF

3. SCAN

4. EXIT

SCAN

*****

1. FCFS

2. SSTF

3. SCAN

4. EXIT

## Enter your choice: 4

EXPERIMENT NO. 10

## Write a program to implement classical inter process communication problem(Reader Writers)

#include<stdio.h>
#include<semaphore.h>

sem_t mutex,writeblock;
int data = 0,rcount = 0;

## void *reader(void *arg)

{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}

## void *writer(void *arg)

{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}

main()
{
int i,b;