Sunday 1 November 2015

HRRN CPU Scheduling - C Program - Operating Systems

#include<stdio.h>
struct process
{
char name;
int at,bt,ct,wt,tt;
int completed;
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;
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 and burst time of process%c: ",p[i].name);
 scanf("%d%d",&p[i].at,&p[i].bt);
 p[i].completed=0;
 sum_bt+=p[i].bt;

}
sortByArrival();

printf("\nName\tArrival Time\tBurst Time\tWaiting Time\tTurnAround Time\t Normalized TT");
  for(time=p[0].at;time<sum_bt;)
  {
 
   float hrr=-9999;
   int loc;
  for(i=0;i<n;i++)
  {
 
   if(p[i].at<=time && p[i].completed!=1)
            {
              float temp=(p[i].bt + (time-p[i].at))/p[i].bt;
              if(hrr < temp)
               {
                hrr=temp;
                loc=i;
               }
         
   }
   }
 
 
   time+=p[loc].bt;
   p[loc].wt=time-p[loc].at-p[loc].bt;
   p[loc].tt=time-p[loc].at;
   p[loc].ntt=((float)p[loc].tt/p[loc].bt);
   p[loc].completed=1;
   avgwt+=p[loc].wt;
printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d\t\t%f",p[loc].name,p[loc].at,p[loc].bt,p[loc].wt,p[loc].tt,p[loc].ntt);
  }

printf("\nAverage waiting time:%f\n",avgwt/n);
}

OUTPUT:

Enter no of processes:5

Enter the arrival time and burst time of processA: 0 3

Enter the arrival time and burst time of processB: 2 6

Enter the arrival time and burst time of processC: 4 4

Enter the arrival time and burst time of processD: 6 5

Enter the arrival time and burst 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
C               4                        4                             5               9                             2.250000
E               8                        2                             5               7                             3.500000
D               6                       5                              9               14                          2.800000
Average waiting time:4.000000



3 comments:

  1. Hi
    Can you write spicial case for this algorthim? Plzz

    ReplyDelete
  2. how we convert this algorithm into java???

    ReplyDelete
  3. can you please write it in arrays..

    ReplyDelete