Beruflich Dokumente
Kultur Dokumente
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 49
User-level Thread Scheduling
Different scheduling algorithms for threads/processes possible
No way to preempt user threads
Thread context switch much faster
Application-specific scheduling possible
Scheduling
within a
quantum
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 50
Kernel-level Thread Scheduling
Scheduler may prefer switches within same process
Context switch more expensive
A blocking thread does not block all process threads
Scheduling
within a
quantum
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 51
Scheduling: outline
Scheduling criteria
Scheduling algorithms
Unix scheduling
Linux Scheduling
Win NT scheduling
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 52
Scheduling in Unix
Two-level scheduling
Low level scheduler uses multiple queues to select the
next process, out of the processes in memory, to get a
time quantum.
High level scheduler moves processes from memory to
disk and back, to enable all processes their share of CPU
time
Low-level scheduler keeps queues for each priority
User mode processes have positive priorities
Kernel mode processes have negative priorities (which are
higher)
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 53
Unix priority queues
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 54
Unix low-level Scheduling Algorithm
Pick process from highest (non-empty) priority queue
Run for 1 quantum (usually 100 ms.), or until it
blocks
Increment CPU usage count every clock tick
Every second, recalculate priorities:
o Divide cpu usage by 2
o New priority = base + cpu_usage/2 + nice
o Base is negative if the process is released from waiting in
kernel mode
Use round robin for each queue (separately)
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 55
Unix low-level scheduling Algorithm - I/O
Blocked processes are removed from queue, but when the
blocking event occurs, are placed in a high priority queue
The negative priorities are meant to release processes
quickly from the kernel
Negative priorities are hardwired in the system, for example,
-5 for Disk I/O is meant to give high priority to a process
released from disk I/O
Interactive processes get good service, CPU bound processes
get whatever service is left...
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 56
Priority Calculation in Unix
P
j
(i) = Priority of process j at beginning of interval i
Base
j
= Base priority of process j
U
j
(i) = Processor utilization of process j in interval i
Gu
k
(i) = Total processor utilization of all processes in group k during
interval i
CPU
j
(i) = Exponentially weighted average processor utilization by
process j through interval i
GCPUk(I)= Exponentially weighted average total processor utilization of group k
through interval i
W
k
= Weighting assigned to group k, with the constraint that 0 W
k
1
and
2
) 1 (
2
) 1 (
) 1 (
2
) 1 (
2
) 1 (
) (
4
) 1 (
2
) 1 (
) (
i GCPU i GU
i GCPU
i CPU i U
i CPU
W
i GCPU
i CPU
Base i P
k k
k
j j
j
k
k
j
j j
k
k
w 1
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 57
Scheduling: outline
Scheduling criteria
Scheduling algorithms
Unix scheduling
Linux Scheduling
Win NT scheduling
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 58
Three classes of threads
Real-time FIFO
o Have highest priority, can only be preempted by a higher-priority
real-time FIFO thread
Real-time round robin
o Assigned time quantum
Timesharing
o Priorities 100-139
Priority determines the number of clock ticks (jiffys)
assigned to a round-robin or timesharing thread
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 59
Linux runqueues
Per-CPU runqueue
<>
Active
Expired
<>
Array[0]
Array[1]
Priority 0
Priority 139
T
Priority 0
Priority 139
T T T
T T
T T
T
T
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 60
Linux runqueues (cont'd)
Per-CPU runqueue
<>
Active
Expired
<>
Array[0]
Array[1]
Priority 0
Priority 139
T
Priority 0
Priority 139
T T T
T T
T T
T
T
Scheduler selects tasks from highest-priority active queue
o If time-slice expires moved to an expired list
o If blocked then, when event arrives, returned to active queue (with smaller
time-slice)
When no more active tasks swap active and expired pointers
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 61
Multiprocessor support
Runqueue per processor
Affinity scheduling
Perform periodic load-balancing between runqueues
o But in most cases, a process will run on the same process it ran
before.
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 62
Scheduling: outline
Scheduling criteria
Scheduling algorithms
Unix scheduling
Linux Scheduling
Win NT scheduling
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 63
WINDOWS NT Scheduling
Mapping of Win32 priorities to Windows 2000 priorities
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 64
WINDOWS NT Scheduling (II)
Windows 2000 supports 32 priorities for threads
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 65
Windows NT scheduling: dynamic changes
Priority is raised:
o Upon I/O completion (disk: 1, serial line: 2, keyboard: 6, sound card:
8)
o Upon event reception (semaphore, mutex): by 2 if in the
foreground process, by 1 otherwise
o If a thread waits `too much it is moved to priority 15 for two
quanta (for solving priority inversion see next slide)
Priority is decreased:
o By 1 if quantum is fully used
o Never bellow base value (normal thread priority)
5/7/2014
Operating Systems, 2009, Danny Hendler & Amnon Meisels 66
An example of priority inversion