Beruflich Dokumente
Kultur Dokumente
Chapter 5
CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling UNIX example
Basic Concepts
Maximum CPU utilization obtained with multiprogramming CPUI/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait CPU times are generally much shorter than I/O times.
Schedulers
Process migrates among several queues
Device queue, job queue, ready queue
Short-term scheduler:
Select ready process to run on CPU Should be fast
Middle-term scheduler
Reduce multiprogramming or memory consumption
6
CPU Scheduler
CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state (by sleep). 2. Switches from running to ready state (by yield). 3. Switches from waiting to ready (by an interrupt). 4. Terminates (by exit). Scheduling under 1 and 4 is nonpreemptive. All other scheduling is preemptive.
Dispatcher
Dispatcher module gives control of the CPU to the process selected by the shortterm scheduler; this involves:
switching context switching to user mode jumping to the proper location in the user program to restart that program
Dispatch latency time it takes for the dispatcher to stop one process and start another running
8
Scheduling Criteria
CPU utilization keep the CPU as busy as possible Throughput # of processes that complete their execution per time unit Turnaround time (TAT) The interval of time of submission of a process to the time of completion i.e amount of time to execute a particular process Waiting time amount of time a process has been waiting in the ready queue Response time amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)
9
10
Algorithms
11
Problem:
Average waiting time depends on arrival order Troublesome for time-sharing systems Convoy effect short process behind long process
12
P2 0 3
P3 6
P1 30
SJF is optimal gives minimum average waiting time for a given set of processes
14
P3 8
P2 12
P4 16 P1s wating time = 0 P2s wating time = 6 P3s wating time = 3 P4s wating time = 7
15
17
t(n+1) is time of next burst. t(n) T(n) is time of current burst. is average of all previous bursts .
Priority 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 in Unix but lowest in Java). Preemptive Non-preemptive SJF is a priority scheduling where priority is the predicted next CPU burst time. Problem | Starvation low priority processes may never execute. Solution | Aging as time progresses increase the priority of the process.
19
Burst Time 53 17 68 24
P3(48)
40
17
P3(28) P3(8)
P4(4)
21
22
TAT can be improved if most process finish their next CPU burst in a single time quantum.
23
Multilevel Queue
Ready queue is partitioned into separate queues: EX: foreground (interactive) background (batch) Each queue has its own scheduling algorithm EX
foreground RR background FCFS
25
26
Scheduling
A new job enters queue Q0 which is served FCFS. When it gains CPU, job receives 8 milliseconds (RR). If it does not finish in 8 milliseconds, job is moved to queue Q1. At Q1 job is again served FCFS and receives 16 additional milliseconds (RR). If it still does not complete, it is preempted and moved to queue Q2. AT Q2 job is served FCFS
27
28
Multiple-Processor Scheduling
CPU scheduling more complex when multiple CPUs are available y Different rules for homogeneous or heterogeneous processors. Load sharing in the distribution of work, such that all processors have an equal amount to do. Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing Symmetric multiprocessing (SMP) each processor is self-scheduling
y Each processor can schedule from a common ready queue OR each one can use a separate ready queue.
29
Thread Scheduling
On operating system that support threads the kernel-threads (not processes) that are being scheduled by the operating system. Local Scheduling (process-contention-scope PCS ) How the threads library decides which thread to put onto an available LWP
PTHREAD_SCOPE_PROCESS
Global Scheduling (system-contention-scope SCS ) How the kernel decides which kernel thread to run next
PTHREAD_SCOPE_PROCESS
30
Linux Scheduling
Two algorithms: time-sharing and real-time Time-sharing
Prioritized credit-based process with most credits is scheduled next Credit subtracted when timer interrupt occurs When credit = 0, another process chosen When all processes have credit = 0, recrediting occurs
Based on factors including priority and history
Real-time
Defined by Posix.1b Real time Tasks assigned static priorities. All other tasks have dynamic (changeable) priorities.
31
32
33
Conclusion
Weve looked at a number of different scheduling algorithms. Which one works the best is application dependent. General purpose OS will use priority based, round robin, preemptive Real Time OS will use priority, no preemption.
34
References
Some slides from
Text book slides Kelvin Sung - University of Washington, Bothell Jerry Breecher - WPI Einar Vollset - Cornell University
35