OS LAB Task 1
OS LAB Task 1
SCI LAB
V.PADMA
Associate Professor
IT Dept.
Course Outcomes:
0 24 27 33
0 24 27 33
P2 P3 P1
0 3 9 33
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest
time
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,p[10],bt[10],w[10],t[10],wt=0,tat=0,t1,t2;
float awt,atat;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(bt[i]>bt[j])
{
t1=bt[i];
bt[i]=bt[j];
bt[j]=t1;
t2=p[i];
p[i]=p[j];
p[j]=t2;
}
}
}
w[0]=0;
for(i=1;i<n;i++)
w[i]=w[i-1]+bt[i-1];
for(i=0;i<n;i++)
wt=wt+w[i];
awt=(float)(wt)/n;
t[0]=bt[0];
for(i=1;i<n;i++)
t[i]=w[i]+bt[i];
for(i=0;i<n;i++)
tat=tat+t[i];
atat=(float)(tat)/n;
printf("\n GANTT CHART \n");
for(i=0;i<n;i++)
{
printf("p[%d]\t",p[i]);
}
printf(" \n SHORTEST JOB FIRST IS \n");
printf("processid\tbursttime\twaitingtime\tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\n",p[i],bt[i],w[i],t[i]);
}
printf("average waiting time is %f\n",awt);
printf("average turnaroundtime is %f\n",atat);
getch();
}
OUTPUT:
3. Priority Scheduling
0 1 6 16 18 19
w[0]=0;
for(i=1;i<n;i++)
w[i]=w[i-1]+bt[i-1];
for(i=0;i<n;i++)
wt=wt+w[i];
awt=(float)(wt)/n;
t[0]=bt[0];
for(i=1;i<n;i++)
t[i]=w[i]+bt[i];
for(i=0;i<n;i++)
tat=tat+t[i];
atat=(float)(tat)/n;
printf("\n GANTT CHART \n");
for(i=0;i<n;i++)
{
printf("p[%d]\t",p[i]);
}
printf(" \n PRIORITY IS \n");
printf("processid\tbursttime\tpriority\twaitingtime\
tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("p[%d]\t\t%d\t\t%d\t\t%d\t\t%d\t\
n",p[i],bt[i],pr[i],w[i],t[i]);
}
printf("average waiting time is %f\n",awt);
printf("average turnaroundtime is %f\n",atat);
getch();
}
OUTPUT:
4. Round Robin (RR)
Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds.
After this time has elapsed, the process is preempted and
added to the end of the ready queue.
If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU
time in chunks of at most q time units at once.
Performance
q large FIFO
q small q must be large with respect to context
switch, otherwise overhead is too high
Example of RR with Time Quantum = 20
Process Burst Time
P1 53 -> 20+20+13
P2 17 -> 20
P3 68 -> 20+20+20+8
P4 24
The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
// RoundRobinProgram:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,p[10],bt[10],cbt[10],i,sum=0,ts,temp=0,tat[10],wt[10];
float awt=0,atat=0;
clrscr();
printf("enter the no of processes:");
scanf("%d",&n);
printf("enter the process id's:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("enter the burst time of the processes:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
cbt[i]=bt[i];
}
printf("enter the time stamp:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
sum=sum+bt[i];
}
while(sum>0)
{
for(i=0;i<n;i++)
{
if(bt[i]>ts)
{
temp=temp+ts;
tat[i]=temp;
bt[i]=bt[i]-ts;
sum=sum-ts;
printf("p[%d]\t",p[i]);
}
else if(bt[i]<=ts&&bt[i]>0)
{
temp=temp+bt[i];
tat[i]=temp;
sum=sum-bt[i];
bt[i]=0;
printf("p[%d]\t",p[i]);
}
else
continue;
}
}
for(i=0;i<n;i++)
wt[i]=tat[i]-cbt[i];
printf("\nprocessid\tbursttime\twaitingtime\
tturnaroundtime\n");
for(i=0;i<n;i++)
{
printf("\np[%d]\t\t%d\t\t%d\t\t%d\
n",p[i],cbt[i],wt[i],tat[i]);
awt=awt+wt[i];
atat=atat+tat[i];
}
printf("\n average waiting time is %f\n",awt/n);
printf("\n average turn around time is %f\n",atat/n);
getch();
}
OUTPUT: