You are on page 1of 7

Practical No.

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

Operating System (4IT03)//IT//PRMCEAM-Badnera Page 1


(d) Turnaround time for Process (n) = waiting time of Process (n)+Burst time for process
(n) Step 6: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process

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

B. Shortest Job First (Non- Preemption)


Theory:
Shortest Job First: The criteria of this algorithm are which process having the smallest CPU
burst, CPU is assigned to that next process. If two process having the same CPU burst time FCFS
is used to break the tie.

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

int p[20], bt[20], wt[20], tat[20], i, k, n, temp;

float wtavg, tatavg;

printf("\nEnter the number of processes -- ");

scanf("%d", &n);

for(i=0;i<n;i++)

p[i]=i;

printf("Enter Burst Time for Process %d -- ", 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;

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("\n\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", p[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 5


Output:-

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.

Operating System (4IT03)//IT//PRMCEAM-Badnera Page 6


Viva Questions:

1. What is First-Come-First-Served (FCFS) Scheduling?


2. Define dispatcher and its functions.
3. Why CPU scheduling is required?
4. Which technique was introduced because a single job could not keep both the CPU and the
I/O devices busy?
5. CPU performance is measured through.
6. Define dispatch latency.
7. Explain different states of process.
8. Explain and differentiate between new and ready state.
9. What are different types of scheduler?
10. In terms of average wait time the optimum scheduling algorithm is.
11. Explain different declaration syntax of for loop.
12. What do you mean by turnaround time, waiting time, and response time?
13. What will you define throughput?
14. What it the return value of a relational operator if it returns any?
15. How does bitwise operator XOR works. What is an infinite loop?
16. Can variables belonging to different scope have same name? If so show an example.
17. What is Shortest-Job-First (SJF) Scheduling?
18. What will we do if two processes in the SJF scheduling have same Burst time?

19. Which one is simple to implement FCFS or SJF?


20. Which algorithm performance is better FCFS or SJF?

Operating System (4IT03)//IT//PRMCEAM-Badnera Page 7

You might also like