Os Lab Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

3.

Simulate the following CPU scheduling algorithms


a) Round Robin b) SJF c) FCFS d) Priority.
a)ROUND ROBIN:
Algorithm:
1. Start the process
2. Declare the array
3. Declare number of processes
4. Input the processes along with burst time and their time slice
5. For each and every process calculate turnaround time and waiting time
6. Finally, calculate total waiting time and total turnaround time
7. Display the results.

Program:
#include<stdio.h>
void main()
{
int n,i,j,temp,pos,p[10],rem_p[10],count,ts[10],pro[10],s[10],finishingtime=0,
arrivaltime=0, tat,wt,sum=0,sum1,sum3,sum2=0;
printf("enter the number of processes:");
scanf("%d",&n);
printf("\nenter burst time for each process:");
for(i=0;i<n;i++)
{
printf("\nenter the process[%d]:",i);
scanf("%d",&p[i]);
rem_p[i]=p[i];
printf("\nenter the timeslice for each process:");
for(i=0;i<n;i++)
{
scanf("%d",&ts[i]);
}
printf("\nprocess \tburst time\t timeslice");
for(i=0;i<n;i++)
{
printf("\n%d \t %d \t%d",i,p[i],ts[i]);
}
for(i=0,count=0;i<n;i++)
{
temp=ts[i];
if(rem_p[i]==0)
{
count++;
continue;
}
if(rem_p[i]>ts[i])
rem_p[i]=rem_p[i]-ts[i];
else
{
if(rem_p[i]>=0)
{
temp=rem_p[i];
rem_p[i]=0;
}
}
}
for(i=0;i<n;i++)
{
finishingtime=finishingtime+p[i];
printf("\nfinishing time of p[%d]=%d",i,finishingtime);
tat=finishingtime-arrivaltime;
printf("\nturn around time of p[%d]=%d",i,tat);
sum=sum+tat;
}
sum1=sum/n;
printf("\n______________________________");
printf("\naverage turn around time=%d",sum1);
printf("\n______________________________");
finishingtime=0;
for(i=0;i<n;i++)
{
if(i==0)
s[i]=0;
else
{
finishingtime=finishingtime+p[i-1];
s[i]=finishingtime;
}
wt=s[i]-arrivaltime;
printf("\nwaiting time of p[%d]=%d",i,wt);
sum2=sum2+wt;
}
sum3=(float)sum2/n;
printf("\n__________________________");
printf("\naverage waiting time=%d",sum3);
printf("\n__________________________");
}

Output:
enter the number of processes:3
enter burst time for each process:
enter the process[0]:30
enter the process[1]:6
enter the process[2]:8
enter the timeslice for each process:
5
5
5
process burst time timeslice
0 1 1
1 2 2
2 3 3
3 4 4
4 5 5
average turn around time=7
waiting time of p[0]=0
waiting time of p[1]=1
waiting time of p[2]=3
waiting time of p[3]=6
waiting time of p[4]=10
__________________________
average waiting time=4

b) SJF:
Algorithm:
1. Start the process
2. Declare the array
3. Declare number of processes
4. Input the processes along with burst time
5. Sort the burst time in increasing order
6. For each and every process calculate turnaround time and waiting time
7. Finally, calculate total waiting time and total turnaround time and display the
results.

Program:
#include<stdio.h>
void main()
{
//SJFS
int p[10],cou[10],k=0,i,j,n,min,wt=0,tat=0,temp;
printf("Enter no.of processes:");
scanf("%d",&n);
printf("\nEnter the processes burst time : ");
for(i=0;i<n;i++)
{
printf("\nEnter process %d time:",i);
scanf("%d",&p[i]);
}
printf("processes and their bt:\n");
for(i=0;i<n;i++)
{
printf("%d\t",p[i]);
cou[i]=i;
}
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(p[min]>=p[j])
{

min=j;
}
}
if(i!=min)
temp=p[min],
p[min]=p[i],
p[i]=temp;
temp=cou[min],
cou[min]=cou[i],
cou[i]=temp;
}
printf("\nProcess\tBursttime\twait time \tTAtime\n");
for(i=0;i<n;i++)
{
tat=tat+p[i];
printf("%d \t%d \t%d \t %d\n",cou[i]+1,p[i],wt,tat);
wt=wt+p[i];
}
}

Output:
Enter no.of processes:
Enter the processes burst time :
5 24 16 10 3
Process Bursttime wait time TAtime
5 3 0 3
1 5 3 8
4 10 8 18
3 16 18 34
2 24 34 58

c) FCFS (first come first serve):


Algorithm:
1. Start the process
2. Declare the array
3. Declare number of processes
4. Input the processes along with burst time
5. For each and every process calculate turnaround time and waiting time
6. Finally, calculate total waiting time and total turnaround time
7. Display the results.

