1. CPU Scheduling
CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold. The main task of CPU scheduling is to make sure that whenever the
CPU remains idle, the OS at least select one of the processes available in the ready queue for
execution. The selection process will be carried out by the CPU scheduler. It selects one of the
processes in memory that are ready for execution.
Important CPU scheduling Terminologies
Burst Time/Execution Time: It is a time required by the process to complete execution.
It is also called running time.
Arrival Time: when a process enters in a ready state
Finish Time: when process complete and exit from a system
Multiprogramming: A number of programs which can be present in memory at the same
time.
Jobs: It is a type of program without any kind of user interaction.
User: It is a kind of program having user interaction.
Process: It is the reference that is used for both job and user.
CPU/IO burst cycle: Characterizes process execution, which alternates between CPU and
I/O activity. CPU times are usually shorter than the time of I/O.
CPU Scheduling Criteria
A CPU scheduling algorithm tries to maximize and minimize the following:
2. Maximize:
CPU utilization: CPU utilization is the main task in which the operating system needs to
make sure that CPU remains as busy as possible. It can range from 0 to 100 percent.
However, for the RTOS, it can be range from 40 percent for low-level and 90 percent for
the high-level system.
Throughput: The number of processes that finish their execution per unit time is known
Throughput. So, when the CPU is busy executing the process, at that time, work is being
done, and the work completed per unit time is called Throughput.
Minimize:
Waiting time: Waiting time is the total time spent by the process in the ready state waiting
for CPU. For example, consider the arrival time of all the below 3 processes to be 0 ms, 0
ms, and 2 ms.
Then the waiting time for all the 3 processes will be:
P1: 0 ms
P2: 8 ms because P2 have to wait for the complete execution of P1 and arrival time of
P2 is 0 ms.
P3: 13 ms becuase P3 will be executed after P1 and P2 i.e. after 8+7 = 15 ms and the
arrival time of P3 is 2 ms. So, the waiting time of P3 will be: 15-2 = 13 ms.
Waiting time = Turnaround time - Burst time
3. Response time: Response time is the time spent when the process is in the ready state and
gets the CPU for the first time. For example, consider the below processes:
Here, the response time of all the 3 processes are:
P1: 0 ms
P2: 7 ms because the process P2 have to wait for 8 ms during the execution of P1 and
then after it will get the CPU for the first time. Also, the arrival time of P2 is 1 ms. So,
the response time will be 8-1 = 7 ms.
P3: 13 ms because the process P3 have to wait for the execution of P1 and P2 i.e. after
8+7 = 15 ms, the CPU will be allocated to the process P3 for the first time. Also, the
arrival of P3 is 2 ms. So, the response time for P3 will be 15-2 = 13 ms.
Response time = Time at which the process gets the CPU for the first time - Arrival time
Turnaround Time: Turnaround time is the total amount of time spent by the process
from coming in the ready state for the first time to its completion.
Turnaround time = Burst time + Waiting time
Types of CPU scheduling Algorithm
Non-Preemptive Scheduling: Under non-preemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it releases the CPU either by
terminating or by switching to the waiting state. In non-preemptive scheduling, it does not
interrupt a process running CPU in the middle of the execution. Instead, it waits till the
process completes its CPU burst time, and then after that it can allocate the CPU to any
other process. Some Algorithms based on non-preemptive scheduling are: Shortest Job
First (SJF basically non-preemptive) Scheduling and Priority (non- preemptive version)
scheduling, etc.
4. Preemptive Scheduling: In this type of Scheduling, the tasks are usually assigned with
priorities. At times it is necessary to run a certain task that has a higher priority before
another task although it is running. Therefore, the running task is interrupted for some time
and resumed later when the priority task has finished its execution. Some Algorithms that
are based on preemptive scheduling are Round Robin Scheduling (RR), Shortest
Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc.
5. First Come First Serve (FCFS)
The process that requests the services of CPU first, get the CPU first. This is the philosophy used
by the first come first serve algorithm..
Important Points
It is non-preemptive
Average waiting time is not optimal
Cannot utilize resources in parallel
Consider the following set of processes that arrive at time 0, with the length of the CPU burst given
in milliseconds:
If the processes arrive in the order P1, P2, P3, and are served in FCFS order, we get the result
shown in the following Gantt chart, which is a bar chart that illustrates a particular schedule,
including the start and finish times of each of the participating processes:
6. The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2, and 27
milliseconds for process P3. Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.
If the processes arrive in the order P2, P3, P1, however, the results will be as shown in the
following Gantt chart:
The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds. This reduction is substantial.
Thus, the average waiting time under an FCFS policy is generally not minimal and may vary
substantially if the processes’ CPU burst times vary greatly.
Sample Code
#include<stdio.h>
int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);
printf("Enter Process Burst Time n");
for(i=0;i<n;i++)
{
printf("P[%d]:",i);
scanf("%d",&bt[i]);
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
7. wt[i]+=bt[j];
}
printf("Process Burst Time Waiting Time
Turnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("nP[%d] %d %d
%d",i,bt[i],wt[i],tat[i]);
}
avwt/=i;
avtat/=i;
printf("nAverage Waiting Time:%d",avwt);
printf("nAverage Turnaround Time:%d",avtat);
return 0;
}
Sample Output
Enter total number of processes(maximum 20):3
Enter Process Burst Time
P[0]:24
P[1]:3
P[2]:3
Process Burst Time Waiting Time Turnaround Time
P[0] 24 0 24
P[1] 3 24 27
P[2] 3 27 30
Average Waiting Time:17
8. Average Turnaround Time:27
Shortest Job First (SJF) Scheduling Algorithm
SJF is an algorithm in which the process having the smallest execution time is chosen for the next
execution. This scheduling method can be preemptive or non-preemptive. It significantly reduces
the average waiting time for other processes awaiting execution. The full form of SJF is Shortest
Job First.
Sample Code
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");