Presentation is loading. Please wait.

Presentation is loading. Please wait.

University of Pennsylvania 10/5/00CSE 3801 CPU Scheduling CSE 380 Lecture Note 9 Insup Lee.

Similar presentations


Presentation on theme: "University of Pennsylvania 10/5/00CSE 3801 CPU Scheduling CSE 380 Lecture Note 9 Insup Lee."— Presentation transcript:

1 University of Pennsylvania 10/5/00CSE 3801 CPU Scheduling CSE 380 Lecture Note 9 Insup Lee

2 University of Pennsylvania 10/5/00CSE 3802 CPU SCHEDULING The basic problem is as follows: How can OS schedule the allocation of CPU cycles to processes in system, to achieve “good performance”? Components of CPU scheduling subsystem of OS: –Dispatcher - performs context switches –Scheduler - selects next process from those in main memory (short-term scheduler) –Swapper - manages transfer of processes between main memory and secondary storage (medium-term scheduler) –Long-Term Scheduler - in a batch system, determines which and how many jobs to admit into system.

3 University of Pennsylvania 10/5/00CSE 3803 Behavior of Processes in Execution Useful concept in context of scheduling: –CPU Burst - period of CPU time process is executing between I/O requests. The text also talks about “I/O burst”. Processes alternate between CPU & I/O bursts: –CPU I/O CPU I/O CPU... I/O CPU CPU-bound process uses CPU for long periods of time. I/O-bound process uses CPU briefly before it issues another I/O request. In interactive system, usually important to have fast response time. Accomplished by having scheduler favor I/O- bound processes. Necessary to determine as quickly as possible the nature (CPU-bound or I/O-bound) of a process, since usually not known in advance.

4 University of Pennsylvania 10/5/00CSE 3804 General Organization for CPU Scheduling Processes ready to use CPU are kept in a ready queue. Processes waiting for I/O are kept in wait queue associated with device on which they are performing I/O. Dispatcher: called from timer interrupt or when current process blocks for I/O performs a context switch to first process in ready queue Scheduler: called periodically, usually less often than dispatcher. Reorders ready queue to reflect changing priorities. Swapper: keeps track of “process mix'' in main memory. If CPU utilization not high enough, processes can be “swapped” between main memory and a swap area on secondary storage.

5 University of Pennsylvania 10/5/00CSE 3805 Types of Scheduling Algorithms Preemptive: process may have CPU taken away before completion of current CPU burst Non-preemptive: processes always run until CPU burst completes Static Priority Dynamic Priority

6 University of Pennsylvania 10/5/00CSE 3806 Performance Criteria for Scheduling Scheduling (as an Optimization task): How to best order the ready queue for efficiency purposes. CPU utilization: % of time CPU in use Throughput: # of jobs completed per time unit Turnaround Time: wall clock time required to complete a job Waiting Time: amount of time process is ready but waiting to run Response Time: in interactive systems, time until system responds to a command Response Ratio: (Turnaround Time)/(Execution Time) -- long jobs should wait longer The overhead of a scheduling algorithm (e.g., data kept about execution activity, queue management, context switches) should also be taken into account.

7 University of Pennsylvania 10/5/00CSE 3807 Kleinrock's Conservation Law No matter what scheduling algorithm is used, you cannot help one class of jobs without hurting the other ones. Example: A minor improvement for short jobs (say, on waiting time) causes a disproportionate degradation for long jobs.

8 University of Pennsylvania 10/5/00CSE 3808 Basic Scheduling Algorithm FCFS - First-Come, First-Served –Non-preemptive –Ready queue is a FIFO queue –Jobs arriving are placed at the end of queue –Dispatcher selects first job in queue and runs to completion of CPU burst Advantages: simple, low overhead Disadvantages: inappropriate for interactive systems, large fluctuations in average turnaround time are possible.

9 University of Pennsylvania 10/5/00CSE 3809 FCFS Example Pid Arr CPU Start Finish Turna Wait Ratio ---+---+---+-----+------+-----+----+----- A 0 3 0 3 3 0 1.0 B 1 5 3 8 7 2 1.4 C 3 2 8 10 7 5 3.5 D 9 5 10 15 6 1 1.2 E 12 5 15 20 8 3 1.6 ---+---+---+-----+------+-----+----+----- A 0 1 0 1 1 0 1.00 B 0 100 1 101 101 1 1.01 C 0 1 101 102 102 101 102.00 D 0 100 102 202 202 102 2.02

