Lecture 07
Lecture 07
Lecture 07
Process Scheduling
• Scheduling is a fundamental operating system
function
– Almost all computer system resources are schedule before
being used
– The CPU is the fundament resource that needs to be shared
• Process scheduling deals with selecting the next
process to execute on the CPU
• The goal is to obtain maximum CPU utilization
using multiprogramming
• Even though we say “process scheduling” most of
the discussion is equally relevant to threads
1
Process Schedulers
• Long-term scheduler (or job scheduler) selects which
processes should be brought into the ready queue
– Determines degree of multiprogramming
– Not invoked very often
– Does not exist in most timesharing systems
• Medium-term scheduler
– Swaps processes out to secondary storage
– We will cover this in a later lecture
• Short-term scheduler (or CPU scheduler) selects which
process should be executed next and allocates CPU
– Invoked frequently so it must be fast
– This lecture focuses on the short-term scheduler
Process Scheduling
• Processes fall into two categories
– I/O-bound processes spend more time doing I/O than
computations, short CPU bursts
– CPU-bound processes spends more time doing
computations, long CPU bursts
– The type and mixture of types of processes has an impact on
determining the best approach to process scheduling
• Process exhibit CPU–I/O burst cycle pattern
– Process execution consists of a cycle of CPU execution and
I/O waiting
• CPU burst is longer for CPU-bound processes
2
Process Scheduling
• As we already learned, the OS keeps track of processes
to be scheduled by maintaining various queues
– Ready queue is the set of all processes residing in main
memory, ready and waiting to execute
– Device queues are sets of processes waiting for a specific
I/O device
– Wait queues are sets of processes waiting for a specific event
• Processes migrate between the various queues as they
execute
• The process scheduler in interested in process on the
ready queue
Process Scheduling
• Selects from among the processes in memory that
are ready to execute, and allocates the CPU to one
of them
• CPU scheduling decisions take place when a
process
1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates
• Scheduling under 1 and 4 is non-preemptive
• Scheduling under 2 and 3 is preemptive
3
Process Dispatching
• Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler; this
involves
– Switching the context
– Switching to user mode
– Jumping to the proper location in the user program to restart
that program
• Dispatch latency is the time it takes for the
dispatcher to stop one process and start another
running
4
Optimizing Process Scheduling Criteria
• Maximum CPU utilization
• Maximum throughput
• Minimum turnaround time
• Minimum waiting time
• Minimum response time
5
Process Scheduling Algorithms
• First-come, first-serve
– The simplest to understand and the simplest to implement
– The CPU is allocated to processes as they arrive
– Processes keep the CPU until they are done with it
• This is a non-preemptive algorithm
– This is essentially a FIFO queue (i.e., first-in, first-out)
– Because of its simplicity, FCFS is not very efficient
P1 P2 P3
0 24 27 30
6
Process Scheduling Algorithms
• First-come, first-serve (con’t)
Suppose that the processes arrive in the order
P2 , P3 , P1
The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
7
Process Scheduling Algorithms
• Shortest-job-first (con’t)
Example: Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
SJF
P2 P3 P1
0 3 6 30
8
Process Scheduling Algorithms
• Shortest-job-first (con’t)
Example: Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
SJF (non-preemptive with arrival times)
P1 P3 P2 P4
0 3 7 8 12 16
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
9
Process Scheduling Algorithms
• Shortest-job-first (con’t)
– What is the real difficulty of SJF?
• Knowing the length of the next CPU request
– This is possible for long-term scheduling, but not so easy for
short-term scheduling
– CPU burst length approximation
• Try to predict the burst length
• Expect that the next burst will be similar to previous bursts
• CPU bursts can be predicted as an exponential average of the
lengths of previous CPU bursts
10
Process Scheduling Algorithms
• Priority
– A priority number (integer) is associated with each process
– The CPU is allocated to the process with the highest priority
(smallest integer ≡ highest priority)
• Preemptive
• Non-preemptive
– SJF is a priority scheduling where priority is the predicted
next CPU burst time
– Potential starvation problem
• Low priority processes may never get to execute
• One solution is process aging – as time progresses increase the
priority of the process that have not executed
11
Process Scheduling Algorithms
• Round-robin (con’t)
Example: Process Burst Time
P1 53
P2 17
P3 68
P4 24
The Gantt chart for these process where the quantum = 20 is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
12
Process Scheduling Algorithms
• Multilevel queue
– Ready queue is partitioned into separate queues, for
example: foreground (interactive) and background (batch)
– Each queue can have its own scheduling algorithm, such as
round-robin for foreground and FCFS for background
– Scheduling must be done between the queues
• Fixed or absolute priority scheduling (i.e., serve all from
foreground before any from background)
– Possibility of starvation
• Time slicing between queues (i.e., each queue gets a certain
amount of CPU time which it can schedule amongst its
processes)
– For example, 80% to foreground and 20% to background
13
Process Scheduling Algorithms
• Multilevel feedback-queue
– A process can move between the various queues; aging can
be implemented this way
– Multilevel feedback-queue scheduler defined by the
following parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine when to upgrade a process
• Method used to determine when to demote a process
• Method used to determine which queue a process will enter
when that process needs service
– This is the most general, but most complex algorithm
Q1
Q2
14
Process Scheduling Algorithms
• Multilevel feedback-queue (con’t)
– Three queues
• Q0 – time quantum 8 milliseconds round-robin
• Q1 – time quantum 16 milliseconds round-robin
• Q2 – FCFS
– Scheduling
• A new job enters queue Q0 which is served FCFS
• When it gains CPU, it receives 8 milliseconds
• If it does not finish in 8 milliseconds, it is moved to queue Q1
• At Q1 it is again served FCFS and receives 16 additional
milliseconds
• If it still does not complete, it is moved to queue Q2 where it is
run FCFS only if other queues are empty and is preempted by
the higher level queues
15
Process Scheduling Algorithms
• Scheduling on multiprocessor machines
– CPU scheduling more complex when multiple CPUs are
available
– Assume homogeneous processors within a multiprocessor
– Load sharing - providing a separate ready queue for each
processor
• Some processors could sit idle
– Symmetric multiprocessing (SMP) – each processor makes
its own scheduling decisions
– Asymmetric multiprocessing – only one processor accesses
the system data structures, alleviating the need for data
sharing
16