Unit3 - Scheduling Algorithms
Unit3 - Scheduling Algorithms
CPU SCHEDULING
CPU SCHEDULING
CPU scheduling is the basis of multiprogrammed OS. If a process is waiting for an I/O request,
the CPU switches from that job to another job and so CPU is made busy always. By switching CPU
among processes, the OS makes the computer productive. CPU scheduling is fundamental and all
resources are scheduled before use. CPU Scheduling algorithms decides how the CPU will be allocated to
the process
• When the process is assigned to a processor, the processor don’t release until the completion of
that process
• The CPU will be assigned to another process(job) only after the previous jobs gets completed
• The CPU can release the process even in the middle of the execution
Schedulers
The OS selects processes from these queues through a mechanism called scheduling and it is
done by a scheduler.
1. Long-term scheduler (or job scheduler) 2.Short-term scheduler (or CPU scheduler)
3. Medium term Scheduler
Long-term scheduler (or job scheduler) selects the jobs from the pool of jobs and load these
jobs into main memory(ready queue) of a computer
Eg: Consider a computer lab in a college. The lab has 30 dummy nodes and a server, and they are
connected by a LAN. Out of 30, 20 are trying to execute their jobs and these 20 are loaded into a spool
spool is a kind of buffer that holds the jobs for a device till the device is ready to accept the job)
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
• A ready queue is a data structure with 2 ends namely front and rear
The jobs are loaded into the ready queue through the front and the jobs are deleted from the ready
queue through the rear
• Short-term scheduler (or CPU scheduler) selects a job from the ready queue and give the
control of the CPU to that process with the help of ‘Dispatcher’
• The method of selecting a process from the ready queue by the short term scheduler depends on
the CPU scheduling algorithm
• Dispatcher is a module which connects the CPU to the process selected by the short term
scheduler
• The main function of the Dispatcher is switching the CPU from one process to another process
3. Medium-term scheduler
• If a process request an I/O in the middle of the execution, the process gets removed from the main
memory and loaded into the waiting queue
• When the I/O operation is completed, the job will be moved from the waiting queue to the ready
queue
Scheduling Criteria
1. CPU utilization
CPU utilization denotes the % of time that the processor is busy. CPU utilization ranges from 0
to 100
2. Throughput
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
3. Turnaround time
Turnaround time is the amount of time to execute a particular process from the time of
submission through the time of completion
It is the sum of all periods spent waiting to get into memory, waiting in the ready queue,
executing on the CPU and doing I/O
4. Waiting time
Waiting time is the amount of time spent by a process in the ready queue
5. Response time
Response time is the amount of time it takes from when a request was submitted until the first
response occurs. It is the time duration between the submission and first response. Example: A process
enters into ready queue at 10:10 am. But the process got the first response from the CPU at 10:15am.
Response time is 5 minutes
Scheduling Algorithms
Preemptive SJF with varied arrival times called Shortest-remaining time First (SRT)
Scheduling
3. Priority Scheduling
“The process that enters the ready queue first is served first”.
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
Example:
[Burst time is the time required for the CPU to execute the jobs]
Gantt chart:
Note:
Average waiting time and average response time are same as it is a non preemptive scheduling
algorithm. In case of non-preemptive scheduling, once the CPU has been allocated to a process, that
process keeps the CPU till it releases the CPU. In the above example, the arrival time of all processes is 0
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
Gantt chart:
In Shortest Job First Scheduling (SJF), CPU will be assigned to a process with smallest CPU
burst . If 2 processes having the same CPU burst time, FCFC is used to break the tie.
Example:
1. Calculate the average waiting time and average turn around time.
Gantt chart:
Here p5 is assigned to CPU first as it has the least burst time, followed by P1,P4,P3 and P2
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
• When a new process joins the ready queue, the short term scheduler compares the remaining time
of executing process and new process
• If the new process has the least CPU burst time, the scheduler select that job and connect to CPU
Gantt chart:
Process P1 arrives at time 0 and gets executed first. Process P2 arrives at time 2 ms. When P2 arrives, the
remaining time for P1 to complete is 7 ms. But P2 needs 5 ms. As 5<7, p2 will be executed. P3 arrives at
time 3 ms. Now the RT of P2 =4 ms, As 4<6, P2 will continue. P5 & P6 arrive at time 5 and 6 ms. P4
needs 4 ms and hence executed. Then P3 will be executed as it needs 6 ms. Then P 1 and P5
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
• When the time quantum is expired, the CPU switched to another job
• A small unit of time is called quantum or time slice and is generally from 10 to 100ms
Example:
4. Priority Scheduling
• The CPU is allocated to the process with the highest priority (smallest integer highest priority)
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
WT of P1: 3
WT of P2: 13
WT of P3: 25
WT of P4: 0
WT of P5:9
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
P3: (2-2)=0
P1: 9-0=0
P2: 25-0=25
P3: 26-0=26
P4: 3-0=3
P5: 13-0=13
Note:
Starvation:
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
Aging:
It is a technique of gradually increasing the priority of processes that wait in the system for a long
time. Thus the older processes attain high priority and are ensured of completion in a finite time.
• Each ready queue is capable to load same type of jobs and each ready queue has its own
scheduling algorithm
Example:
In Fig., the system process queue has the highest priority. The student process queue has the
lowest priority. No process in the student queue can run unless the queues for system process, foreground
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus
• In multi-level feedback queue scheduling, a process can move between the various queues
• In this algorithm, processes are not permanently assigned to queues, instead they are allowed to
move between the queues
• The decision to move a process between queues is based on the time of its execution and waiting
time
• A process which waits for a long time in a lower priority queue is moved to a higher priority
queue in order to avoid starvation
Consider a multilevel feedback queue scheduler with 3 queues say Q1, Q2 and Q3. Q1, Q2
employ RR scheduling algorithm with time quantum of 8 ms and 16 ms, while in Q3, the processes are
scheduled in FCFS order. The scheduler first executes all processes in Q1, followed by Q2 and Q3. While
executing processes in Q2, if a new process arrives in Q1, Q2 is preempted and Q1 starts executed. Also a
process arriving in Q2 preempts a process executing in Q3
J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus