Module 2.2 CPU Scheduling
Module 2.2 CPU Scheduling
Scheduling
Nirmala Shinde Baloorkar
Assistant Professor
Department of Computer Engineering
Outline
• Basic Concept
• Scheduling Criteria
• Scheduling Algorithm
Process Scheduling
• The operating system is responsible for managing the scheduling
activities.
• A uniprocessor system can have only one running process at a time
• The main memory cannot always accommodate all processes at run-time
• The operating system will need to decide on which process to execute next
(CPU scheduling), and which processes will be brought to the main memory
(job scheduling)
Process Scheduling Queues
• Ready queue – set of all processes residing in main memory, ready and
waiting for CPU.
Ready Running
Timeout
Event
occurs Event wait
Waiting
Non-preemptive vs. Preemptive Scheduling
• Under non-preemptive scheduling, each running process keeps the
CPU until it completes or it switches to the waiting (blocked) state
(points 2 and 5 from previous slides).
0 24 27 30
0 24 27 30
0 3 6 30
• Turnaround Time for P1 = 30; P2 = 3; P3 = 6
• Average Turnaround time: (30+3+6)/3 = 13ms
• Problems:
• Convoy effect (short processes behind long processes)
• Non-preemptive -- not suitable for time-sharing systems
FCFS Scheduling (Cont.)
• Suppose that the processes arrive in the order P2 , P3 , P1
Waiting Time = Turnaround Time – Burst Time
0 3 6 30
0 3 7
Example for Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
• P3 gets the CPU first since it is the shortest. P2 then P4 get the CPU in turn (based on arrival time)
• Turnaround Time for process p1= 7, p2= 10. p3=4, p4=11
• Average Turnaround time : (7+10+4+11)/4 = 8ms
Example for Non-Preemptive SJF
P1 P3 P2 P4
0 3 7 8 12 16
P1 P2 P3
0 2 4 5
Example for Preemptive SJF (SRTF)
P1 P2 P3 P2 P4 P1
2 4 5 7 11 16
Example for Preemptive SJF (SRTF)
P1 P2 P3 P2 P4 P1
2 4 5 7 11 16
P1 P2 P3 P2 P4 P1
2 4 5 7 11 16
• Turnaround Time p1=16 ,p2=5,p3=1,p4=6
• Waiting Time p1=9 ,p2=1,p3=0,p4=2
• Average waiting time time = (9 + 1 + 0 +2)/4 = 3ms
Priority-Based Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority
(smallest integer highest priority).
• When a process arrives at the ready queue,
• the priority is compared with priority of the current running process.
• It can be
• pre-emptive
• non pre-emptive
Priority-Based Scheduling (cont…)
Scenario:
If a newly arrived process has a higher priority than the currently running
process.
Characteristics:
• Preemptive Priority Scheduling Algorithm:
• The CPU is preempted, and the currently running process is moved to the ready
queue.
• The newly arrived process is then scheduled for execution.
• Non-Preemptive Priority Scheduling Algorithm:
• The newly arrived process is placed at the tail of the ready queue.
• The currently running process continues execution until it finishes, after which the
scheduler picks the next process.
Priority-Based Scheduling (cont…)
• SJF is a special case of priority scheduling:
• process priority = the inverse of remaining CPU time
• The larger the CPU burst, the lower the priority and vice versa
• Gantt Chart
• Gantt Chart
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
• If the quantum is very short, then short processes will move through the system
relatively quickly.
• On the other hand, there is processing over head involved in handling the clock
interrupt and performing the scheduling and dispatching function.
• Thus, very short time quanta should be avoided.
Effect of time quanta
• When the time quantum is greater than the typical interaction time, it
means that each process gets more time to execute before being
switched out.
• This can lead to fewer context switches, which might improve
efficiency for CPU-bound processes but could also increase response
time for interactive processes.
Choosing a Time Quantum