Exp4 B

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

EX.NO : 4.

b) CPU SCHEDULING ALGORITHMS


Shortest Job First
DATE :

AIM:
To write a program to implement the concept of Shortest Job First.

ALGORITHM:

STEP 1: Inside the structure declare the variables.

STEP 2: Declare the variable i, j as integer, totwtime and totttime is equal to zero.

STEP 3: Get the value of ‘n’, assign pid as I and get the value of p[i].btime.
STEP 4: Assign p[0] wtime as zero and tot time as btime and inside the loop calculate wait
time and turn around time.

STEP 5: Calculate total wait time and total turn around time by dividing by total number of
process.

STEP 6: Print total wait time and total turn around time.
STEP 7: Stop the program.
PROGRAM:
#include<stdio.h>

#include<stdlib.h>
typedef struct{
int pid, btime, wtime;
}sp;
int main(){
int i, j, n, tbm=0, towtwtime=0, totttime=0;
sp* p,t;
printf(“\n*****SJF Scheduling*****\n”);
printf(“Enter the No.of Processes : “);
scanf(“%d”, &n);
p = (sp*)malloc(sizeof(sp));
printf(“\nEnter the Burst Time : “);
SAMPLE INPUT AND OUTPUT:
*****SJF Scheduling*****
Enter No.of Process : 4
Enter the Burst Time:
Process 1 :4
Process 2 :6
Process 3 :8
Process 4 : 10
Process Scheduling…
Process Burst Time Waiting Time Turn Around Time
4 10 0 10
3 8 10 18
2 6 18 24
1 4 24 28
Total Waiting Time : 52
Average Waiting Time : 13.000000
Total Turn Around Time : 80
Average Turn Around Time : 20.000000
for( i=0; i<n; i++){
printf(“\nProcess %d:\t”, i+1);
scanf(“%d”, &p[i].btime);
p[i].pid = i+1;
p[i].wtime = 0;
}
for(i=0; i<n; i++){
for(j=0; j<n; j++){
if( p[i].btime > p[j].btime){
t = p[i];
p[i] = p[j];
p[j] = t;
}
}
}
printf(“\nProcess\tBrust Time\tWaiting Time\tTurn Around Time\n”);
for(i=0; i<n; i++){
towtwtime+= p[i].wtime= tbm;
tbm+= p[i].btime;
printf(“\n%d\t\t%d”, p[i].pid, p[i].btime);
printf(“\t\t%d\t\t%d”, p[i].wtime, p[i].btime+p[i].wtime);
}
totttime = tbm + towtwtime;
printf(“\nTotal Waiting Time : %d”, towtwtime);
printf(“\nAverage Wating Time : %f”, (float)towtwtime/n);
printf(“\nTotal Turn Around Time : %d”, totttime);
printf(“\nAverage Turn Around Time : %f”, (float)totttime/n);
}

RESULT :
Thus the program was Executed and Verified Successfully.

You might also like