Experiment - 6
Experiment - 6
Theory:
1. First Come First Served (FCFS) algorithm
The FCFS scheduler s Gantt chart for these tasks would be: ‟
The tasks are inserted into the queue in order A, B, C and D. as shown above. Task A takes 8
time units to complete, B takes 4 units to complete (therefore, B completes at time 12), etc.Task
D ends at time 26, which is the time it took to run and complete all processes.
.Algorithm :
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.
Code:
1. #include <stdio.h>
2. int main()
3. {
4. int pid[15];
5. int bt[15];
6. int n;
7. printf("Enter the number of processes: ");
8. scanf("%d",&n);
9.
10. printf("Enter process id of all the processes: ");
11. for(int i=0;i<n;i++)
12. {
13. scanf("%d",&pid[i]);
14. }
15.
16. printf("Enter burst time of all the processes: ");
17. for(int i=0;i<n;i++)
18. {
19. scanf("%d",&bt[i]);
20. }
21.
22. int i, wt[n];
23. wt[0]=0;
24.
25. //for calculating waiting time of each process
26. for(i=1; i<n; i++)
27. {
28. wt[i]= bt[i-1]+ wt[i-1];
29. }
30.
31. printf("Process ID Burst Time Waiting Time TurnAround Time\n");
32. float twt=0.0;
33. float tat= 0.0;
34. for(i=0; i<n; i++)
35. {
36. printf("%d\t\t", pid[i]);
37. printf("%d\t\t", bt[i]);
38. printf("%d\t\t", wt[i]);
39.
40. //calculating and printing turnaround time of each process
41. printf("%d\t\t", bt[i]+wt[i]);
42. printf("\n");
43.
44. //for calculating total waiting time
45. twt += wt[i];
46.
47. //for calculating total turnaround time
48. tat += (wt[i]+bt[i]);
49. }
50. float att,awt;
51.
52. //for calculating average waiting time
53. awt = twt/n;
54.
55. //for calculating average turnaround time
56. att = tat/n;
57. printf("Avg. waiting time= %f\n",awt);
58. printf("Avg. turnaround time= %f",att);
59. }
Output:
Output:
RR assigns a time quantum (i.e. time slot) to each process waiting to be run. For the jobs A, B,
C and D, RR s Gantt chart would be: ‟ If time quantum = 3
Algorithm:
Round Robin Scheduling Algorithm:
Step1: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.
CODE:
1. #include<stdio.h>
2.
3. int main()
4. {
5. //Input no of processed
6. int n;
7. printf("Enter Total Number of Processes:");
8. scanf("%d", &n);
9. int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];
10. int x = n;
11.
12. //Input details of processes
13. for(int i = 0; i < n; i++)
14. {
15. printf("Enter Details of Process %d \n", i + 1);
16. printf("Arrival Time: ");
17. scanf("%d", &arr_time[i]);
18. printf("Burst Time: ");
19. scanf("%d", &burst_time[i]);
20. temp_burst_time[i] = burst_time[i];
21. }
22.
23. //Input time slot
24. int time_slot;
25. printf("Enter Time Slot:");
26. scanf("%d", &time_slot);
27.
28. //Total indicates total time
29. //counter indicates which process is executed
30. int total = 0, counter = 0,i;
31. printf("Process ID Burst Time Turnaround Time Waiting Time\n");
32. for(total=0, i = 0; x!=0; )
33. {
34. // define the conditions
35. if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)
36. {
37. total = total + temp_burst_time[i];
38. temp_burst_time[i] = 0;
39. counter=1;
40. }
41. else if(temp_burst_time[i] > 0)
42. {
43. temp_burst_time[i] = temp_burst_time[i] - time_slot;
44. total += time_slot;
45. }
46. if(temp_burst_time[i]==0 && counter==1)
47. {
48. x--; //decrement the process no.
49. printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1,
burst_time[i],
50. total-arr_time[i], total-arr_time[i]-burst_time[i]);
51. wait_time = wait_time+total-arr_time[i]-burst_time[i];
52. ta_time += total -arr_time[i];
53. counter =0;
54. }
55. if(i==n-1)
56. {
57. i=0;
58. }
59. else if(arr_time[i+1]<=total)
60. {
61. i++;
62. }
63. else
64. {
65. i=0;
66. }
67. }
68. float average_wait_time = wait_time * 1.0 / n;
69. float average_turnaround_time = ta_time * 1.0 / n;
70. printf("\nAverage Waiting Time:%f", average_wait_time);
71. printf("\nAvg Turnaround Time:%f", average_turnaround_time);
72. return 0;
73. }
Output:
Conclusion: Thus we have analysed that the FCFS is better for a small burst time. The SJF
is better if the process comes to processor simultaneously. The Round Robin, is better to adjust
the average waiting time desired.