Download presentation
Presentation is loading. Please wait.
Published byBuck Payne Modified over 9 years ago
1
CPU Scheduling Chapter 6 Advanced Operating System
2
2 CPU Scheduling What is CPU Scheduling? Determining which processes run when there are multiple run able processes Why is it important? It can have a big effect on resource utilization and the overall performance of the system
3
3 CPU Scheduling Long-term scheduling Deals with creating a new process Controls degree of multiprogramming May be FCFS or priority based Interactive systems tend to accept users unless the system is swamped Medium-term scheduling Deals with swapping processes in/out Short-term scheduling What process should we run next? Invoked on: clock or I/O interrupt system call, signal
4
4 Short-term Criteria Criteria Response Time – Start to first output Important in interactive systems Turnaround Time – Start to finish Deadlines Predictability – Time doesn’t depend on what else is going on Throughput – Jobs finished/unit time Processor Utilization Fairness – Processes treated equally Enforcing Priorities Balancing Resources – Try to keep all system resources busy
5
5 How do processes behave? CPU/IO burst cycle A process will run for a while (the CPU burst), perform some IO (the IO burst), then run for a while more (the next CPU burst) IO Bound processes Processes that perform lots of IO operations. Each IO operation is followed by a short CPU burst to process the IO, then more IO happens CPU bound processes Processes that perform lots of computation and do little IO. Tend to have a few long CPU bursts
6
6 How do processes behave? Scheduler will typically do switch the CPU to another process when one process does IO The IO will take a long time, and don't want to leave the CPU idle while wait for the IO to finish What are possible process states? Running - process is running on CPU Ready - ready to run, but not actually running on the CPU Waiting - waiting for some event like IO to happen.
7
7 Scheduling Priorities Are some processes more important than others? Don’t want to starve low-priority processes Decision Mode Will we suspend the currently active process if it can continue? No:Nonpreemptive Yes:Preemptive Some systems (Win 3.1, early Mac) used cooperative multitasking (processes voluntarily give up the CPU) Preemption incurs more O.S. overhead, but prevents monopolizing the processor Also helps with infinite loops
8
8 How to evaluate scheduling algorithm? CPU Utilization: Keep CPU utilization as high as possible. Throughput: number of processes completed per unit time. Turnaround Time: mean time from submission to completion of process. Waiting Time: Amount of time spent ready to run but not running. Response Time: Time between submission of requests and first response to the request. Scheduler Efficiency: The scheduler doesn't perform any useful work, so any time it takes is pure overhead. So, need to make the scheduler very efficient.
9
9 First Come First Served Processes queued in order of arrival Runs until finished or blocks on I/O Tends to penalize short processes Have to wait for earlier long processes Favors processor-bound processes I/O processes block quickly
10
10 First Come First Served Process Burst Time P 1 24 P 2 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 P1P1 P2P2 P3P3 2427300
11
11 First Come First Served Suppose that the processes arrive in the order P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0 ; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P2 63300
12
12 Shortest-Job-First (SJF) Shortest-Job-First (SJF) can eliminate some of the variance in Waiting and Turnaround time. It is optimal with respect to average waiting time. Big problem: how does scheduler figure out how long will it take the process to run?
13
13 Shortest-Job-First (SJF) Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time SJF is optimal – gives minimum average waiting time for a given set of processes Two schemes: nonpreemptive preemptive
14
14 Shortest-Job-First (SJF) Preemptive scheduler Reruns scheduling decision when process becomes ready. if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. If the new process has priority over running process, the CPU preempts the running process and executes the new process. Non-preemptive scheduler Only does scheduling decision when running process voluntarily gives up CPU. once CPU given to the process it cannot be preempted until completes its CPU burst It allows every running process to finish its CPU burst.
15
15 Shortest-Job-First (SJF) Shortest Process Next Select process with shortest expected running time (nonpreemptive) Difficult to estimate required time Can estimate from previous runs Tends to be less predictable Can starve long processes Short processes may still wait if a long process has just started Shortest Remaining Time Preemptive version of Shortest Process Next May switch processes when a new process arrives Still may starve long processes
16
16 Non-preemptive ProcessArrival TimeBurst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 = 4 P1P1 P3P3 P2P2 73160 P4P4 812
17
17 Preemptive Process Arrival TimeBurst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P 4 5.0 4 SJF (preemptive) Average waiting time = (9 + 1 + 0 +2)/4 = 3 P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16
18
18 Priority Scheduling Each process is given a priority, then CPU executes process with highest priority. If multiple processes with same priority are runnable, use some other criteria - typically FCFS. SJF is an example of a priority-based scheduling algorithm. The priorities of a given process change over time.
19
19 Priority Scheduling Assume we have 5 processes P1 (burst time 10, priority 3) P2 (burst time 1, priority 1) P3 (burst time 2, priority 3) P4 (burst time 1, priority 4) P5 (burst time 5, priority 2) Lower numbers represent higher priorities. What would a standard priority scheduler do?
20
20 Priority Scheduling Big problem with priority scheduling algorithms: starvation or blocking of low-priority processes. Can use aging to prevent this - make the priority of a process go up the longer it stays runnable but isn't run.
21
21 Priority Scheduling What about interactive systems? Cannot just let any process run on the CPU until it gives it up - must give response to users in a reasonable time. Use an algorithm called round-robin scheduling. Similar to FCFS but with preemption. Have a time quantum or time slice. Let the first process in the queue run until it expires its quantum (i.e. runs for as long as the time quantum), then run the next process in the queue.
22
22 Round Robin (RR) Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. If there are n processes in the ready queue Time quantum is q, Each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units. Performance q large FIFO q small q must be large with respect to context switch, otherwise overhead is too high
23
23 Round Robin (RR) queue dispatch processor At timeout, process switching occurs. Each process is allowed to execute for at most a quantum.
24
24 Round Robin (RR) ProcessBurst Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: Typically, higher average turnaround than SJF, but better response P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162
25
25 Other Algorithms Highest Response Ratio Next Nonpreemptive, tries to get best average normalized turnaround time Depends on Response Ratio W = time spent waiting S = expected service time RR = (W + S) / S Select process with highest RR Feedback Starts in high-priority queue, moves down in priority as it executes Lower-priority queues often given longer time slices Can starve long processes Move up processes if they wait too long
26
26 Comparison P1: arrives at time 0, requires 3 units P2: arrives at time 2, requires 6 units P3: arrives at time 4, requires 4 units P4: arrives at time 6, requires 5 units P5: arrives at time 8, requires 2 units
27
27 Comparison Turnaround Time Finish Time-Arrival time Normalized Turnaround Time Turnaround Time/Service Time
28
28 Response Time If the system gets too slow to a user, they may slow down or abort the operation Must balance response time with the cost required Faster/more expensive hardware may be required Priorities that may penalize certain processes Ranges 15 seconds or greater Rules out conversational interaction. Most users will not wait this long If it cannot be avoided, allow the user to continue on to something else Foreground/background threads
29
29 Response Time 4 to 15 seconds Generally user loses track of the current operation in short-term memory Ok after end of a major closure 2 to 4 seconds Inhibits user concentration Bothers a user who is focused on the job Ok after end of a minor closure 1 to 2 seconds Important when information has to be remembered over several responses Important limit for terminal activities Less than 1 second For keeping user’s attention for thought-intensive activities Example: graphics Less than 1/10 second Response to key presses or mouse clicks
30
30 Response Time User productivity tends to improve with a more rapid response time Especially true for expert users Becomes very noticeable as response time drops below 1 second User time – “think time”, time user spends thinking about the response System time – Time system takes to generate its response Short response times are important User response time tends to decrease as system response time decreases Web Pages – Loading a page in 3 seconds or less increases the user’s attention User may abort after 10s or more
31
31 Multiprocessor Scheduling Brief Review Loosely Coupled (cluster) Separate main memory and I/O devices Functionally Specialized Dedicated I/O processors Tightly Coupled Share main memory, O.S.
32
32 Design Issues Assigning processes to processors Are processes/threads assigned to processors? Who handles the assignment? Master/Slave Single processor handles O.S. functions Tends to become a bottleneck Peer O.S. can run on any processor More complicated operating system Generally use simple schemes Overhead is a greater problem Threads add additional concerns Generally response time is more important than CPU utilization
33
33 Process Scheduling Response time is much less sensitive to the algorithm used Thread Scheduling Load Sharing Gang Scheduling Dedicated Processor Assignment Dynamic scheduling Load Sharing Select threads from a global queue Avoids idle processors Queue may be a bottleneck Doesn’t take advantage of local cache May not schedule interacting threads at the same time Widely used
34
34 Process Scheduling Gang Scheduling Schedule related threads on processors to run at the same time Less overhead since we schedule multiple processors at once Interacting threads are more likely to be running and ready to interact Have to allocate processors Dedicated Processor Assignment Like gang scheduling, but never switch between applications Best to have # threads = # processors Dynamic Scheduling Vary the number of threads Job uses the processors in its partition Appears better than gang scheduling
35
35 Real-time Scheduling Very common in embedded systems Require results be produced before specified deadlines Hard real-time: missed deadlines result in damage or death Soft real-time: missed deadlines may result in lower performance, but can be tolerated Most real-time systems are soft real-time
36
36 Real-Time Systems Features Fast process/thread switch Small size/minimal functionality Ability to respond to interrupts quickly Multitasking with IPC tools like semaphores, signals, and events Use of special sequential files that can accumulate data at a fast rate Preemptive scheduling with priority Minimize time with interrupts off Primitives to delay tasks for a fixed amount of time, pause/resume tasks Special alarms and timeouts Short-term scheduler is critical Need to have priorities, preemption
37
37 Deadline Scheduling Importance is meeting deadlines, not mere speed in handling events Use more task information: Ready Time (when it is ready to start) Starting Deadline (begin by …) Completion Deadline (finish by …) Processing Time (time to execute) Resource Requirements Priority (relative importance) Subtask structure Parts of a task may be optional Parts of a task may have lower priority Generally best to schedule task with the earliest deadline If we don’t allow preemption, we may need to let the CPU sit idle and wait for a task to arrive
38
38 Deadline Scheduling Execution profile of two periodic tasks Process A Arrives020 40… Execution Time10 1010 … End by20 4060 … Process B Arrives050 100… Execution Time25 2525 … End by50 100150 … Scheduling of two periodic tasks
39
39 Deadline Scheduling Execution profile of five aperiodic tasks Scheduling of aperiodic tasks (no preemption) Proce ss Arrival Time Executio n Time Starting Deadline A1020110 B20 C402050 D 2090 E602070
40
40 Deadline Scheduling
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.