Title: Operating Systems Principles Process Management and Coordination Lecture 5: Process and Thread Sched
1Operating Systems PrinciplesProcess Management
and CoordinationLecture 5Process and Thread
Scheduling
2Content
- Organization of Schedulers
- Embedded and Autonomous Schedulers
- Priority Scheduling
- Scheduling Methods
- A Framework for Scheduling
- Common Scheduling Algorithms
- Comparison of Methods
- Priority Inversion
3Operating Systems PrinciplesProcess Management
and CoordinationLecture 5Process and Thread
Scheduling
- Organization of Schedulers
4Process Scheduling/Dispatching
We use scheduling to refer to both.
- Process scheduling
- Long term scheduling
- Move process to Ready List (RL) after creation
(When and in which order?) - Process Dispatching
- Short term scheduling
- Select process from Ready List to run
5Organization of Schedulers
- Embedded
- Called as function at end of kernel call
- Runs as part of calling process
- Autonomous
- Separate process
- May have dedicated CPU on a multiprocessor
- On single-processor,run at every
quantumscheduler and other processes alternate
pi process S scheduler
6Organization of Schedulers
- Embedded
- Called as function at end of kernel call
- Runs as part of calling process
- Autonomous
- Separate process
- May have dedicated CPU on a multiprocessor
- On single-processor,run at every
quantumscheduler and other processes alternate
S
S
S
S
Windows 2000
OS
Unix
S
7Priority Scheduling
Who runs next?
Based on priority
- Priority function returns numerical value P for
process p - P Priority(p)
- Static priority unchanged for lifetime of p
- Dynamic priority changes at runtime
8Priority-Leveled Processes
- Priority divides processes into levels
- Implemented as multilevel ready list, say, RL
- p at RLi run before q at RLj if i gt j
- p, q at same level are ordered by other criteria,
e.g., the order of arrival.
RLn
9The Processes in Ready List
Status.Type ? running, ready_a, ready_s
10The Scheduler
11The Scheduler
- The scheduler may be invoked
- periodically
- when some events affect the priorities of
existing processes, e.g., - a process is blocked/suspended
- a process is destroyed
- a new process created
- a process is activated
12The Scheduler
The scheduler/dispatcher should always keep the
np (processors) highest-priority active
processes running.
Some processes in RL can get CPU.
- The scheduler may be invoked
- periodically
- when some events affect the priorities of
existing processes, e.g., - a process is blocked/suspended
- a process is destroyed
- a new process created
- a process is activated
Running processes may be preempted.
13Invoking the Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
14Invoking the Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
15An Embedded Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
Scheduler() do Find highest priority
ready_a process p Find a free cpu
if (cpu ! NIL) Allocate_CPU(p,cpu) while
(cpu ! NIL) do Find highest
priority ready_a process p Find lowest
priority running process q if (Priority(p)
gt Priority(q)) Preempt(p,q) while
(Priority(p) gt Priority(q)) if
(self-gtStatus.Type!running) Preempt(p,self)
If free CPUs are available, allocate these free
CPUs to high-priority processes in ready_a state
To preempt low-priority running processes by
high-priority ready_a ones.
If the caller is going to be idled, preempt
itself.
16An Embedded Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
Scheduler() do Find highest priority
ready_a process p Find a free cpu
if (cpu ! NIL) Allocate_CPU(p,cpu) while
(cpu ! NIL) do Find highest
priority ready_a process p Find lowest
priority running process q if (Priority(p)
gt Priority(q)) Preempt(p,q) while
(Priority(p) gt Priority(q)) if
(self-gtStatus.Type!running) Preempt(p,self)
To preempt low-priority running processes by
high-priority ready_a ones.
If the caller is going to be idled, preempt
itself.
17An Embedded Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
Scheduler() do Find highest priority
ready_a process p Find a free cpu
if (cpu ! NIL) Allocate_CPU(p,cpu) while
(cpu ! NIL) do Find highest
priority ready_a process p Find lowest
priority running process q if (Priority(p)
gt Priority(q)) Preempt(p,q) while
(Priority(p) gt Priority(q)) if
(self-gtStatus.Type!running) Preempt(p,self)
If the caller is going to be idled, preempt
itself.
18An Embedded Scheduler
- The caller to the scheduler may be a process to
be - blocked/suspended or
- awakened/activated
Scheduler() do Find highest priority
ready_a process p Find a free cpu
if (cpu ! NIL) Allocate_CPU(p,cpu) while
(cpu ! NIL) do Find highest
priority ready_a process p Find lowest
priority running process q if (Priority(p)
gt Priority(q)) Preempt(p,q) while
(Priority(p) gt Priority(q)) if
(self-gtStatus.Type!running) Preempt(p,self)
19An Embedded Scheduler
How to simplify the scheduler for a uniprocessor
system?
Scheduler() do Find highest priority
ready_a process p Find a free cpu
if (cpu ! NIL) Allocate_CPU(p,cpu) while
(cpu ! NIL) do Find highest
priority ready_a process p Find lowest
priority running process q if (Priority(p)
gt Priority(q)) Preempt(p,q) while
(Priority(p) gt Priority(q)) if
(self-gtStatus.Type!running) Preempt(p,self)
20Operating Systems PrinciplesProcess Management
and CoordinationLecture 5Process and Thread
Scheduling
21Scheduling Policy
- Scheduling based on certain criteria.
- For examples
- batch jobs vs. interactive users
- System processes vs. user processes
- I/O bound processes vs. CPU bound processes
22The General Framework
- Decision mode
- Priority function
- Arbitration Rule
A scheduling policy is determined by
- When to schedule?
- Preemptive
- Nonpreemptive
- Who to schedule?
- Priority evaluation
- Arbitration to break ties
Decision Mode
23The Decision Modes
- Decision mode
- Priority function
- Arbitration Rule
A scheduling policy is determined by
- Preemptive scheduler called
- periodically (quantum-oriented) or
- when system state changes
- More costly
- Nonpreemptive scheduler called
- when process terminates or blocks
- Not adequate in real-time or time-shared systems
24The Priority Function
- Decision mode
- Priority function
- Arbitration Rule
A scheduling policy is determined by
- Possible parameters
- Attained service time (a)
- Real time in system (r)
- Total service time (t)
- Period (d)
- Deadline (explicit or implied by period)
- External priority (e)
- Memory requirements (mostly for batch)
- System load (not process-specific)
25The Arbitration Rules
- Decision mode
- Priority function
- Arbitration Rule
A scheduling policy is determined by
- Break ties among processes of the same priority
- Random
- Chronological (First In First Out FIFO)
- Cyclic (Round Robin RR)
26Common Scheduling Algorithms
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
27Example Processesfor Scheduling
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
28FIFO
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
29FIFO
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
30SJF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
31SJF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
32SRT
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
33SRT
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
34RR
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
Assume a time-quantum of 0.1 time units.
35RR
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
Assume a time-quantum of 0.1 time units.
36ML
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
37ML
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
Each process has a fixed priority.
38ML (Nonpreemptive)
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
Each process has a fixed priority.
39ML (Preemptive)
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
Each process has a fixed priority.
40ML (Nonpreemptive)
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
ep1 15
ep2 14
41MLF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
- Like ML, but priority changes dynamically
- Every process enters at highest level n
- Each level P prescribes maximum time tP
- tP increases as P decreases
- Typically
- tn T (constant)
- tP 2 ? tP1
42MLF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
a attained service time
- The priority of a process is a function of a.
- Find P for given a
- priority attained time
- n altT
- n 1 altT2T
- n 2 altT2T4T
- . . . . . .
- n i alt(2i11)T
P n i n ?lg2(a/T1)?
43MLF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
a attained service time
- The priority of a process is a function of a.
- Find P for given a
- priority attained time
- n altT
- n 1 altT2T
- n 2 altT2T4T
- . . . . . .
- n i alt(2i11)T
P n i n ?lg2(a/T1)?
44RM EDF
- First-In/First-Out (FIFO)
- Shortest-Job-First (SJF)
- Shortest-Remaining-Time (SRT)
- Round-Robin (RR)
- Multilevel Priority (ML)
- Multilevel Feedback (MLF)
- Rate Monotonic (RM)
- Earliest Deadline (EDF)
- RM
- Intended for periodic (real-time) processes
- Preemptive
- Highest priority shortest period P d
- EDF
- Intended for periodic (real-time) processes
- Preemptive
- Highest priority shortest time to next deadline
- r ? d number of completed periods
- r d time in current period
- d r d time remaining in current period
- P (d r d)
45Common Scheduling Algorithms
46Comparison of Methods
- FIFO, SJF, SRT Primarily for batch systems
- FIFO is the simplest
- SJF SRT have better average turnaround times
-
ri the real time that the ith process spends
in the system.
47Example
48Comparison of Methods
- Time-sharing systems
- Response time is critical
- RR or MLF with RR within each queue are suitable
49Comparison of Methods
- Time-sharing systems
- Response time is critical
- RR or MLF with RR within each queue are suitable
- Choice of quantum determines overhead
- When q ? ?, RR approaches FIFO
- When q ? 0, context switch overhead ? 100
- When q gtgt context switch overhead,n processes
run concurrently at 1/n CPU speed
50Interactive Systems
- Most dynamic schemes tend to move interactive and
I/O-bound processes to the top of the priority
queues and to let CPU-bound processes drift to
lower levels. - MLF puts I/O-completed processes into highest
priority queue.
51Comparison of Methods
- Real-time systems
- Feasible All deadlines are met
- CPU utilization is defined as U? ti/di
- Schedule is feasible if U ? 1
- EDF always yields feasible schedule (if U ? 1)
- RM yields feasible schedule if U ? 0.7
52Example RM and EDF
CPU Utilization
53Example RM and EDF
CPU Utilization
?
fail
54Operating Systems PrinciplesProcess Management
and CoordinationLecture 5Process and Thread
Scheduling
55Priority Inversion Problem
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
56Priority Inversion Problem
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
57Priority Inversion Problem
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
?
?
?
?
?
?
?
?
?
?
?
?
?
?
58Priority Inversion Problem
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
?
?
?
?
?
?
?
(Unrelated) p2 may delay p1 indefinitely.
?
?
?
?
?
?
?
59Solutions
- Make CSs nonpreemptable
- Practical is CSs are short and few
- Unsatisfactory if high-priority process often
found themselves waiting for lower-priority
process, especially unrelated ones. - Naïve solution Always run CS at priority of
highest process that shares the CS - Problem p1 cannot preempt lower-priority process
inside CS, even when it does not try to enter CS
-- a different form of priority inversion. - Dynamic Priority Inheritance
- See Next
60Dynamic Priority Inheritance
Sha, Rajkumar, and Lehocsky 1990
- p3 is in its CS
- p1 attempts to enter its CS
- p3 inherits p1s (higher) priority for the
duration of CS
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
61Dynamic Priority Inheritance
- p3 is in its CS
- p1 attempts to enter its CS
- p3 inherits p1s (higher) priority for the
duration of CS
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
62Dynamic Priority Inheritance
- p3 is in its CS
- p1 attempts to enter its CS
- p3 inherits p1s (higher) priority for the
duration of CS
?
?
?
?
?
?
?
- Assume priority order p1 gt p2 gt p3.
- p1 and p3 share common resource.
- p2 is independent of p1 and p3 .
?
?
?
?
?
?
?
63References
- Rensselaer Polytechnic Institute
http//www.cs.rpi.edu/academics/courses/fall04/os/
c8/ - University of east London
- Inside the Windows NT Scheduler, Part 1
- Inside the Windows NT Scheduler, Part 2
64Reading Assignment
- Chapter 6 CPU Scheduling in
- Operating System Concepts, Seventh Edition, by
Abraham Silberschatz, Peter Baer Galvin, Greg
Gagne, John Wiley Sons, Inc.