Lab 8
Lab 8
P1 6 ms 2 ms
P2 2 ms 5 ms
P3 8 ms 1 ms
P4 3 ms 0 ms
P5 4 ms 4 ms
The Shortest Job First CPU Scheduling Algorithm will work on the basis of
steps as mentioned below:
At time = 0,
Process P4 arrives and starts executing
Initia
l
Time Waitin Burs Remainin
Instanc Proces Arriva g Executio t g Burst
e s l Time Table n Time Time Time
At time= 1,
Process P3 arrives.
But, as P4 still needs 2 execution units to complete.
Thus, P3 will wait till P4 gets executed.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time =2,
Process P1 arrives and is added to the waiting table
P4 will continue its execution.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time = 3,
Process P4 will finish its execution.
Then, the burst time of P3 and P1 is compared.
Process P1 is executed because its burst time is less as compared
to P3.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time = 4,
Process P5 arrives and is added to the waiting Table.
P1 will continue execution.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time = 5,
Process P2 arrives and is added to the waiting Table.
P1 will continue execution.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
5-6ms
P5 4ms P3, P5 0ms 4ms 4ms
P3, P5,
P2 5ms 0ms 2ms 2ms
P2
At time = 6,
Process P1 will finish its execution.
The burst time of P3, P5, and P2 is compared.
Process P2 is executed because its burst time is the lowest among
all.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
6-9ms
P5 4ms P3, P5 0ms 4ms 4ms
P3, P5,
P2 5ms 0ms 2ms 2ms
P2
At time=9,
Process P2 is executing and P3 and P5 are in the waiting Table.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time = 11,
The execution of Process P2 will be done.
The burst time of P3 and P5 is compared.
Process P5 is executed because its burst time is lower than P3.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
At time = 15,
Process P5 will finish its execution.
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
15-
P3 1ms 8ms 8ms 0ms
23ms
At time = 23,
Process P3 will finish its execution.
The overall execution of the processes will be as shown below:
Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time
P3, P5,
P2 5ms 0ms 2ms 2ms
P2
6-9ms
P5 4ms P3, P5 0ms 4ms 4ms
P3, P5,
P2 5ms 0ms 2ms 2ms
P2
15-
P3 1ms 8ms 8ms 0ms
23ms
Gantt chart for above execution:
Gantt chart
Now, let’s calculate the average waiting time for above example:
P4 = 0 – 0 = 0
P1 = 3 – 2 = 1
P2 = 9 – 5 = 4
P5 = 11 – 4 = 7
P3 = 15 – 1 = 14
Average Waiting Time = 0 + 1 + 4 + 7 + 14/5 = 26/5 = 5.2
#include <stdio.h>
int main()
{
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int A[100][4];
int i, j, n, total = 0, index, temp;
float avg_wt, avg_tat;
printf("Enter number of process: ");
scanf("%d", &n);
printf("Enter Burst Time:\n");
// User Input Burst Time and alloting Process Id.
for (i = 0; i < n; i++) {
printf("P%d: ", i + 1);
scanf("%d", &A[i][1]);
A[i][0] = i + 1;
}
// Sorting process according to their Burst Time.
for (i = 0; i < n; i++) {
index = i;
for (j = i + 1; j < n; j++)
if (A[j][1] < A[index][1])
index = j;
temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
// Calculation of Turn Around Time and printing the
// data.
for (i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],
A[i][1], A[i][2], A[i][3]);
}
avg_tat = (float)total / n;
printf("Average Waiting Time= %f", avg_wt);
printf("\nAverage Turnaround Time= %f", avg_tat);
}
Or
Problem Description:
Write an SJF scheduling program in C to determine the average waiting time and
average turnaround time given n processes and their burst times.
P1 0 5
P2 0 4
P3 0 12
P4 0 7
Gantt Chart:
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
P1 waiting time: 4
P2 waiting time: 0
P3 waiting time: 16
P4 waiting time: 9
1. /*
2. * C Program to Implement SJF Scheduling
3. */
4.
5. #include<stdio.h>
6. int main()
7. {
8. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
9. float avg_wt,avg_tat;
10. printf("Enter number of process:");
11. scanf("%d",&n);
12.
13. printf("\nEnter Burst Time:\n");
14. for(i=0;i<n;i++)
15. {
16. printf("p%d:",i+1);
17. scanf("%d",&bt[i]);
18. p[i]=i+1;
19. }
20.
21. //sorting of burst times
22. for(i=0;i<n;i++)
23. {
24. pos=i;
25. for(j=i+1;j<n;j++)
26. {
27. if(bt[j]<bt[pos])
28. pos=j;
29. }
30.
31. temp=bt[i];
32. bt[i]=bt[pos];
33. bt[pos]=temp;
34.
35. temp=p[i];
36. p[i]=p[pos];
37. p[pos]=temp;
38. }
39.
40. wt[0]=0;
41.
42. //finding the waiting time of all the processes
43. for(i=1;i<n;i++)
44. {
45. wt[i]=0;
46. for(j=0;j<i;j++)
47. //individual WT by adding BT of all previous completed processes
48. wt[i]+=bt[j];
49.
50. //total waiting time
51. total+=wt[i];
52. }
53.
54. //average waiting time
55. avg_wt=(float)total/n;
56.
57. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
58. for(i=0;i<n;i++)
59. {
60. //turnaround time of individual processes
61. tat[i]=bt[i]+wt[i];
62.
63. //total turnaround time
64. totalT+=tat[i];
65. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
66. }
67.
68. //average turnaround time
69. avg_tat=(float)totalT/n;
70. printf("\n\nAverage Waiting Time=%f",avg_wt);
71. printf("\nAverage Turnaround Time=%f",avg_tat);
72. }
Program Explanation
1. Initialize two array pid[] and bt[] of size 20.
2. Ask the user for number of processes n.
3. Ask the user for process id and burst time for all n processes and store them
into pid[] and bt[] respectively.
4. Sort all the processes according to their burst time.
5. Assign waiting time = 0 to the smallest process.
6. Calculate waiting time of each process by using two loops and adding all the
burst time of previously completed processes.
7. Print Process Id, Burst Time, waiting time and Turnaround time of each
process in tabular manner.
8. Calculate and print turnaround time of each process = bt[i] + wt[i].
9. Add waiting time of all the processes and store it in the variable total.
10. Add turnaround time of all the processes and store it in the variable totalT.
11. Calculate average waiting time as avg_wt = total/n.
12. Calculate average turnaround time as avg_tat = totalT/n;
13. Print average waiting time and average turnaround time.
14. Exit.
gedit program.c
/ * C Program to Implement SJF Scheduling */
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
Output:
Enter number of process:4
Enter Burst Time:
p1:5
p2:4
p3:12
p4:7
Process Burst Time Waiting Time Turnaround Time
p2 4 0 4
p1 5 4 9
p4 7 9 16
p3 12 16 28