CPU Scheduling
CPU Scheduling
CPU Scheduling
• processes are managed through the use of multiple queues (or lists) of PCB's; the
word queue (in an OS context) has a loose interpretation
• the job queue contains all jobs submitted to the system, but not yet in main memory
• the ready queue contains all jobs in main memory ready to execute
• each I/O device has a queue of jobs waiting for various I/O operations
• a process is dispatched from the ready queue to the CPU; its processing may cause it
to be put on a device queue
• all of these events are signaled by interrupts
• job scheduling versus process scheduling (or CPU scheduling)
• here we are primarily discussing process scheduling
Process scheduling
Preemptive scheduling
Jyoti Verma 1
CPU SCHEDULING
• waiting time: amount of time a process has been waiting in the ready queue
(minimize)
• response time: amount of time it takes from when a request is submitted to when the
response is produced (minimize); does not include the time for a response to be
output
• Some work is being done to minimize response time variance, to promote
predictability.
CPU Scheduling Algorithms
Jyoti Verma 2
CPU SCHEDULING
another example:
Priority Scheduling
Jyoti Verma 3
CPU SCHEDULING
• associate a priority with each process, allocate the CPU to the process with the highest
priority
• any 2 processes with the same priority are handled FCFS
• SJF is a version of priority scheduling where the priority is defined using the predicted
CPU burst length
• priorities are usually numeric over a range
• high numbers may indicate low priority (system dependent)
• internal (process-based) priorities: time limits, memory requirements, resources
needed, burst ratio
• external (often political) priorities: importance, source (e.g., faculty, student)
• priority scheduling can be non-preemptive or preemptive
• problem: starvation --- low priority processes may never execute because they are
waiting indefinitely for the CPU
• a solution: aging --- increase the priority of a process as time progresses
Priority Scheduling example
Gantt chart:
• associate with each process the length of its next CPU burst
• schedule the process with the shortest time
• two schemes
• non-preemptive: once scheduled, a process continues until the end of its CPU burst
• preemptive: preempt if a new process arrives with a CPU burst of less length than the
remaining time of the currently executing process; known as the Shortest Remaining
Time First (SRTF) algorithm
• SJF is provably optimal; it yields a minimum average waiting time for any set of
processes however, we cannot always predict the SJF (non-preemptive) examples
Jyoti Verma 4
CPU SCHEDULING
example 1:
Gantt chart:
Jyoti Verma 5
CPU SCHEDULING
example 2:
Gantt chart:
Jyoti Verma 6
CPU SCHEDULING
Jyoti Verma 7
CPU SCHEDULING
Round Robin
• time sharing (preemptive) scheduler where each process is given access to the CPU
for 1 time quantum (slice) (e.g., 20 milliseconds)
• a process may block itself before its time slice expires
• if it uses its entire time slice, it is then preempted and put at the end of the ready
queue
• the ready queue is managed as a FIFO queue and treated as a circular
• if there are n processes on the ready queue and the time quantum is q, then each
process gets 1/n time on the CPU in chunks of at most q time units
• no process waits for more than (n-1)q time units
• the choice of how big to make the time slice (q) is extremely important
• if q is very large, Round Robin degenerates into FCFS
• if q is very small, the context switch overhead defeats the benefits
example 1 (q = 20):
Gantt chart:
Jyoti Verma 8
CPU SCHEDULING
3 68 0 37 94 162 162
4 24 0 57 97 121 121
waiting times:
p2: (20-0) = 20
Example
Multilevel queue scheduling algorithm with five queues, listed below in order of
priority:
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes
Jyoti Verma 9
CPU SCHEDULING
Jyoti Verma 10
CPU SCHEDULING
queues.
• Also if a process waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation.
Example
Queue-0
Queue-1
Queue-2
Jyoti Verma 11