0% found this document useful (0 votes)
30 views11 pages

Experiment - 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

Experiment - 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment No.

Aim: Write a program to implement Basic Process management algorithms such as


FCFS, SJF and RR.

Theory:
1. First Come First Served (FCFS) algorithm

It is the simplest algorithm and NON-PREEMPTIVE.


• The process that requests the CPU first is allocated the CPU first.
• The implementation is easily managed by queue. When a process enters the ready
queue, its PCB is linked onto the tail of the queue. When the CPU is free, it is allocated
to the process at the head of the queue.
• The average waiting time, however, is long. It is not minimal and may vary substantially
if the process CPU burst time varies greatly.
• This algorithm is particularly troublesome for time-sharing systems.

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.

The average waiting time of FCFS is usually quite long.

.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:

2. Shortest Job First(SJF)Process Management

• The SJF is a special case of priority scheduling.


• In priority scheduling algorithm, a priority is associated with each process, and the CPU is
allocated to the process with the highest priority. Two Schemes:Non-preemptive (SRTF-
Shortest Remaining Time First)-Once CPU given to the process it cannot be preempted until
completes its CPU burst. Preemptive-If a new process arrives with CPU burst length less than
remaining time of current executing process, preempt. This scheme is known as the Shortest-
Remaining-TimeFirst(SRTF). SJF is optimal-Gives minimum average waiting time for a given
set of processes To illustrate it, suppose the scheduler is given 4 tasks, A, B, C and D. Each
task requires a certain number of time units to complete.

The SJF Gantt Chart would be:


From the metrics, we see that SJF achieves better performance than FCFS. However, unlike
FCFS, there is the potential for starvation in SJF. Starvation occurs when a large process never
gets run to run because shorter jobs keep entering the queue. In addition, SJF needs to know how
long a process is going to run (i.e. it needs to predict the future). This runtime estimationfeature
may be hard to implement, and thus SJF is not a widely used scheduling scheme.
Algorithm:
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.
Code:
1. #include<stdio.h>
2. int main()
3. {
4. int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,totalT=0,pos,temp;
5. float avg_wt,avg_tat;
6. printf("Enter number of process:");
7. scanf("%d",&n);
8.
9. printf("\nEnter Burst Time:\n");
10. for(i=0;i<n;i++)
11. {
12. printf("p%d:",i+1);
13. scanf("%d",&bt[i]);
14. p[i]=i+1;
15. }
16.
17. //sorting of burst times
18. for(i=0;i<n;i++)
19. {
20. pos=i;
21. for(j=i+1;j<n;j++)
22. {
23. if(bt[j]<bt[pos])
24. pos=j;
25. }
26.
27. temp=bt[i];
28. bt[i]=bt[pos];
29. bt[pos]=temp;
30.
31. temp=p[i];
32. p[i]=p[pos];
33. p[pos]=temp;
34. }
35.
36. wt[0]=0;
37.
38. //finding the waiting time of all the processes
39. for(i=1;i<n;i++)
40. {
41. wt[i]=0;
42. for(j=0;j<i;j++)
43. //individual WT by adding BT of all previous completed processes
44. wt[i]+=bt[j];
45.
46. //total waiting time
47. total+=wt[i];
48. }
49.
50. //average waiting time
51. avg_wt=(float)total/n;
52.
53. printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
54. for(i=0;i<n;i++)
55. {
56. //turnaround time of individual processes
57. tat[i]=bt[i]+wt[i];
58.
59. //total turnaround time
60. totalT+=tat[i];
61. printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
62. }
63.
64. //average turnaround time
65. avg_tat=(float)totalT/n;
66. printf("\n\nAverage Waiting Time=%f",avg_wt);
67. printf("\nAverage Turnaround Time=%f",avg_tat);
68. }

Output:

3. Round Robin (RR) Process Management Algorithm:

• It is designed specially for time-sharing systems.


• It is similar to FCFS, but preemption is added to switch between processes. A time quantum
is defined.
• The CPU scheduler goes around the ready queue, allocating the CPU to each process for a
time interval of upto 1 time quantum. If a processes CPU burst exceeds 1 time quantum, that
process is preempted and is put back in the ready queue.
• The scheduler assigns a fixed time unit per process, and cycles through them.
• RR scheduling involves extensive overhead, especially with a small time unit. Balanced
throughput between FCFS and SJF, shorter jobs are completed faster than in FCFS and longer
processes are completed faster than in SJF. Fastest average response time, waiting time is
dependent onnumber of processes,and not average process length. Because of high waiting
times, deadlines are rarely met in a pure RR system. Starvation can never occur, since no
priority is given. Order of time unit allocation is based upon process
arrival time, similar to FCFS. To illustrate it, suppose the scheduler is given 4 tasks, A, B, C
and D. Each task requires a certain number of time units to complete.

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:

Enter Total Number of Processes:3


Enter Details of Process 1
Arrival Time: 0
Burst Time: 10
Enter Details of Process 2
Arrival Time: 1
Burst Time: 8
Enter Details of Process 3
Arrival Time: 2
Burst Time: 7
Enter Time Slot:5

Process ID Burst Time Turnaround Time Waiting Time


Process No 1 10 20 10
Process No 2 8 22 14
Process No 3 7 23 16

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.

You might also like