Monday, December 26, 2011

Exercise Program - 4.b IMPLEMENATION OF SJF SCHEDULING ALGORITHM

AIM:

To write a C program for implementing the shortest job first scheduling algorithm.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the variable.

Step 3: Read the value of n.

Step 4: Scan the value of p[i] for i=1 to i

Step 5: Read the value of at and print the value of at.

Step 6: Do step 7 for i=1 to i

Step 7: Do step 8 for j=j+1 to j<=n and increment j value by 1.

Step 8: if p[i]>p[j], then assign t=p[j] and p[j]=p[i],then p[i]=t.

Step 9: Assign w[j]=at,at=at+p[i].

Step 10: Print the value at.

Step 11: Do step 8 for i=1,i<=n and increment value of i by 1.

Step 12: Assign sum=sum+w[i] and print the value of w[i].

Step 13: Assign avg=sum/n then print the value of avg.

Step 14: Print the value of i and p[i],w[i].

Step 15: Assign avg1=sum1/n and print the value of avg1.

Step 16: Stop the program.

PROGRAM:

#include

#include

typedef struct pcb

{

int pid, bt, bt_bal, tat, wt;

} PCB;

int main()

{

PCB pr[10], temp;

int i,j,k,n,tq;

int sum_bt=0,sum_tat=0,sum_wt=0;

int gantt[3][50];

printf("\n\nSJF Scheduling \n");

printf("\nEnter the no. of process : ");

scanf("%d",&n);

for(i=0;i

{

printf("\nEnter burst time of the process %d: ",i+1);

pr[i].pid = i+1;

scanf("%d", &pr[i].bt);

}

for( i=0; i

for(j=i+1; j

if( pr[i].bt > pr[j].bt )

{

temp = pr[i];

pr[i] = pr[j];

pr[j] = temp;

}

k=0;

for( i=0;i

{

sum_bt += pr[i].bt;

pr[i].tat = sum_bt;

pr[i].wt = pr[i].tat - pr[i].bt;

sum_wt += pr[i].wt;

sum_tat += pr[i].tat;

gantt[0][k] = pr[i].pid;

gantt[1][k] = sum_bt;

gantt[2][k] = sum_bt - pr[i].bt;

k++;

}

printf("\n\nSJF Scheduling \n");

printf("PID: ");

for( i=0; i

printf("%4d",gantt[0][i]);

printf("\nBTime:");

for( i=0; i

printf("%4d",pr[i].bt);

printf("\n\nGANTT Chart\n\n");

printf("PID: ");

for( i=0; i

printf("%4d",gantt[0][i]);

printf("\nTime: ");

for( i=0; i

printf("%4d",gantt[1][i]);

printf("\n\nWTime:");

for( i=0; i

printf("%4d",gantt[2][i]);

printf("\n\nTotal waiting time = %d\n",sum_wt);

printf("Average waiting time = %.2f\n",(float)sum_wt/n);

printf("\nTotal turn around time = %d\n",sum_tat);

printf("Average turn around time = %.2f\n\n",(float)sum_tat/n);

return 0;

}

SAMPLE OUTPUT:

[it65@AntiViruS ~]$ cc sjf.c

[it65@AntiViruS ~]$ ./a.out

SJF Scheduling

Enter the no. of process : 4

Enter burst time of the process 1: 7

Enter burst time of the process 2: 4

Enter burst time of the process 3: 1

Enter burst time of the process 4: 4

SJF Scheduling

PID: 3 2 4 1

BTime: 1 4 4 7

GANTT Chart

PID: 3 2 4 1

Time: 1 5 9 16

WTime: 0 1 5 9

Total waiting time = 15

Average waiting time = 3.75

Total turn around time = 31

Average turn around time = 7.75


RESULT:

Thus a C program for implementing the shortest job first scheduling algorithm has been executed and the output is verified successfully.

2 comments:

TECHFriendz said...

Good program,but if you want to see output
window try this one
TechFriendz

Anonymous said...

sir,


will u please help me to know the conditions in each for loop