Os Practical File
Os Practical File
Os Practical File
PRACTICAL FILE
OPERATING SYSTEM(CS-5002)
Name: Year:
Section: subject:
Semester:
Approved by:
EXPERIMENT NO. 1
#include<iostream>
int main()
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
cin>>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];
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
#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);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
total+=wt[i];
}
Output
EXPERIMENT NO. 3
#include<iostream>
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;
for(i=0;i<n;i++)
cout<<"\nP["<<i+1<<"]\n";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>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;
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
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];
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];
scanf("%d",&time_quantum);
for(time=0,count=0;remain!=0;)
{
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=time_quantum;
time+=time_quantum;
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;
}
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<<“\n0.MENU”;
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;
}
cout<<“\nEnter your choice:”;
cin>>ch;
getch();
}while(ch<5);
}
EXPERIMENT NO. 6
#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
1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 2
Enter length of reference string : 12
1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 3
1.FIFO
2.LRU
3.Optimal
4.Exit
Enter Choice : 4
EXPERIMENT NO. 7
#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();
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
cout<<"\nDeadlock occurence!!\n";
getch();
}
EXPERIMENT NO. 8
#include<stdio.h>
#include<semaphore.h>
#include<pthread.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];
int state[N];
int phil_num[N]={0,1,2,3,4};
int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinking\n",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}
Write a program to implement & Compare various Disk & Drum scheduling Algorithms
#include<stdio.h>
#include<math.h>
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 MENU \n");
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;
Output
MENU
1. FCFS
2. SSTF
3. SCAN
4. EXIT
FCFS
*****
MENU
1. FCFS
2. SSTF
3. SCAN
4. EXIT
SSTF
*****
MENU
1. FCFS
2. SSTF
3. SCAN
4. EXIT
SCAN
*****
MENU
1. FCFS
2. SSTF
3. SCAN
4. EXIT
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t mutex,writeblock;
int data = 0,rcount = 0;
main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}