Friday, June 20, 2014

CPU SCHEDULING ALGORITHMS:


Simulate the following CPU scheduling algorithms :
a) Round Robin    b) SJF    c) FCFS     d) Priority
-------------------------------------------------------------------------------------------------------------------------
A) ROUND ROBIN:
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,wait,ts2,ta;
float nta;
}p[20];
main()
{
int i,j,slice,n;
float tamean=0,ntamean=0;
clrscr();
printf("Enter Number of Processes :: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c : ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for process-%c : ",65+i);
scanf("%d",&p[i].ts);
}
printf("\nEnter Time Slice: ");
scanf("%d",&slice);
for(i=0;i<n+1;i++)
{
if(i==0)
p[i].ts2=n*slice;
else
p[i].ts2=p[i-1].ts2+(p[i-1].ts-slice);
if(i<n)
p[i].st=i*slice;
if(i>=1)
p[i-1].ft=p[i].ts2;
}
for(i=0;i<n;i++)
p[i].wait=(i*slice-p[i].at)+(p[i].ts2-(i+1)*slice);
for(i=0;i<n;i++)
{
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)tamean/n;
ntamean=(float)ntamean/n;
printf("\n Process AT ST StT FT WT TA NTA\n");
for(i=0;i<n;i++)
{
printf("Process-%c%9d%9d%12d%12d%10d%6d%10.4f",65+i,p[i].at,p[i].ts,p[i].st,p[i].ft,p[i].wait,p[i].ta,p[i].nta);
printf("\n");
}
printf("\nturn around mean is : %f",tamean);
printf("\nnorm.turn around mean is : %f",ntamean);
getch();
}
OUTPUT:-
Image
---------------------------------------------------------------------------------------------------------------------------------------------------
b) SJF:-
#include<stdio.h>
#include<conio.h>
main()
{
int sbt[10],swt[10],st[10],stt[10],sft[10];
int n,i,j,wt,tt,temp;
float avgwt,avgtt;
wt=0;tt=0;temp=0;st[1]=0;
clrscr();
printf("enter no.of jobs");
scanf("%d",&n);
printf("enter the burst times of jobs");
for(i=1;i<=n;i++)
scanf("%d",&sbt[i]);
for(i=1;i<=(n-1);i++)
for(j=i+1;j<=n;j++)
if((sbt[i]>sbt[j])&&(sbt[i]!=sbt[j]))
{
temp=sbt[i];
sbt[i]=sbt[j];
sbt[j]=temp;
}
for(i=1;i<=n;i++)
{ st[i+1]=st[i]+sbt[i];
sft[i]=st[i]+sbt[i];
if(i==1)
swt[i]=0;
else
swt[i]=swt[i-1]+sbt[i-1];
stt[i]=swt[i]+sbt[i];
wt=wt+swt[i];
tt=tt+stt[i];
} avgwt=((float)wt/(float)n);
avgtt=((float)tt/(float)n);
printf("\nJOB sert st wt ft turt");
for(i=1;i<=n;i++)
printf("\nJ%d\t%d\t%d\t %d\t%d\t%d\n",i,sbt[i],st[i],sft[i],swt[i],stt[i]);
printf("\navg waiting time=%0.2f,turnover total time=%0.2f",avgwt,avgtt);
getch();
}
output:-
Image
------------------------------------------------------------------------------------------------------------------------------------------------------
c) FCFS:-
#include<stdio.h>
#include<conio.h>
struct process
{
int at,ts,st,ft,ta;
float nta;
};
main()
{
struct process p[20];
int n,i,j;
float tamean=0,ntamean=0;
clrscr();
printf("\nEnter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Arrival Time for Process-%c :: ",65+i);
scanf("%d",&p[i].at);
printf("\nEnter Service Time for Process-%c :: ",65+i);
scanf("%d",&p[i].ts);
}
for(i=0;i<n;i++)
{
if(i==0)
p[i].st=p[i].at;
else
{
p[i].st=0;
for(j=0;j<i;j++)
p[i].st=p[i].st+p[j].ts;
}
p[i].ft=p[i].ts+p[i].st;
p[i].ta=p[i].ft-p[i].at;
p[i].nta=(float)p[i].ta/p[i].ts;
tamean=tamean+p[i].ta;
ntamean=ntamean+p[i].nta;
}
tamean=(float)(tamean/n);
ntamean=(float)(ntamean/n);
printf("\nProcess AT ST StT FT TA NTA");
for(i=0;i<n;i++)
printf("\n%3c%12d%10d%10d%10d%10d%15f",65+i,p[i].at,p[i].ts, p[i].st,p[i].ft,p[i].ta,p[i].nta);
printf("\n\n Mean of Turn-around time : %f",tamean);
printf("\n\n Mean of Normalized turn-around time : %f",ntamean);
getch();
}
output:-
Image
-----------------------------------------------------------------------------------------------------------------------------------------------
d) Priority:-
#include<stdio.h>
#include<conio.h>
struct process
{
int ts,pri,wait,ft;
}p[20];
main()
{
int n,pri1[20],i,j,temp,ft1[25];
clrscr();
printf("\n Enter Number of Processes:: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter Service Time for Process-%c : ",65+i);
scanf("%d",&p[i].ts);
printf("\nEnter Priority for Process-%c : ",65+i);
scanf("%d",&p[i].pri);
}
for(i=0;i<n;i++)
pri1[i]=p[i].pri;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pri1[i]>pri1[j])
{
temp=pri1[i];
pri1[i]=pri1[j];
pri1[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(pri1[i]==p[j].pri)
{
if(i==0)
{
p[j].wait=0;
p[j].ft=p[j].ts;
ft1[i]=p[j].ft;
}
else
{
p[j].ft=ft1[i-1]+p[j].ts;
p[j].wait=ft1[i-1];
ft1[i]=p[j].ft;
}
}
}
}
printf("\nProcess ST PRI FT WT ");
for(i=0;i<n;i++)
{
printf("\nprocess-%c%10d%13d%14d%15d",65+i,p[i].ts,p[i].pri,p[i].ft,p[i].wait);
}
getch();
}
output:-
Image
----------------------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment