Journal Programs
Journal Programs
Problem Description:
Write an FCFS Scheduling Program in C to determine the average waiting time and average
turnaround time given n processes and their burst times.
Problem Solution
1. Enter all the processes and their burst time.
2. Find waiting time, WT of all the processes.
3. For the 1st process, WT = 0.
4. For all the next processes i, WT[i] = BT[i-1] + WT[i-1].
5. Calculate Turnaround time = WT + BT for all the processes.
6. Calculate average waiting time = total waiting time/no. of processes.
7. Calculate average turnaround time = total turnaround time/no. of processes.
Example:
Process Arrival Time Burst Time
P1 0 5
P2 0 11
P3 0 11
Gantt Chart:
Waiting Time: Time Difference between turnaround time and burst time.
Waiting Time = Turnaround Time – Burst Time
P1 waiting time: 0
P2 waiting time: 5
P3 waiting time: 16
Average Waiting Time = (0 + 5 + 16)/3 = 21/3 = 7
Turnaround Time: Difference between completion time and arrival time.
Turnaround Time = Completion Time – Arrival Time
P1 turnaround time: 5-0 = 5
P2 turnaround time: 16-0 = 16
P3 turnaround time: 27-0 = 27
Average Turnaround Time = (5+16+27)/3 = 16
Program/Source Code
1. /*
2. * FCFS Scheduling Program in C
3. */
4.
5. #include <stdio.h>
6. int main()
7. {
8. int pid[15];
9. int bt[15];
10. int n;
11. printf("Enter the number of processes: ");
12. scanf("%d",&n);
13.
14. printf("Enter process id of all the processes: ");
15. for(int i=0;i<n;i++)
16. {
17. scanf("%d",&pid[i]);
18. }
19.
20. printf("Enter burst time of all the processes: ");
21. for(int i=0;i<n;i++)
22. {
23. scanf("%d",&bt[i]);
24. }
25.
26. int i, wt[n];
27. wt[0]=0;
28.
29. //for calculating waiting time of each process
30. for(i=1; i<n; i++)
31. {
32. wt[i]= bt[i-1]+ wt[i-1];
33. }
34.
35. printf("Process ID Burst Time Waiting Time TurnAround Time\
n");
36. float twt=0.0;
37. float tat= 0.0;
38. for(i=0; i<n; i++)
39. {
40. printf("%d\t\t", pid[i]);
41. printf("%d\t\t", bt[i]);
42. printf("%d\t\t", wt[i]);
43.
44. //calculating and printing turnaround time of each process
45. printf("%d\t\t", bt[i]+wt[i]);
46. printf("\n");
47.
48. //for calculating total waiting time
49. twt += wt[i];
50.
51. //for calculating total turnaround time
52. tat += (wt[i]+bt[i]);
53. }
54. float att,awt;
55.
56. //for calculating average waiting time
57. awt = twt/n;
58.
59. //for calculating average turnaround time
60. att = tat/n;
61. printf("Avg. waiting time= %f\n",awt);
62. printf("Avg. turnaround time= %f",att);
63.}
Program Explanation
1. Initialize two array pid[] and bt[] of size 15.
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. Calculate waiting time of each process by the formula wt[i] = wt[i-1] + bt[i-1].
5. Print Process Id, Burst Time, waiting time and Turnaround time of each process in tabular
manner.
6. Calculate and print turnaround time of each process = bt[i] + wt[i].
7. Add waiting time of all the processes and store it in the variable twt.
8. Add turnaround time of all the processes and store it in the variable tat.
9. Calculate average waiting time as awt = twt/n.
10. Calculate average turnaround time as att = tat/n;
11. Print average waiting time and average turnaround time.
12. Exit.
OUTPUT
Enter the number of processes: 3
Enter process id of all the processes: 1 2 3
Enter burst time of all the processes: 5 11 11
Process ID Burst Time Waiting Time TurnAround Time
1 5 0 5
2 11 5 16
3 11 16 27
Avg. waiting time= 7.000000
Avg. turnaround time= 16.000000
SJF Scheduling Algorithm in C
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
Program/Source Code
Here is the source code of the C Program to Implement SJF Scheduling. The C program is
successfully compiled and run on a Linux system. The program output is also shown below.
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.
OUTPUT
Enter number of process:4
Problem Solution
Round Robin Scheduling Algorithm:
Step 1: Start the Program.
Step 2: Input the number of processes.
Step 3: Input the burst time and arrival time of each process and the limit of the time slot.
Step 4: Push all processes into the ready queue according to their arrival time. Then execute each
process upto time slot and push left over process in queue again for execution.
Step 5: After a process is completely executed, print its turn around time and waiting time.
Example:
Following is the example of round robin scheduling.
Process Id Arrival Time Burst Time
P1 0 10
P2 1 8
P3 2 7
Time Slot is 5 Sec.
First P1 is executed for 5 seconds, left burst time is 5 sec
Then P2 is executed for 5 seconds, left burst time is 3 sec
Then P3 is executed for 5 seconds, left burst time is 2 sec
Then P1 is executed for 5 seconds, execution of P1 is completed.
Then P2 is executed for 3 seconds, execution of P2 is completed.
Then P1 is executed for 2 sec, execution P3 is completed.
Execution of all processes completed
P1 10 20 10
P2 8 22 14
P3 7 23 16
Advantages:
There is no starvation of resources as each process gets equal share.
Every Process gets equal allocation of CPU.
Increases Performance time in terms of response time.
Disadvantages:
More time is wasted on context switching.
Frequent context switching increases CPU overhead.
Average Waiting time increases for each process.
Program/Source Code
Here is the source code of the C program to implement Round Robin scheduling. The C program
is successfully compiled and run on a Linux system. The program output is also shown below.
1. /*
2. * Round Robin Scheduling Program in C
3. */
4.
5. #include<stdio.h>
6.
7. int main()
8. {
9. //Input no of processed
10. int n;
11. printf("Enter Total Number of Processes:");
12. scanf("%d", &n);
13. int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n],
temp_burst_time[n];
14. int x = n;
15.
16. //Input details of processes
17. for(int i = 0; i < n; i++)
18. {
19. printf("Enter Details of Process %d \n", i + 1);
20. printf("Arrival Time: ");
21. scanf("%d", &arr_time[i]);
22. printf("Burst Time: ");
23. scanf("%d", &burst_time[i]);
24. temp_burst_time[i] = burst_time[i];
25. }
26.
27. //Input time slot
28. int time_slot;
29. printf("Enter Time Slot:");
30. scanf("%d", &time_slot);
31.
32. //Total indicates total time
33. //counter indicates which process is executed
34. int total = 0, counter = 0,i;
35. printf("Process ID Burst Time Turnaround Time Waiting Time\
n");
36. for(total=0, i = 0; x!=0; )
37. {
38. // define the conditions
39. if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
40. {
41. total = total + temp_burst_time[i];
42. temp_burst_time[i] = 0;
43. counter=1;
44. }
45. else if(temp_burst_time[i] > 0)
46. {
47. temp_burst_time[i] = temp_burst_time[i] - time_slot;
48. total += time_slot;
49. }
50. if(temp_burst_time[i]==0 && counter==1)
51. {
52. x--; //decrement the process no.
53. printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1,
burst_time[i],
54. total-arr_time[i], total-arr_time[i]-burst_time[i]);
55. wait_time = wait_time+total-arr_time[i]-burst_time[i];
56. ta_time += total -arr_time[i];
57. counter =0;
58. }
59. if(i==n-1)
60. {
61. i=0;
62. }
63. else if(arr_time[i+1]<=total)
64. {
65. i++;
66. }
67. else
68. {
69. i=0;
70. }
71. }
72. float average_wait_time = wait_time * 1.0 / n;
73. float average_turnaround_time = ta_time * 1.0 / n;
74. printf("\nAverage Waiting Time:%f", average_wait_time);
75. printf("\nAvg Turnaround Time:%f", average_turnaround_time);
76. return 0;
77.}
Program Explanation
1. Ask the user for number of processes n.
2. After that, ask the user for the arrival time and burst time of each process. Also input the time
quantum.
3. In the loop, if time slot is greater than left burst time, execute process and find burst time.
4. Else if burst time is greater than time slot, execute it up to time slot and again push into the
queue.
5. when the execution is completed, print the process information such as turnaround time and
waiting time.
OUTPUT
Process No 1 10 20 10
Process No 2 8 22 14
Process No 3 7 23 16
#include<stdio.h>
int main() {
/* array will store at most 5 process with 3 resoures if your process or
resources is greater than 5 and 3 then increase the size of array */
int p, c, count = 0, i, j, alc[5][3], max[5][3], need[5][3], safe[5], available[3],
done[5], terminate = 0;
printf("Enter the number of process and resources");
scanf("%d %d", & p, & c);
// p is process and c is diffrent resources
printf("enter allocation of resource of all process %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & alc[i][j]);
}
}
printf("enter the max resource process required %dx%d matrix", p, c);
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
scanf("%d", & max[i][j]);
}
}
printf("enter the available resource");
for (i = 0; i < c; i++)
scanf("%d", & available[i]);
printf("\n need resources matrix are\n");
for (i = 0; i < p; i++) {
for (j = 0; j < c; j++) {
need[i][j] = max[i][j] - alc[i][j];
printf("%d\t", need[i][j]);
}
printf("\n");
}
/* once process execute variable done will stop them for again execution */
for (i = 0; i < p; i++) {
done[i] = 0;
}
while (count < p) {
for (i = 0; i < p; i++) {
if (done[i] == 0) {
for (j = 0; j < c; j++) {
if (need[i][j] > available[j])
break;
}
//when need matrix is not greater then available matrix then if j==c will true
if (j == c) {
safe[count] = i;
done[i] = 1;
/* now process get execute release the resources and add them in available
resources */
for (j = 0; j < c; j++) {
available[j] += alc[i][j];
}
count++;
terminate = 0;
} else {
terminate++;
}
}
}
if (terminate == (p - 1)) {
printf("safe sequence does not exist");
break;
}
}
if (terminate != (p - 1)) {
printf("\n available resource after completion\n");
for (i = 0; i < c; i++) {
printf("%d\t", available[i]);
}
printf("\n safe sequence are\n");
for (i = 0; i < p; i++) {
printf("p%d\t", safe[i]);
}
}
return 0;
}
OUTPUT
Enter the number of process and resources
53
enter allocation of resource of all process 5x3 matrix
010
200
302
211
002
enter the max resource process required 5x3 matrix
753
322
902
422
533
enter the available resource 3 3 2
need resources matrix are
7 4 3
1 2 2
6 0 0
2 1 1
5 3 1
available resource after completion
10 5 7
safe sequence are
p1 p3 p4 p0 p2