Professional Documents
Culture Documents
: - 1
Aim: To simulate First Come First Serve & Shortest Job First process scheduling algorithm on
following parameters.
Average waiting time, Average turnaround time, Average Response time, throughput and
CPU utilization for the following scenario
P-ID Arrival Time Burst Time
P1 2 3
P2 1 5
P3 2 3
P4 3 1
P5 2 1
Software Used: GCC Compiler
Theory:-
A. First Come First Serve
For FCFS scheduling algorithm, read the number of processes/jobs in the system, their CPU burst
times. The scheduling is performed on the basis of arrival time of the processes irrespective of
their other parameters. Each process will be executed according to its arrival time. Calculate the
waiting time and turnaround time of each of the processes accordingly.
FCFS provides an efficient, simple and error-free process scheduling algorithm that saves valuable
CPU resources. It uses non-preemptive scheduling in which a process is automatically queued and
processing occurs according to an incoming request or process order. FCFS derives its concept
from real-life customer service.
Let's take a look at how FCFS process scheduling works. Suppose there are three processes in a
queue: P1, P2 and P3. P1 is placed in the processing register with a waiting time of zero seconds
and 10 seconds for complete processing. The next process, P2, must wait 10 seconds and is placed
in the processing cycle until P1 is processed. Assuming that P2 will take 15 seconds to complete,
the final process, P3, must wait 25 seconds to be processed. FCFS may not be the fastest process
scheduling algorithm, as it does not check for priorities associated with processes. These priorities
may depend on the processes' individual execution times.
Algorithm:-
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time Step 4: Set the waiting of the first process as „0‟ and its burst time as its turnaround
time Step 5: for each process in the Ready Q calculate
(c) Waiting time for process (n) = waiting time of process (n-1) + Burst time of process(n-1)
Program:-
#include<stdio.h>
main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg, tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf ("\n Enter Burst Time for Process %d -- ", i);
scanf ("%d", &bt[i]);
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0];
for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", i, bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n); printf("\
nAverage Turnaround Time -- %f", tatavg/n);
}
Operating System (4IT03)//IT//PRMCEAM-Badnera Page 2
Output:-
Algorithm:-
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Operating System (4IT03)//IT//PRMCEAM-Badnera Page 3
Step 4: Start the Ready Q according the shortest Burst time by sorting according to lowest
to highest burst time.
Step 5: Set the waiting time of the first process as „0‟ and its turnaround time as its burst time.
Step 6: For each process in the ready queue, calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of process(n-1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for process(n)
Step 6: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of
process Step 7: Stop the process
Program:
#include<stdio.h>
main()
scanf("%d", &n);
for(i=0;i<n;i++)
p[i]=i;
scanf("%d", &bt[i]);
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
temp=bt[i];
Operating System (4IT03)//IT//PRMCEAM-Badnera Page 4
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
wt[0] = wtavg = 0;
for(i=1;i<n;i++)
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
Conclusion:
Two process scheduling algorithm FCFS and SJF are both non preemptive scheduling
algorithm. After giving input in the form of process id, burst time and arrival time it is
found that FCFS has minimal average waiting that whereas SJF non preemption is best for
processes having minimum execution time. Longer processes will have more waiting time,
eventually they'll suffer starvation. In FCSF There is no option for pre-emption of a
process. If a process is started, then CPU executes the process until it ends.