Program:
#include<stdio.h>
int main()
{
int bt[10], wt[10], tat[10], rt[10], gt[10], i, n;
float wt_sum=0, tat_sum=0, rt_sum=0;
float total_wt, total_tat, total_rt;
printf("\nEnter the number of processes required : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\nEnter Burst Time for Process P%d : ", i);
scanf("%d", &bt[i]);
}
gt[0]=0;
for(i=1;i<=n;i++)
{
gt[i]= gt[i-1]+bt[i-1];
tat[i-1]=gt[i]-gt[0];
tat_sum = tat_sum + tat[i-1];
wt[i-1]=gt[i-1]-gt[0];
wt_sum = wt_sum + wt[i-1];
rt[i-1]=gt[i-1]-gt[0];
rt_sum = rt_sum + rt[i-1];
}
printf("\nProcess Burst_time Turnaround_time Waiting_time
Response_time");
for(i=0;i<n;i++)

{
printf("\nP%d\t\t%d\t\t%d\t\t%d\t\t%d",i,bt[i],tat[i],wt[i],rt[i]);
}
total_tat = tat_sum / n;
total_wt = wt_sum / n;
total_rt = rt_sum / n;
printf("\nTotal turnaround time : %f",total_tat);
printf("\nTotal waiting time : %f",total_wt);
printf("\nTotal response time : %f",total_rt);
}

OUTPUT:-
Enter the number of processes required : 5
Enter Burst Time for Process P0 : 5
Enter Burst Time for Process P1 : 24
Enter Burst Time for Process P2 : 16
Enter Burst Time for Process P3 : 10
Enter Burst Time for Process P4 : 3
Process Burst_time Turnaround_time Waiting_time Response_time
P0 5 5 0 0
P1 24 29 5 5
P2 16 45 29 29
P3 10 55 45 45
P4 3 58 55 55
Total turnaround time : 38.400002
Total waiting time : 26.799999
Total response time : 26.799999

d) Priority algorithm:
Algorithm:
1. Start the process
2. Declare the array
3. Declare number of processes
4. Input the processes along with their burst time and priority
5. Sort the priority, burst time, processes according to their priority
6. If any two processes has the same priority then break them using FCFS technique
7. For each and every process calculate turnaround time and waiting time
8. Finally, calculate total waiting time and total turnaround time
9. Display the results.
10.Stop the process.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int bt[10],tt[10],wt[10],rt[10],p,pt[10],i,j,temp;
float tttotal=0,wttotal=0,rttotal=0;
clrscr();
scanf("%d",&p);
printf("Enter the number of processes : %d",p);
for(i=0;i<p;i++)
{
scanf("%d",&bt[i]);
printf("\nEnter Burst Time of process %d = %d",i,bt[i]);
scanf("%d",&pt[i]);
printf("\nEnter priority of the process %d =%d",i,pt[i]);
}
for(i=0;i<p;i++)
{
for(j=i+1;j<p;j++)
{
if(pt[i]>pt[j])
{
pt[i]=pt[i]+pt[j]-(pt[j]=pt[i]);

bt[i]=bt[i]+bt[j]-(bt[j]=bt[i]);
}
}
}
tt[0]=bt[0];
printf("\nTurnaround time for process 0 = %d",tt[0]);
for(i=1;i<p;i++)
{
tt[i]= tt[i-1]+bt[i];
printf("\nTurnaround time for process %d = %d ",i,tt[i]);
tttotal=tttotal+tt[i];
}
printf("\nTotal Turnaround time = %f milli sec",(tttotal/p));
wt[0]=0;
printf("\nWaiting time for process 0 = %d",wt[0]);
for(i=1;i<p;i++)
{
wt[i]= wt[i-1]+bt[i-1];
printf("\nWaiting time for process %d = %d",i,wt[i]);
wttotal=wttotal+wt[i];
}
printf("\nTotal Waiting time = %f milli sec",(wttotal/p));
rt[0]=0;
printf("\nResponse time for process 0 = %d",rt[0]);
for(i=1;i<p;i++)
{
rt[i]= rt[i-1]+bt[i-1];
printf("\nResponse time for process %d = %d",i,rt[i]);
rttotal=rttotal+rt[i];
}
printf("\nTotal Response time = %f milli sec",(rttotal/p));
getch();
}

