Sie sind auf Seite 1von 15

1.

Simulate the following CPU scheduling algorithms


a) FCFS b) SJF c) Priority d)Round Robin

FIRST COME FIRST SERVED


SOURCE CODE:
#include<stdio.h>
#include<string.h>
#include<conio.h>

main()

char pn[10][10],t[10];

int arrt[10],burt[10],strt[10],fint[10],tat[10],wt[10],i,j,n,temp;

int totwt=0,tottat=0;

clrscr();

printf("Enter the number of processes:");

scanf("%d",&n);

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

printf("Enter the Process Name,Arrival Time & Burst Time:");

scanf("%s%d%d",&pn[i],&arrt[i],&burt[i]);

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

{
for(j=0;j<n;j++)

if(arrt[i]<arrt[j])

temp=arrt[i];
arrt[i]=arrt[j];
arrt[j]=temp;
temp=burt[i];

burt[i]=burt[j];

burt[j]=temp;

strcpy(t,pn[i]);

strcpy(pn[i],pn[j]);

strcpy(pn[j],t);

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

printf("%s%d%d\n",pn[i],arrt[i],burt[i]);

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

if(i==0)

strt[i]=arrt[i];

else

strt[i]=fint[i-1];

wt[i]=strt[i]-arrt[i];

fint[i]=strt[i]+burt[i];

tat[i]=fint[i]-arrt[i];

printf("\nPName Arrtime Burstime Start WtTime Finish TAT");

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

printf("\n%s\t%2d\t%5d\t%5d\t%5d\t%5d\t%5d",pn[i],arrt[i],burt[i],strt[i],wt[i],fint[i],tat[i]);
totwt+=wt[i];

tottat+=tat[i];

printf("\nAverage Waiting time:%f",(float)totwt/n);


printf("\nAverage Turn Around Time:%f",(float)tottat/n);

getch();

return 0;

OUTPUT:
SHORTEST JOB FIRST

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()

char pn[10][10],t[10];

int arrt[10],exet[10],s,n,i,j,temp,strt[10],fint[10],wt[10],tat[10];

int totwt=0,tottat=0;

printf("Enter the number of Processes:");

scanf("%d",&n);

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

printf("Enter the Process Name,Execution Time:");

scanf("%s%d",pn[i],&exet[i]);

arrt[i]=0;

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

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

{
if(exet[i]<exet[j])

temp=exet[i];

exet[i]=exet[j];

exet[j]=temp;
strcpy(t,pn[i]);

strcpy(pn[i],pn[j]);

strcpy(pn[j],t);

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

if(i==0)
strt[i]=0;

else

strt[i]=fint[i-1];
wt[i]=strt[i]-arrt[i];

fint[i]=strt[i]+exet[i];

tat[i]=fint[i]-arrt[i];

printf("\nPname Execution Strttime Wttime Finttime Tatime");

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

printf("\n%s\t%3d\t\t%3d\t\t%3d\t\t%3d\t\t%3d",pn[i],exet[i],strt[i],wt[i],fint[i],tat[i]);
totwt+=wt[i];

tottat+=tat[i];

printf("\nAverage Waiting time is:%f",(float)totwt/n);

printf("\nAverage Turnaround Time is:%f",(float)tottat/n);

}
PRIORITY

SOURCE CODE:

#include<stdio.h>

#include<string.h>

void main()

int exet[20],arrt[10],n,i,j,temp,p[10],strt[10],fint[10],wt[10],tat[10];

int totwt=0,tottat=0;

float ata,awt;

char pn[10][10],t[10];

printf("Enter the number of processes:");

scanf("%d",&n);

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

printf("Enter Process name,Execution time & Priority:");

scanf("%s%d%d",pn[i],&exet[i],&p[i]);

arrt[i]=0;

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

if(p[i]<p[j])
{

temp=p[i];

p[i]=p[j];

p[j]=temp;

temp=exet[i];

exet[i]=exet[j];

exet[j]=temp;
strcpy(t,pn[i]);

strcpy(pn[i],pn[j]);

strcpy(pn[j],t);

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

if(i==0)

strt[i]=arrt[i];
else

strt[i]=fint[i-1];

wt[i]=strt[i]-arrt[i];

fint[i]=strt[i]+exet[i];

tat[i]=fint[i]-arrt[i];

printf("\nPname\tExetime\tPriority\tStrtme\tWttime\tFintime\tTatime");

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

printf("\n%s\t%5d\t%5d\t%5d\t%5d\t%5d\t%5d",pn[i],exet[i],p[i],strt[i],wt[i],fint[i],tat[i]);
totwt+=wt[i];

tottat+=tat[i];

printf("\nAverage Waiting time is:%f",(float)totwt/n);

printf("\nAverage TurnAroundTime is:%f",(float)tottat/n);

OUTPUT:
ROUND ROBIN

SOURCE CODE:

#include<stdio.h>

void main()

int exet[10],ts,n,i,tot=0,st[10],j,w1=0,tat[10],wt[10];

int tt=0,tw=0;

char pn[10][10];

float aw,at;

printf("Enter the no. of processes:");

scanf("%d",&n);

printf("Enter the time quantum:");

scanf("%d",&ts);

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

printf("Enter Processes name & Estimated time:");

scanf("%s%d",pn[i],&exet[i]);

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

st[i]=exet[i];

tot=tot+exet[i];
}

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

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

if(exet[i]>ts)

exet[i]=exet[i]-ts;

w1=w1+ts;

tat[i]=tat[i]-st[i];

printf("\n%s->%d",pn[i],ts);

else

if(exet[i]!=0)

if(exet[i]<ts)

w1=w1+exet[i];
tat[i]=w1;

wt[i]=tat[i]-st[i];

exet[i]=0;

printf("\n%s->%d",pn[i],st[i]);

else

w1=w1+ts;

tat[i]=w1;

wt[i]=tat[i]-st[i];
exet[i]=exet[i]-ts;

printf("\n%s->%d",pn[i],ts);

printf("\nProcess\tTsert\tWaitt\tTat\n");

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

printf("%s\t%d\t%d\t%d\n",pn[i],st[i],wt[i],tat[i]);

tw=tw+wt[i];

tt=tt+tat[i];

}
printf("Average Waiting Time=%f\n",(float)tw/n);

printf("Average Turn Around Time=%f\n",(float)tt/n);

printf("\nTotal Estimated Time:%d",tot);

OUTPUT 1:

OUTPUT 1:

Das könnte Ihnen auch gefallen