You are on page 1of 3

Program One

Objective:- Write a program in C/C++ to calculate average waiting


time in non primitive SJF CPU scheduling.

Theory and concept:- Shortest job first (SJF) scheduling is a policy, the
process with the shortest expected processing time is selected for the
execution. If two processes have same expected processing time, then at
FCFS scheduling is used to break tie.

Program :

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

struct process
{
int pid ;
int burst ;
int wait ;
};
void main()
{
process p[5] , temp ;
int i,j,wait=0 ;
int twait=0.0 ;
for(i=0;i<5;i++)
{
p[i].pid = i+1 ;
printf("\nprocess p%d\n",i+1) ;
printf("\nenter burst time\n");
scanf("%d",&p[i].burst);
}
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
{
if(p[j].burst>p[i].burst)
{
temp=p[i];
p[i]=p[j] ;
p[j]=temp;
}
}
}
printf("\nprocess\t\tburst time\t\twaiting time\n");
for(i=0;i<5;i++)
{
p[i].wait=wait ;
twait = twait+wait ;
wait = wait + p[i].burst ;
printf("\np%d\t\t%d\t\t%d\n",p[i].pid,p[i].burst,p[i].wait);
}
printf("\n\n average waiting time %d",(twait/5));
getch();
}

Output:

Process p1

enter burst time


3

Process p2

enter burst time


4

Process p3

enter burst time


1
Process p4

enter burst time


7

Process p5

enter burst time


8

Process p6

enter burst time


3

process burst time waiting time

p2 1 0

p0 3 1

p1 4 4

p3 7 8

p4 8 15

average waiting time 5.6

You might also like