10 University of Pennsylvania 10/5/00CSE 38010 RR - Round Robin Preemptive version FCFS Treat ready queue as circular –arriving jobs are placed at end –dispatcher selects first job in queue and runs until completion of CPU burst, or until time quantum expires –if quantum expires, job is again placed at end

11 University of Pennsylvania 10/5/00CSE 38011 Properties of RR Advantages: simple, low overhead, works for interactive systems Disadvantages: if quantum too small, too much time wasted in context switching; if too large, approaches FCFS. Typical value: 10 - 100 msec Rule of thumb: choose quantum so that large majority (80- 90%) of jobs finish CPU burst in one quantum

12 University of Pennsylvania 10/5/00CSE 38012 SJF - Shortest Job First non-preemptive ready queue treated as a priority queue based on smallest CPU- time requirement –arriving jobs inserted at proper position in queue – dispatcher selects shortest job (1st in queue) and runs to completion Advantages: provably optimal w.r.t. average turnaround time Disadvantages: in general, unimplementable. Also, starvation possible! Can do it approximately: use exponential averaging to predict length of next CPU burst ==> pick shortest predicted burst next!

13 University of Pennsylvania 10/5/00CSE 38013 Exponential Averaging  n+1  t n  n  n+1 : predicted length of next CPU burst t n : actual length of last CPU burst  n : previous prediction  = 0 implies make no use of recent history  n+1  n   = 1 implies  n+1 = t n (past prediction not used).  = 1/2 implies weighted (older bursts get less and less weight).

14 University of Pennsylvania 10/5/00CSE 38014 SRTF - Shortest Remaining Time First Preemptive version of SJF Ready queue ordered on length of time till completion (shortest first) Arriving jobs inserted at proper position Dispatcher selects shortest job and runs to completion or until a job with a shorter remaining time arrives in the system.

15 University of Pennsylvania 10/5/00CSE 38015 Performance Evaluation Deterministic Modeling (vs. Probabilistic) Look at behavior of algorithm on a particular workload, and compute various performance criteria Example: workload - Job 1: 24 units Job 2: 3 units Job 3: 3 units Gantt chart for FCFS: | Job 1 | Job 2 | Job 3 | 0 24 27 30 Total waiting time: 0 + 24 + 27 = 51 Average waiting time: 51/3 = 17 Total turnaround time: 24 + 27 + 30 = 81 Average turnaround time: 81/3 = 9

16 University of Pennsylvania 10/5/00CSE 38016 RR and SJF Chart for RR with quantum of 3: | Job 1 | Job 2 | Job 3 | Job 1 | 0 3 6 9 30 Total waiting time: 6 + 6 + 3 = 15 Avg. waiting time: 15 / 3 = 5 Chart for SJF: | Job 2 | Job 3 | Job 1 | 0 3 6 30 Total waiting time: 6 + 0 + 3 = 9 Avg. waiting time: 9 / 3 = 3 Can see that SJF gives minimum waiting time. RR is intermediate. (This can be proved in general.)

17 University of Pennsylvania 10/5/00CSE 38017 HPF - Highest Priority First general class of algorithms each job assigned a priority which may change dynamically may be preemptive or non-preemptive Problem: how to compute priorities?

18 University of Pennsylvania 10/5/00CSE 38018 Multi-Level Feedback (FB) ·process enters (next) lower queue with each timer interrupt (penalized) ·bottom queue is standard Round Robin ·process in a given queue are not scheduled until all higher queues are empty

19 University of Pennsylvania 10/5/00CSE 38019 FB Discussion  I/O-bound processes tend to congregate in higher-level queues. (Why?)  Nice, since obtain greater device utilization  Quantum in top queue should be large enough to satisfy majority of I/O-bound processes Variations – Priority  can assign a process a lower priority by starting it at a lower-level queue  can raise priority by moving process to a higher queue  can use in conjunction with aging Adaptive  to adjust priority of a process changing from CPU-bound to I/O- bound, can move process to a higher queue each time it voluntarily relinquishes CPU.

