Beruflich Dokumente
Kultur Dokumente
BATCH :
ROLL NO :
********************************************************************************
***********************
AIM:Simulation of following CPU schedulling algorithm.
i)FCFS
ii)SJF
iii)Priority
********************************************************************************
**********************
Program:
#include<stdio.h>
char pname[10][20];
float b_time[10],wt_time[10],awt_time[10],tat_time[10],atat_time[10],t_time[10];
void sjf();
void fcfs();
void pri1();
void main()
{
int ch;
clrscr();
do
{
printf("\n\nSCHEDULLING ALGORITHM");
printf("\n1.FCFS\n2.SJF\n3.PRIORITY\n4.EXIT");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:fcfs();
break;
case 2:sjf();
break;
case 3:pri1();
break;
case 4:exit(0);
break;
}
}while(ch!=4);
}
void fcfs()
{
char pname[10][20];
float b_time[10],wt_time[10],awt_time,tat_time[10],atat_time[10],t_time[
10];
float s,k,t=0,bt=0;
int i,n;
printf("Enter no of processes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the process name");
scanf("%s",&pname[i]);
printf("Enter the burst time");
scanf("%f",&b_time[i]);
}
for(i=0;i<n;i++)
{
if(i==0)
{
wt_time[i]=0;
wt_time[i]=0;
}
else
{
wt_time[i]=wt_time[i-1]+b_time[i-1];
}
t_time[i]=wt_time[i]+b_time[i];
t+=t_time[i];
awt_time+=wt_time[i];
}
for(i=0;i<n;i++)
{
bt=bt+b_time[i];
}
k=t/n;
s=awt_time/n;
printf("\n\t\tGANTT CHART\t\t\n");
printf("\n________________________________________________________\n\n")
;
for(i=0;i<n;i++)
{
printf("|P[%d]",i+1);
printf("|\t\t");
}
printf("\n_________________________________________________\n\n"
);
for(i=0;i<n;i++)
{
printf("%f\t",wt_time[i]);
}
printf("%f",bt);
printf("\n\n\nProcess No.\tProcess name\tBurst time\tWaiting time\tTurn
around time\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%s\t\t%f\t%f\t%f\n",i+1,pname[i],b_time[i],wt_time
[i],t_time[i]);
}
printf("\n\nAverage waiting time=%f\n\nAverage turn around time=%f\n",s,
k);
}
void sjf()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i,q[10],bt=0;
float att=0,awt=0;
for(i=0;i<n;i++)
{
b[i]=0;
w[i]=0;
}
printf("Enter the no of processes");
scanf("%d",&n);
printf("Enter the burst time");
for(i=0;i<n;i++)
{
printf("process p%d",i+1);
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for(i=0;i<n;i++)
{
w[i+1]=w[i]+b[i];
}
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
q[i]=t[i];
}
for(i=0;i<n;i++)
{
awt=awt+w[i];
att=att+t[i];
}
for(i=0;i<n;i++)
{
bt=bt+b[i];
}
awt=awt/n;
att=att/n;
printf("\n\t\tGANTT CHART\t\t\n");
printf("\n_______________________________________________________\n\n");
for(i=0;i<n;i++)
{
printf("|P[%d]",i+1);
printf("|\t\t");
}
printf("\n______________________________________________________
_\n\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",w[i]);
}
printf("%d",bt);
printf("\nProcess\tWaiting time\tTurn around time\n");
for(i=0;i<n;i++)
{
printf("\tp[%d]\t\t%d\t\t%d\n",i,w[i],q[i]);
}
printf("\n\nAverage Witing time=%f\n\nAverage Turn around time=%f",awt,a
tt);
}
void pri1()
{
int x,n,p[10],pp[10],pt[10],w[10],t[10],awt,atat,i,j,bt=0;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("\n Enter process : time priorities \n");
for(i=0;i<n;i++)
{
printf("\nProcess no %d : ",i+1);
scanf("%d %d",&pt[i],&pp[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
bt=bt+pt[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pp[i]<pp[j])
{
x=pp[i];
pp[i]=pp[j];
pp[j]=x;
x=pt[i];
pt[i]=pt[j];
pt[j]=x;
x=p[i];
p[i]=p[j];
p[j]=x;
}
}
}
w[0]=0;
awt=0;
t[0]=pt[0];
atat=t[0];
for(i=1;i<n;i++)
{
w[i]=w[i-1]+pt[i-1];
awt+=w[i];
t[i]=w[i]+pt[i];
atat+=t[i];
}
printf("\n\t\tGANTT CHART\t\t\n");
printf("\n_______________________________________________________\n\n");
for(i=0;i<n;i++)
{
printf("|P[%d]",i+1);
printf("|\t\t");
}
printf("\n______________________________________________________
_\n\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",w[i]);
}
printf("%d",bt);
printf("\n\n Job \t Burst Time \t Wait Time \t Turn Around Time Priority \n");
for(i=0;i<n;i++)
{
printf("\n %d \t\t %d \t\t %d \t\t %d \t\t %d \n",p[i],pt[i],w[i],t[i],pp[i]);
}
awt/=n;
atat/=n;
printf("\n Average Wait Time : %d \n",awt);
printf("\n Average Turn Around Time : %d \n",atat);
}
********************************************************************************
*************************************
OUTPUT:
SCHEDULLING ALGORITHM
1.FCFS
2.SJF
3.PRIORITY
4.EXIT
Enter your choice:1
Enter no of processes3
Enter the process namep1
Enter the burst time24
Enter the process namep2
Enter the burst time3
Enter the process namep3
Enter the burst time3
GANTT CHART
________________________________________________________
|P[1]|
|P[2]|
|P[3]|
_________________________________________________
0.000000
24.000000
27.000000
30.000000
Process No.
Process name
Burst time
Waiting time
1
2
3
p1
p2
p3
24.000000
3.000000
3.000000
0.000000
24.000000
27.000000
24.000000
27.000000
30.000000
1.FCFS
2.SJF
3.PRIORITY
4.EXIT
Enter your choice:2
Enter the no of processes3
Enter the burst timeprocess p124
process p23
process p33
GANTT CHART
_______________________________________________________
|P[1]|
|P[2]|
|P[3]|
_______________________________________________________
0
3
Process Waiting time
p[0]
p[1]
p[2]
6
Turn around time
0
3
3
6
6
30
30
3
Job
27
Burst Time
Wait Time
30
Turn Around Time
Priority
24
27
27
30