OUTPUT :
Enter the number of processes : 5
Enter Burst Time of process 0 = 10
Enter priority of the process 0 =3
Enter Burst Time of process 1 = 1
Enter priority of the process 1 =1
Enter Burst Time of process 2 = 2
Enter priority of the process 2 =3
Enter Burst Time of process 3 = 1
Enter priority of the process 3 =4
Enter Burst Time of process 4 = 5
Enter priority of the process 4 =2
Turnaround time for process 0 = 1
Turnaround time for process 1 = 6
Turnaround time for process 2 = 8
Turnaround time for process 3 = 18
Turnaround time for process 4 = 19
Total Turnaround time = 10.200000 milli sec
Waiting time for process 0 = 0
Waiting time for process 1 = 1
Waiting time for process 2 = 6
Waiting time for process 3 = 8
Waiting time for process 4 = 18
Total Waiting time = 6.600000 milli sec
Response time for process 0 = 0
Response time for process 1 = 1
Total Response time = 6.600000 milli sec

13. Simulate the following disk scheduling algorithms algorithms.

A)LOOK B)CLOOK C)SCAN D)CSCAN E)FCFS

A)Look Algorithm:

Program:

#include<stdio.h>

//#include<conio.h>
void main()
{
int i,j,temp,max,nb,h,b[10],sum=0,diff;
float result;
//clrscr();
printf("Enter the number of blocks you need to cover:");
scanf("%d\n",&nb);
for(i=0;i<nb;i++)
{
printf("\n enter string %d:",i);
scanf("%d",&b[i]);
}
printf("\n");
for(i=0;i<nb;i++)
{
printf("%d\t",b[i]);
}
printf("\n enter the header value:");
scanf("%d",&h);
for(i=0;i<nb-1;i++)
{
for(j=0;j<nb-1-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}

}
}
printf("\n");
for(i = 0; i < nb; i++)
{
printf("%d\t",b[i]);
}
printf("\nstrings greater than header value are:");
for(i=0;i<nb;i++)
{
if(b[i]>h)
{
printf("%d\t",b[i]);
}
}
diff = b[nb-1]-h;
sum = sum + diff;

max=b[0];
for(i=1;i<nb;i++)
{
if(b[i]>max)
max=b[i];
}
printf("\n maximum element :%d",max);
printf("\nstrings lesser than header value are:");

for(i=nb-1;i>=0;i--)
{
if(b[i]<h)
{
printf("%d\t",b[i]);
}

}
diff = b[nb-1]-b[0];
sum = sum + diff;
result = (float)sum/nb;
printf("\n%f",result);
//getch();

Output:
Enter the number of blocks you need to cover:8

enter string 0:87


enter string 1:170
enter string 2:40
enter string 3:150
enter string 4:36
enter string 5:72
enter string 6:66
enter string 7:15
87 170 40 150 36 72 66 15
enter the header value:60
strings greater than header value are:66 72 87 150 170
maximum element :170
strings lesser than header value are:40 36 15
Enter the number of blocks you need to cover:8

87 170 40 150 36 72 66 15
enter the header value:60
strings greater than header value are:66 72 87 150 170
maximum element :170
strings lesser than header value are:40 36 15

B)Clook Algorithm:

Program
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,temp,max,nb,h,b[10],sum=0,diff,first;
float result;
clrscr();
printf("Enter the number of blocks you need to cover:");
scanf("%d",&nb);
for(i=0;i<nb;i++)
{
printf("\t enter string %d:",i);
scanf("%d",&b[i]);
}

for(i=0;i<nb;i++)
{
printf("%d\t",b[i]);
}
printf("\n enter the header value:");
scanf("%d",&h);

for(i=0;i<nb-1;i++)
{
for(j=0;j<nb-1-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}

}
}
printf("strings greater than header value are:");
for(i = 0; i < nb; i++)
{
printf("%d\t",b[i]);

}
for(i=0;i<nb;i++)
{
if(b[i]>h)
{
printf("--->%d\t",b[i]);
}
}
diff = b[nb-1]-h;
sum = sum + diff;

max=b[0];
for(i=0;i<nb;i++)
{
if(b[i]>max)
max=b[i];
}
printf("\n maximum element :%d",max);
printf("\nstrings lesser than header value are:");

for(i=0;i<nb;i++)
{
if(b[i]<h)
{
printf("--->%d\t",b[i]);
first = b[i];
}

}
diff = first - b[0];
sum = sum + diff;
diff = b[nb-1]-b[0];
sum = sum + diff;
result = (float)sum/nb;
printf("\n%f",result);
getch();
}

Input: 

Request sequence = {176, 79, 34, 60, 92, 11, 41, 114} 

Initial head position = 50 


Direction = right (Moving from left to right) 

Output: 

Initial position of head: 50 

Total number of seek operations = 156 

Seek Sequence is 

60 

79 

92 

114 

176 

11 

34 

41 

C) Scan Algorithm:

Program:

#include<stdio.h>
#include<stdlib.h>
void main()
{
int
queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],temp1=0,te
mp2=0;
float avg;
scanf("%d",&max);
printf("Enter the max range of disk : %d\n",max);
scanf("%d",&head);
printf("Enter the initial head position : %d\n",head);
scanf("%d",&n);
printf("Enter the size of string : %d\n",n);
printf("Enter input request string values : ");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
printf("%d ",temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
temp=queue1[i];
queue1[i]=queue1[j];
queue1[j]=temp;
}

}
}

for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]<queue2[j])
{
temp=queue2[i];
queue2[i]=queue2[j];
queue2[j]=temp;
}
}
}
for(i=1,j=0;j<temp1;i++,j++)
queue[i]=queue1[j];
queue[i]=max;
for(i=temp1+2,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];
queue[0]=head;
for(j=0;j<=n;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek+=diff;
printf("\nDisk head moves from %d to %d with seek
%d",queue[j],queue[j+1],diff);
}
printf("\nTotal seek time is %d",seek);
avg=seek/(float)n;
printf("\nNumber of cylinders : %f",avg);
}

Input:
180
60
5
65
70
170
15
35

Input:
180
60
5
65
70
170
15
35
Output:
Enter the max range of disk : 180
Enter the initial head position : 60
Enter the size of string : 5
Enter input request string values : 65 70 170 15 35
Disk head moves from 70 to 170 with seek 100
Disk head moves from 170 to 180 with seek 10
Disk head moves from 180 to 35 with seek 145
Disk head moves from 35 to 15 with seek 20
Total seek time is 285
Number of cylinders : 57.000000

D)Csan

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
void main()
{
int
queue[20],n,head,i,j,k,seek=0,max,diff,temp,queue1[20],queue2[20],temp1=0,te
mp2=0;
float avg;
printf("Enter the max range of disk\n");
scanf("%d",&max);
printf("Enter the initial head position\n");
scanf("%d",&head);
printf("Enter the size of string\n");
scanf("%d",&n);
printf("Enter input request string values : \n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>=head)
{
queue1[temp1]=temp;
temp1++;
}
else
{
queue2[temp2]=temp;
temp2++;
}
}
for(i=0;i<temp1-1;i++)
{
for(j=i+1;j<temp1;j++)
{
if(queue1[i]>queue1[j])
{
queue1[i] = (queue1[i]+queue1[j]) - (queue1[j]=queue1[i]);
}
}
}
for(i=0;i<temp2-1;i++)
{
for(j=i+1;j<temp2;j++)
{
if(queue2[i]>queue2[j])
{
queue2[i] = (queue2[i]+queue2[j]) - (queue2[j]=queue2[i]);
}
}
}
queue[0]=head;
for(i=1,j=0 ; j<temp1 ; i++,j++)
queue[i]=queue1[j];

queue[i]=max;
queue[i+1]=0;

for(i=temp1+3,j=0;j<temp2;i++,j++)
queue[i]=queue2[j];

for(j=0;j<=n+1;j++)
{
diff=abs(queue[j+1]-queue[j]);
seek += diff;
}
printf("Total seek time is : %d\n",seek);
avg=seek/(float)n;
printf("Total number of cylinders : %f\n",avg);
}
OUTPUT:

Enter the max range of disk 180


Enter the initial head position 60
Enter the size of string 8
Enter input request string values :
87
170
40
150
36
72
66
15
Total seek time is : 340
Total number of cylinders : 42.500000
E)FCFS

PROGRAM:

#include<stdio.h>
int main()
{
int q[20],head,max,i,j,diff,n,seek=0;
float avg;
printf("Enter the maximum range of the disk\n");
scanf("%d",&max);
printf("Enter the size of the string\n");
scanf("%d",&n);
printf("Enter the string values\n");
for(i=1;i<=n;i++)
{
scanf("%d",&q[i]);
}
printf("Enter the head value\n");
scanf("%d",&head);
q[0]=head;
for(j=0;j<=n-1;j++)
{
diff=abs(q[j+1]-q[j]);
seek+=diff;
printf("Header moves from %d to %d with difference
%d\n",q[j],q[j+1],diff);
}
printf("total seek time is %d\n",seek);
avg=(float)seek/n;
printf("Average seek time is %f\n",avg);
}

Output:

Enter the maximum range of the disk:180


Enter the size of the string:6
Enter the string values:87,170,40,150,36,72
Enter the head value:60
Header moves from 60 to 87 with difference 27
Header moves from 87 to 170 with difference 83
Header moves from 170 to 40 with difference 130
Header moves from 40 to 150 with difference 110
Header moves from 150 to 36 with difference 114
Header moves from 36 to 72 with difference 36
total seek time is 500
Average seek time is 83.333336

You might also like