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

CPU SCHEDULING ALGORITHMS

1. To write a C program for implementation of Round Robin scheduling


algorithms.
2. To write a C program for implementation of SJF scheduling algorithms.

How to compute below times in SJF using a program?


 Completion Time: Time at which process completes its execution.
 Turn Around Time: Time Difference between completion time and
arrival time.
Turn Around Time = Completion Time – Arrival Time
 Waiting Time(W.T): Time Difference between turn around time and
burst time.
Waiting Time = Turn Around Time – Burst Time
 Burst Time:Burst time, also referred to as “execution time”.
It is the amount of CPU time the process requires to
complete its execution.
Examples to show working of Non-Preemptive Shortest Job
First CPU Scheduling Algorithm:
Example-1: Consider the following table of arrival time and burst time for five
processes P1, P2, P3, P4 and P5.
Process Burst Time Arrival Time

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

0-1ms P4 0ms 1ms 3ms 2ms

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

P4 0ms 1ms 2ms 1ms


1-2ms
P3 1ms P3 0ms 8ms 8ms

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

P4 0ms 1ms 1ms 0ms

2-3ms P3 1ms P3 0ms 8ms 8ms

P1 2ms P3, P1 0ms 6ms 6ms

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

P3 1ms P3 0ms 8ms 8ms


3-4ms
P1 2ms P3 1ms 6ms 5ms

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

P3 1ms P3 0ms 8ms 8ms

4-5ms P1 2ms P3 1ms 5ms 4ms

P5 4ms P3, P5 0ms 4ms 4ms

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

P3 1ms P3 0ms 8ms 8ms

P1 2ms P3 1ms 4ms 3ms

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

P3 1ms P3 0ms 8ms 8ms

P1 2ms P3 3ms 3ms 0ms

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

P3 1ms P3 0ms 8ms 8ms

9-11ms P5 4ms P3, P5 0ms 4ms 4ms

P2 5ms P3, P5 2ms 2ms 0ms

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

P3 1ms P3 0ms 8ms 8ms


11-
15ms
P5 4ms P3 4ms 4ms 0ms

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

0-1ms P4 0ms 1ms 3ms 2ms

P4 0ms 1ms 2ms 1ms


1-2ms
P3 1ms P3 0ms 8ms 8ms

P4 0ms 1ms 1ms 0ms

2-3ms P3 1ms P3 0ms 8ms 8ms

P1 2ms P3, P1 0ms 6ms 6ms

P3 1ms P3 0ms 8ms 8ms


3-4ms
P1 2ms P3 1ms 6ms 5ms

P3 1ms P3 0ms 8ms 8ms

4-5ms P1 2ms P3 1ms 5ms 4ms

P5 4ms P3, P5 0ms 4ms 4ms

5-6ms P3 1ms P3 0ms 8ms 8ms

P1 2ms P3 1ms 4ms 3ms


Initial Remaining
Time Arrival Waiting Execution Burst Burst
Instance Process Time Table Time Time Time

P5 4ms P3, P5 0ms 4ms 4ms

P3, P5,
P2 5ms 0ms 2ms 2ms
P2

P3 1ms P3 0ms 8ms 8ms

P1 2ms P3 3ms 3ms 0ms

6-9ms
P5 4ms P3, P5 0ms 4ms 4ms

P3, P5,
P2 5ms 0ms 2ms 2ms
P2

P3 1ms P3 0ms 8ms 8ms

9-11ms P5 4ms P3, P5 0ms 4ms 4ms

P2 5ms P3, P5 2ms 2ms 0ms

P3 1ms P3 0ms 8ms 8ms


11-
15ms
P5 4ms P3 4ms 4ms 0ms

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);
}

Time Complexity: O(n^2)


Auxiliary Space: O(n)

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.

SJF Scheduling Algorithm in C:


The CPU scheduling algorithm Shortest Job First (SJF), allocates the CPU to the
processes according to the process with smallest execution time.
SJF uses both preemptive and non-preemptive scheduling. The preemptive version of
SJF is called SRTF (Shortest Remaining Time First). Here we will discuss about SJF
i.e., the non-preemptive scheduling.
Advantages of SJF:
 It has the minimum waiting time among all the scheduling algorithms.
 A process having larger burst time may get into starvation but the problem
can be solved using concept of Ageing.
 It is a greedy algorithm and provides optimal scheduling.
Problem Solution
1. Enter number of processes.
2. Enter the burst time of all the processes.
3. Sort all the processes according to their burst time.
4. Find waiting time, WT of all the processes.
5. For the smallest process, WT = 0.
6. For all the next processes i, find waiting time by adding burst time of all the previously
completed process.
7. Calculate Turnaround time = WT + BT for all the processes.
8. Calculate average waiting time = total waiting time / no. of processes.
9. Calculate average turnaround time= total turnaround time / no. of processes.
SJF Example:
Process Arrival Time Burst Time

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

Average Waiting Time = (4 + 0 + 16 + 9)/4 = 29/4 = 7.25

Turnaround Time: Difference between completion time and arrival time.


Turnaround Time = Completion Time – Arrival Time
P1 turnaround time: 9-0 = 9
P2 turnaround time: 4-0 = 4
P3 turnaround time: 28-0 = 28
P4 turnaround time: 16-0 = 16

Average Turnaround Time = (9 + 4 + 28 + 16)/4 = 14.25

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);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0;

//finding the waiting time of all the processes


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
//individual WT by adding BT of all previous completed
//processes
wt[i]+=bt[j];

//total waiting time


total+=wt[i];
}

//average waiting time


avg_wt=(float)total/n;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround


Time");
for(i=0;i<n;i++)
{
//turnaround time of individual processes
tat[i]=bt[i]+wt[i];

//total turnaround time


totalT+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
//average turnaround time
avg_tat=(float)totalT/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}

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

Average Waiting Time=7.250000


Average Turnaround Time=14.250000

Time Complexity: O(n2)


The above program for implementing SJF Scheduling has a time
complexity of O(n2), as the for loop runs for n^2 times for calculating
waiting time of each process.

You might also like