CPU Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive
algorithms are designed so that once a process enters the running state, it cannot
be preempted until it completes its allotted time, whereas the preemptive
scheduling is based on priority where a scheduler may preempt a low priority
running process anytime when a high priority process enters into a ready state.
First Come First Serve (FCFS)
Jobs are executed on first come, first serve basis.
It is a non-preemptive, pre-emptive scheduling algorithm.
Easy to understand and implement.
Its implementation is based on FIFO queue.
Poor in performance as average wait time is high.
FCFS is considered as simplest CPU-scheduling algorithm. In FCFS algorithm,
the process that requests the CPU first is allocated in the CPU first. The
implementation of FCFS algorithm is managed with FIFO (First in first out)
queue. FCFS scheduling is non-preemptive. Non-preemptive means, once the
CPU has been allocated to a process, that process keeps the CPU until it executes
a work or job or task and releases the CPU, either by requesting I/O.
Real Life Example Of FCFS Scheduling
As a real life example of FCFS scheduling a billing counter system of shopping
mall can be observed. The first person in the line gets the bill done first and then
the next person gets the chance to get the bill and make payment and so on. If no
priority is given to the VIP customers then the billing system will go on like this
(means the first person (first task) in the line will get the bill first and after
finishing (executing) the first customers payment the counter boy(CPU) will pay
attention to other customers (separate tasks) as they are in the line). As FCFS is
non-preemptive type so no priority will be given to the random important tasks.
FCFS Scheduling Mathematical Examples
In CPU-scheduling problems some terms are used while solving the problems, so
for conceptual purpose the terms are discussed as follows −
Arrival time (AT) − Arrival time is the time at which the process arrives in
ready queue.
Burst time (BT) or CPU time of the process − Burst time is the unit of time in
which a particular process completes its execution.
Completion time (CT) − Completion time is the time at which the process has
been terminated.
Turn-around time (TAT) − The total time from arrival time to completion time
is known as turn-around time. TAT can be written as,
Turn-around time (TAT) = Completion time (CT) − Arrival time
(AT) or TAT = Burst time (BT) + Waiting time (WT)
Waiting time (WT) Waiting time is the time at which the process waits for its
allocation while the previous process is in the CPU for execution. WT is written
as,
Waiting time (WT) = Turn-around time (TAT) − Burst time (BT)
Response time (RT) − Response time is the time at which CPU has been
allocated to a particular process first time.
In case of non-preemptive scheduling, generally Waiting time and Response time
is same.
Gantt chart − Gantt chart is a visualization which helps to scheduling and
managing particular tasks in a project. It is used while solving scheduling
problems, for a concept of how the processes are being allocated in different
algorithms.
Problem 1
Consider the given table below and find Completion time (CT), Turn-around
time (TAT), Waiting time (WT), Response time (RT), Average Turn-around time
and Average Waiting time.
Process ID Arrival time Burst time
P1 2 2
P2 5 6
P3 0 4
P4 0 7
P5 7 4
Solution
Gantt chart
For this problem CT, TAT, WT, RT is shown in the given table
Process Arrival Burst time CT TAT=CT- WT=TAT- RT
ID time AT BT
P1 2 2 13 13-2= 11 11-2= 9 9
P2 5 6 19 19-5= 14 14-6= 8 8
P3 0 4 4 4-0= 4 4-4= 0 0
P4 0 7 11 11-0= 11 11-7= 4 4
P5 7 4 23 23-7= 16 16-4= 12 12
Average Waiting time = (9+8+0+4+12)/5 = 33/5 = 6.6 time unit (time unit can
be considered as milliseconds)
Average Turn-around time = (11+14+4+11+16)/5 = 56/5 = 11.2 time unit (time
unit can be considered as milliseconds)
Example
Input-: processes = P1, P2, P3
Burst time = 5, 8, 12
Output-:
Processes Burst Waiting Turn around
1 5 0 5
2 8 5 13
3 Output
Processes Burst Waiting Turn around
1 5 0 5
2 8 5 13
3 12 13 25
Average Waiting time = 6.000000
Average turn around time = 14.333333
12 13 25
Average Waiting time = 6.000000
Average turn around time = 14.333333
Start
Step 1-> In function int waitingtime(int proc[], int n, int burst_time[], int
wait_time[])
Set wait_time[0] = 0
Loop For i = 1 and i < n and i++
Set wait_time[i] = burst_time[i-1] + wait_time[i-1]
End For
Step 2-> In function int turnaroundtime( int proc[], int n, int burst_time[], int
wait_time[], int tat[])
Loop For i = 0 and i < n and i++
Set tat[i] = burst_time[i] + wait_time[i]
End For
Step 3-> In function int avgtime( int proc[], int n, int burst_time[])
Declare and initialize wait_time[n], tat[n], total_wt = 0, total_tat = 0;
Call waitingtime(proc, n, burst_time, wait_time)
Call turnaroundtime(proc, n, burst_time, wait_time, tat)
Loop For i=0 and i<n and i++
Set total_wt = total_wt + wait_time[i]
Set total_tat = total_tat + tat[i]
Print process number, burstime wait time and turnaround time
End For
Print "Average waiting time =i.e. total_wt / n
Print "Average turn around time = i.e. total_tat / n
Step 4-> In int main()
Declare the input int proc[] = { 1, 2, 3}
Declare and initialize n = sizeof proc / sizeof proc[0]
Declare and initialize burst_time[] = {10, 5, 8}
Call avgtime(proc, n, burst_time)
Stop
Program in C:
#include <stdio.h>
// Function to find the waiting time for all processes
int waitingtime(int proc[], int n,int burst_time[], int wait_time[])
{
// waiting time for first process is 0
wait_time[0] = 0;
// calculating waiting time
for (int i = 1; i < n ; i++ )
wait_time[i] = burst_time[i-1] + wait_time[i-1] ;
return 0;
}
// Function to calculate turn around time
int turnaroundtime( int proc[], int n,int burst_time[], int wait_time[], int tat[])
{
// calculating turnaround time by adding
// burst_time[i] + wait_time[i]
int i;
for ( i = 0; i < n ; i++)
tat[i] = burst_time[i] + wait_time[i];
return 0;
}
//Function to calculate average time
int avgtime( int proc[], int n, int burst_time[]) {
int wait_time[n], tat[n], total_wt = 0, total_tat = 0;
int i;
//Function to find waiting time of all processes
waitingtime(proc, n, burst_time, wait_time);
//Function to find turn around time for all processes
turnaroundtime(proc, n, burst_time, wait_time, tat);
//Display processes along with all details
printf("Processes Burst Waiting Turn around ");
// Calculate total waiting time and total turn
// around time
for ( i=0; i<n; i++) {
total_wt = total_wt + wait_time[i];
total_tat = total_tat + tat[i];
printf("\n %d\t %d\t\t %d \t%d", i+1, burst_time[i], wait_time[i], tat[i]);
}
printf("\nAverage waiting time = %f", (float)total_wt / (float)n);
printf("\nAverage turn around time = %f", (float)total_tat / (float)n);
return 0;
}
// main function
int main() {
//process id's
int proc[] = { 1, 2, 3};
int n = sizeof proc / sizeof proc[0];
//Burst time of all processes
int burst_time[] = {5, 8, 12};
avgtime(proc, n, burst_time);
return 0;
}
Output
Processes Burst Waiting Turn around
1 5 0 5
2 8 5 13
3 12 13 25
Average Waiting time = 6.000000
Average turn around time = 14.333333
Shortest Job First (SJF) Scheduling Algorithm
Shortest Job First is a non-preemptive scheduling algorithm in which the process
with the shortest burst or completion time is executed first by the CPU. That
means the lesser the execution time, the sooner the process will get the CPU. In
this scheduling algorithm, the arrival time of the processes must be the same, and
the processor must be aware of the burst time of all the processes in advance. If
two processes have the same burst time, then First Come First Serve
(FCFS) scheduling is used to break the tie.
The preemptive mode of SJF scheduling is known as the Shortest Remaining
Time First scheduling algorithm.