20 University of Pennsylvania 10/5/00CSE 38020 Short-term Process Scheduling in 4.3 BSD based on multi-level feedback queues priorities range from 0 to 127 0-49 reserved for processes executing in kernel mode 50-127 reserved for scheduling processes in user mode number of queues 127 in theory; 32 in VAX time quantum = 1/10 sec (empirically found to be the longest quantum that could be used without loss of the desired response for interactive jobs such as editors) –short time quantum means better interactive response –long time quantum means higher overall system throughput since less context switch overhead and less processor cache flush. priority dynamically adjusted to reflect – resource requirement (e.g., blocked awaiting an event) – resource consumption (e.g., CPU time)

21 University of Pennsylvania 10/5/00CSE 38021 Unix CPU scheduler (cont.) two values in the PCB p_cpu: an estimate of the recent CPU use p_nice: a user settable weighting factor (-20..20); default = 0; negative increases priority. A process' priority calculated every four clock ticks (40 msec) p_usrpi = PUSER + p_cpu/4 + 2 p_nice where PUSER = 50. CPU utilization, p_cpu, is incremented each time the system clock ticks and the process is found to be executing. p_cpu is adjusted once every second (using a digital decay filter) p_cpu = [(2 load)/(2 load + 1)] p_cpu + p_nice where load is an average number of ready processes over the previous 1 minute.

22 University of Pennsylvania 10/5/00CSE 38022 Examples Example 1. single compute-bound process which monopolizes CPU. p_cpu = 0.66 p_cpu + p_nice Example 2. this process accumulates T i clock ticks every 1 second and p_nice is zero. p_cpu = 0.66 T 0 p_cpu = 0.66 (T 1 +.66 T 0 ) =.66 T 1 +.44 T 0 p_cpu = 0.66 T 2 +.44 T 1 +.30 T 0 p_cpu = 0.66 T 3 +... +.20 T 0 p_cpu = 0.66 T 4 +... +.13 T 0 About 90 % of the CPU utilization is forgotten after 5 seconds.

23 University of Pennsylvania 10/5/00CSE 38023 For blocked process,  the system recomputes the priority of a process when the process is awakened and has been sleeping for longer than 1 second.  p_slptime is set to 0 when a process starts sleep and is incremented once every second.  when the process is awakened, p_cpu = [2 load/(2 load +1)] p_slptime p_cpu Then, computes p_usrpri as above.

24 University of Pennsylvania 10/5/00CSE 38024 Real-time Systems On-line transaction systems and interaction systems Real-time monitoring systems Signal processing systems otypical computations otiming requirements otypical architectures Control systems ocomputational and timing requirements of direct computer control ohierarchical structure ointelligent control

25 University of Pennsylvania 10/5/00CSE 38025 Desired characteristics of RTOS Predictability, not speed, fairness, etc. Under normal load, all deterministic (hard deadline) tasks meet their timing constraints under overload conditions, failures in meeting timing constrains occur in a predictable manner.  Interrupt handling and context switching take bounded times Application- directed resource management scheduling mechanisms allow different policies resolution of resource contention can be under explicit direction of the application.

26 University of Pennsylvania 10/5/00CSE 38026 Real-Time System Scheduling Processes have timing constraints such as period, deadline Schedulability analysis Cyclic executive Priority based scheduling algorithms earliest deadline first rate monotonic algorithm Priority inversion problem; e.g., Mars’ Pathfinder reset problem

27 University of Pennsylvania 10/5/00CSE 38027 The Priority Inversion Problem T1 T2 T3 blocked attempt lock Rlock(R) unlock(R)      

28 University of Pennsylvania 10/5/00CSE 38028 The priority inheritance protocol T1 T2 T3 lock R failslock(R) unlock(R) T3 blocks T2 T3 directly blocks T1 T3’s priority is  


Download ppt "University of Pennsylvania 10/5/00CSE 3801 CPU Scheduling CSE 380 Lecture Note 9 Insup Lee."

Similar presentations


Ads by Google