Sunday 1 November 2015

Shortest Job First - CPU Scheduling - Operating Systems

#include<stdio.h>
struct process
{
char name;
int at,bt,ct,wt,tt;
int processed;
float ntt;
}p[10];
int n;

void sortByArrival()
{
struct process temp;
int i,j;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
void main()
{
int i,j,time,sum_bt=0,smallest;
char c;
        float avgwt=0;
 printf("Enter no of processes:");
 scanf("%d",&n);
 for(i=0,c='A';i<n;i++,c++)
 {
 p[i].name=c;
 printf("\nEnter the arrival time of process%c: ",p[i].name);
 scanf("%d",&p[i].at);
 printf("\nEnter the burst time of process%c: ",p[i].name);
 scanf("%d",&p[i].bt);
 p[i].processed=0;
 sum_bt+=p[i].bt;

}
sortByArrival();
p[9].bt=9999;
printf("\nName\tArrival Time\tBurst Time\tWaiting Time\tTurnAround Time\t Normalized TT");
  for(time=p[0].at;time<sum_bt;)
  {
    smallest=9;
    for(i=0;i<n;i++)
    {
      if(p[i].at<=time && p[i].processed!=1 && p[i].bt<p[smallest].bt)
        smallest=i;
    }
      time+=p[smallest].bt;
 p[smallest].ct=time;
          p[smallest].wt=time-p[smallest].at-p[smallest].bt;
     p[smallest].tt=p[smallest].wt+p[smallest].bt;
     p[smallest].ntt=((float)p[smallest].tt/p[smallest].bt);
    p[smallest].processed=1;
    avgwt+=p[smallest].wt;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d\t\t%f",p[smallest].name,p[smallest].at,p[smallest].bt,p[smallest].wt,p[smallest].tt,p[smallest].ntt);
}
printf("\nAverage waiting time:%f\n",avgwt/n);
}




OUTPUT:


Enter no of processes:5

Enter the arrival time of processA: 0 3

Enter the arrival time of processB: 2 6

Enter the arrival time of processC: 4 4

Enter the arrival time of processD: 6 5

Enter the arrival time of processE: 8 2

Name    Arrival Time    Burst Time      Waiting Time    TurnAround Time  Normalized TT
A                0                            3                        0               3                   1.000000
B                2                            6                        1               7                   1.166667
E                8                            2                        1               3                   1.500000
C               4                            4                        7               11                  2.750000
D               6                            5                        9               14                  2.800000
Average waiting time:3.600000

No comments:

Post a Comment