Scheduling Copyright Nahrstedt Angrave Abdelzaher 1 Copyright Nahrstedt
Scheduling Copyright ©: Nahrstedt, Angrave, Abdelzaher 1
Copyright ©: Nahrstedt, Angrave, Abdelzaher Content of This Lecture n n Why CPU scheduling? Basic scheduling algorithms n n n FIFO (FCFS) Shortest job first Round Robin Priority Scheduling Goals: n Understand how your program is executed on the machine together with other programs 2
Copyright ©: Nahrstedt, Angrave, Abdelzaher Review: State Process Model 3
Copyright ©: Nahrstedt, Angrave, Abdelzaher OS Representation of Process via Process Control Block (PCB) 4
Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Scheduling n Deciding which process/thread should occupy the resource (CPU, disk, etc) CPU I want to play Process 1 Whose turn is it? Process 2 Process 3 5
Copyright ©: Nahrstedt, Angrave, Abdelzaher Context Switch n n n Switch CPU from one process to another Performed by scheduler It includes: n n n Context switch is expensive (1 -1000 microseconds) n n n save PCB state of the old process; load PCB state of the new process; Flush memory cache; Change memory mapping (TLB); No useful work is done (pure overhead) Can become a bottleneck Need hardware support 6
Copyright ©: Nahrstedt, Angrave, Abdelzaher When should the scheduler be called? n n n A new process is admitted The running process exits The running process is blocked I/O interrupt (some processes will be ready) Clock interrupt (every 10 milliseconds) 7
Copyright ©: Nahrstedt, Angrave, Abdelzaher Preemptive vs. Nonpreemptive scheduling n Non-preemptive scheduling: n The running process keeps the CPU until it voluntarily gives up the CPU n n process exits switches to blocked state Transition 3 is only voluntary Preemptive scheduling: n Running 3 Ready 4 Terminated 1 Blocked The running process can be interrupted and must release the CPU (can be forced to give up CPU) 8
Copyright ©: Nahrstedt, Angrave, Abdelzaher Scheduling Objectives n n n Fairness (equitable shares of CPU) Priority (highest priority first) Efficiency (make best use of equipment) Encouraging good behavior (can’t take advantage of the system) Support for heavy loads (degrade gracefully) Adapting to different environments (interactive, real-time, multi-media) 9
Copyright ©: Nahrstedt, Angrave, Abdelzaher Performance Criteria n n n Efficiency: keep resources as busy as possible Throughput: # of processes that complete in unit time Waiting Time n n Initial Waiting Time n n amount of time from when a job is admitted until it completes Proportionality: n n Amount of time spent by the process waiting in ready queue before it starts executing Response Time n n Total amount of time spent by the process waiting in ready queue Assign CPU proportionally to given application weight Meeting Deadlines: n example, complete by 5 pm Dec. 15 th 2009! 10
Copyright ©: Nahrstedt, Angrave, Abdelzaher Process Profiles n I/O – Bound n n CPU – Bound n n Does too much I/O to keep CPU busy Does too much computation to keep I/O busy Process Mix n n Scheduling should load balance between I/O bound and CPU-bound processes Ideal would be to run all equipment at 100% utilization but that would not necessarily be good for response time 11
Copyright ©: Nahrstedt, Angrave, Abdelzaher Simple Processor Scheduling Algorithms n Batch systems n n n First Come First Serve (FCFS) Shortest Job First Interactive Systems n n n Round Robin Priority Scheduling … 12
Copyright ©: Nahrstedt, Angrave, Abdelzaher First Come First Serve (FCFS) n n Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Used in Batch Systems Implementation n n FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue. Quiz: Is it Preemptive or Non-preemptive? 13
Copyright ©: Nahrstedt, Angrave, Abdelzaher First Come First Serve (FCFS) n n Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Used in Batch Systems Implementation n n FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue. Quiz: Is it Preemptive or Non-preemptive? once a process has CPU, it runs to completion (non-preemptive)! 14
Copyright ©: Nahrstedt, Angrave, Abdelzaher FCFS Example Process Duration P 1 24 P 2 3 The final schedule: P 3 4 P 1 (24) 0 P 1 waiting time: 0 P 2 waiting time: 24 P 3 waiting time: 27 Order 1 2 3 Arrival Time 0 0 0 P 2 (3) P 3 (4) 24 27 The average waiting time: (0+24+27)/3 = 17 Does the execution order affect the average waiting time? 15
Copyright ©: Nahrstedt, Angrave, Abdelzaher Problems with FCFS n n n Non-preemptive Does not minimize AWT Cannot utilize resources in parallel: n n n Assume 1 process CPU bounded and many I/O bounded processes result: Convoy effect, low CPU and I/O Device utilization Why? 16
Why Convoy Effects with FCFS? Copyright ©: Nahrstedt, Angrave, Abdelzaher Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound: n 1. 2. 3. 4. 5. I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O. CPU bound job arrives at head of queue and executes until completion. I/O bound jobs re-join ready queue and wait for CPU bound job to complete. I/O devices idle until CPU bound job completes. When CPU bound job completes, the ready I/Obounded processes quickly move through the running state and become blocked on I/O events again. CPU becomes idle. 17
Copyright ©: Nahrstedt, Angrave, Abdelzaher Interactive Scheduling Algorithms n Usually preemptive n n n Performance Criteria n n n Time is sliced into quanta (time intervals) Scheduling decision is also made at the beginning of each quantum Average response time Average initial waiting time Average waiting time Fairness (or proportional resource allocation) Representative algorithms: n n n Round-robin Priority-based … 18
Copyright ©: Nahrstedt, Angrave, Abdelzaher Round-robin n n One of the oldest, simplest, most commonly used scheduling algorithm Select process/thread from ready queue in a round-robin fashion (take turns) Problems: • Does not consider priority • Context switch overhead Preemption after quantum expiration 19
Copyright ©: Nahrstedt, Angrave, Abdelzaher Round-robin: Example Process Duration Order Arrival Time P 1 3 1 P 2 4 2 3 never block Suppose. P 3 time quantum is: 13 unit, P 1, P 2 & P 3 P 1 P 2 P 3 0 0 P 1 P 2 P 3 P 2 P 1 waiting time: 4 P 2 waiting time: 6 P 3 waiting time: 6 10 The average waiting time (AWT): (4+6+6)/3 = 5. 33 Note that Initial Waiting Time of P 1 is zero, P 2’s IWT is 1, and P 3’s IWT is 2 20
Copyright ©: Nahrstedt, Angrave, Abdelzaher Choosing the Time Quantum n Time slice too large n n n Time slice too small n n n Too many context switches (overheads) Inefficient CPU utilization Heuristic: n n FIFO behavior Poor initial waiting time 70 -80% of jobs block within time-slice Typical time-slice n 10 -100 ms (depends on job priority) 21
Copyright ©: Nahrstedt, Angrave, Abdelzaher Shortest Job First (SJF) n n n Schedule the job with the shortest computation time first Scheduling in Batch Systems Two types: n n n Non-preemptive Preemptive Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time) 22
Copyright ©: Nahrstedt, Angrave, Abdelzaher Non-preemptive SJF: Example Process P 1 P 2 P 3 P 4 (3) 0 Duration 6 8 7 3 P 1 (6) 3 P 4 waiting time: 0 P 1 waiting time: 3 P 3 waiting time: 9 P 2 waiting time: 16 9 Order 1 2 3 Arrival Time 0 0 0 P 3 (7)4 P 2 0 (8) 16 24 The total time is: 24 The average waiting time (AWT): (0+3+9+16)/4 = 7 23
Copyright ©: Nahrstedt, Angrave, Abdelzaher Comparing to FCFS Process P 1 P 2 P 3 Duration 6 8 7 Order 1 2 3 Arrival Time 0 0 0 P 4 3 4 0 24
Copyright ©: Nahrstedt, Angrave, Abdelzaher Comparing to FCFS Process P 1 P 2 P 3 Duration 6 8 7 P 1 P 4 (6) 0 Order 1 2 3 P 2 (8)3 6 P 1 waiting time: 0 P 2 waiting time: 6 P 3 waiting time: 14 P 4 waiting time: 21 4 14 Arrival Time 0 0 0 P 3 (7) 0 P 4 (3) 21 24 The total time is the same. The average waiting time (AWT): (0+6+14+21)/4 = 10. 25 (comparing to 7) 25
Copyright ©: Nahrstedt, Angrave, Abdelzaher Preemptive SJF n n Shortest job runs first. A job that arrives and is shorter than the running job will preempt it 26
Copyright ©: Nahrstedt, Angrave, Abdelzaher A Problem with Preemptive SJF n Starvation n n A job may keep getting preempted by shorter ones Example n n Process A with computation time of 1 hour arrives at time 0 But every 1 minute, a short process with computation time of 2 minutes arrives Result of SJF: A never gets to run What’s the difference between starvation and a deadlock? 27
- Slides: 27