Operating Systems Practical Session 3, Scheduling 1 A quick recap Quality criteria measures: 1. Throughput – The number of completed processes per time unit. 2. Turnaround time – The time interval between the process submission and its completion. 3. Waiting time – The sum of all time intervals in which the process was in the ready queue. 4. Response time – The time taken between submitting a command and the generation of first output. 5. CPU utilization – Percentage of time in which the CPU is not idle. 2 A quick recap Two types of scheduling: Preemptive scheduling A task may be rescheduled to operate at a later time (for example, it may be rescheduled by the scheduler upon the arrival of a “more important” task). Non Preemptive scheduling (cooperative) Task switching can only be performed with explicitly defined system services (for example: the termination task, explicit call to yield() , I/O operation which changes the process state to blocking, etc’…). 3 A quick recap Scheduling algorithms taught in class: 1. FCFS (First – Come, First – Served). • • Non preemptive. Convoy effect. 2. SJF (Shortest Job First). • • • • Provably minimal with respect to the minimal average turn around time. No way of knowing the length of the next CPU burst. Can approximate according to: Tn+1=tn+(1- )Tn Preemptive (Shortest Remaining Time First) or non preemptive. 3. Round Robin. • • When using large time slices it imitates FCFS. When using time slices which are closer to context switch time, more CPU time is wasted on switches. 4 A quick recap 4. Guaranteed scheduling. • • Constantly calculates the ratio between how much time the process has had since its creation and how much CPU time it is entitled to. Guarantees 1/n of CPU time per process / user. 5. Priority scheduling. • A generalization of SJF (How?). 6. Multi Level Queue scheduling. • • • Partition the ready queue. Each partition employs its own scheduling scheme. A process from a lower priority group may run only if there is no higher priority process. 5 A quick recap 7. Dynamic Multi Level scheduling. • i. Takes into account the time spent waiting (the notion of aging to prevent starvation). Highest Response Ratio Next: ii. Feedback scheduling. • • Demote processes running longer. Combine with aging to prevent starvation. 8. Two Level scheduling. • Involves schedulers for Memory-CPU operations, and another scheduler for Memory-Disk operations. 6 The Completely Fair Scheduler (Linux Kernel 2.63) CFS tries to model an “ideal, precise multitasking CPU” – one that could run multiple processes simultaneously, giving each equal processing power. Obviously, this is purely theoretical, so how can we model it? We may not be able to have one CPU run things simultaneously, but we can measure how much runtime each task has had and try and ensure that everyone gets their fair share of time. The Completely Fair Scheduler (cont.) This is held in the vruntime variable for each task, and is recorded at the nanosecond level. A lower vruntime indicates that the task has had less time to compute, and therefore has more need of the processor. CFS uses a Red-Black tree to store, sort, and schedule tasks. The CFS Tree & Scheduling Algo. The key for each node is the vruntime of the corresponding task. To pick the next task to run, simply take the leftmost node. The task accounts for its time with the CPU by adding its execution time to the virtual runtime and is then inserted back into the tree if runnable http://www.ibm.com/developerworks/linux/library/l-completely-fair-scheduler/ CFS Data Structures All tasks within Linux are represented by a task structure called task_struct. CFS has three primary structures – task_struct, sched_entity, and cfs_rq. task_struct is the top-level entity, containing things such as task priority, scheduling class, and the sched_entity struct. sched_entity includes a node for the RB tree and the vruntime statistic (64 bits field) and more. cfs_rq contains the root node of the whole RB tasks tree. Priorities • CFS doesn't use priorities directly but instead uses them as a decay factor for the time a task is permitted to execute. • Lower-priority tasks have higher factors of decay, where higher-priority tasks have lower factors of decay. Priority is inverse to its effect – a higher priority task will accumulate vruntime more slowly, since it needs more CPU time. Likewise, a low-priority task will have its vruntime increase more quickly, causing it to be preempted earlier. CFS Group Scheduling CFS introduced group scheduling in release 2.6.24, adding another level of fairness. Tasks can be grouped together, such as by the user which owns them. CFS can then be applied to the group level as well as the individual task level. Group scheduling is another way to bring fairness to scheduling, particularly in the face of tasks that spawn many other tasks. Example: Two groups exist, group A (contains 10 tasks) and group B (contains 2 tasks). Each group will receive an equal 50% of CPU power. Warm up (1) • Why bother with multiprogramming? • Assume processes in a given system wait for I/O 60% of the time. 1. What is the approximate CPU utilization with one process running? 2. What is the approximate CPU utilization with three processes running? 14 Warm up (1) 1. If a process is blocking on I/O 60% of the time, than there is only 40% of CPU utilization. 2. At a given moment, the probability that all three processes are blocking on I/O is (0.6)3. That means that the CPU utilization is (1-(0.6)3)=0.784, or roughly 78%. 15 Warm up (2) • Assume a single CPU machine with a non preemptive scheduler, attempts to schedule n independent processes. How many possible schedules exist? • Answer: This is exactly like ordering a set of n different characters forming a word of length n. That is, there are n! different possible schedules. 16 Round Robin The following list of processes require scheduling (each requires x Time Units, or TUs): • • • • PA – 6 TU PB – 3 TU PC – 1 TU PD – 7 TU If RR scheduling is used, what quanta size should be used to achieve minimal average turnaround time? (assume 0 cost for context switches) 17 Round Robin 1. Quanta = 1: A B C D A B D A B D A D A D A D D 1 2 3 4 6 7 9 10 11 5 8 12 13 14 15 16 17 (15+9+3+17)/4=11 TU 2. Quanta = 2: A A B B C D D A A B D D A A D D D 1 2 3 4 5 6 9 10 11 14 7 8 12 13 15 16 17 (14+10+5+17)/4=11.5 TU 3. 4. 5. 6. 7. Quanta = 3: 10.75 TU Quanta = 4: 11.5 TU Quanta = 5: 12.25 TU Quanta = 6: 10.5 TU Quanta = 7: 10.5 TU 18 Round Robin Turnaround time depends on the size of the time quantum used. Note that it does not necessarily improve as time quantum size increases! 19 Non preemptive scheduling (Taken from Tanenbaum) Assume 5 different jobs arrive at a computer center, roughly at the same time (same clock tick). Their expected run times are 10, 6, 2, 4 and 8 TU. Their (externally determined) priorities are 3, 5, 2, 1 and 4 respectively. For each of the following scheduling algorithm, determine the mean process turnaround time. Ignore process switching overhead. All jobs are completely CPU bound. 1. Priority Scheduling (nonpreemptive). [Higher number means higher priority] 2. First come first served (in order 10, 6, 2, 4, 8) (non-preemptive) 3. Shortest job first (non-preemptive) 1. 2. 3. PID Priority Time P1 3 10 P2 5 6 P3 2 2 P4 1 4 P5 4 8 Priority Scheduling: (6+14+24+26+30)/5=20 FCFS: (10+16+18+22+30)/5=19.2 SJF: (2+6+12+20+30)/5=14 20 Preemptive dynamic priorities scheduling (Taken from Silberschatz, 5-9) Consider the following preemptive priority scheduling algorithm with dynamically changing priorities: When a process is waiting for the CPU (in the ready queue, but not running), its priority changes at rate α; when it is running, its priority changes at rate β. All processes are given a priority of 0 when they enter the ready queue. The parameters alpha and beta can be set. Higher priority processes take higher values. 1. What is the algorithm that results from β> α > 0? 2. What is the algorithm that results from α < β< 0? 3. Is there a starvation problem in 1? in 2? explain. 21 Preemptive dynamic priorities scheduling 1. >>0. To get a better feeling of the problem, we will create an example: C, P1, P2, P3 arrive one after the other and last for 3 TU, =1 and =2(bold marks the running process): Time 1 2 3 P1 0 2 4 0 P2 P3 4 5 6 1 2 4 6 0 1 2 3 7 8 9 4 6 8 The resulting schedule is FCFS. Slightly more formal: If a process is running it must have the highest priority value. While it is running, it’s priority value increases at a rate greater than any other waiting process. As a result, it will continue it’s run until it completes (or waits on I/O, for example). All processes in the waiting queue, increase their priority at the same rate, hence the one which arrived earliest will have the highest priority once the CPU is available. 22 Preemptive dynamic priorities scheduling 2. <<0. We will use (almost) the same example problem as before, but this time =-2, =-1: Time 1 2 3 4 5 6 7 8 9 P1 0 -1 -3 -5 -7 -9 -11 -13 -14 0 -1 -3 -5 -7 -8 0 -1 -2 P2 P3 The resulting schedule is LIFO. More formally: If a process is running it must have the highest priority value. While it is running, it’s priority value decreases at a much lower rate than any other waiting process. As a result, it will continue it’s run until it completes (or waits on I/O, for example), or a new process with priority 0 is introduced. As before, all processes in the waiting queue, decrease their priority at the same rate, hence the one which arrived later will have the highest priority once the CPU is available. 23 Preemptive dynamic priorities scheduling 3. In the first case it is easy to see that there is no starvation problem. When the kth process is introduced it will wait for at most (k-1)max{timei} Time Units. This number might be large but it is still finite. This is not true for the second case - consider the following scenario: P1 is introduced and receives CPU time. While still working a 2nd process, P2, is initiated. According to the scheduling algorithm in the second case, P2 will receive the CPU time and P1 will have to wait. As long as new processes will keep coming before P1 gets a chance to complete its run, P1 will never complete its task. 24 Dynamic Multi Level scheduling An OS keeps two queues, Q1 and Q2. Each queue implements the round robin (RR) algorithm for all processes it holds. The OS prioritize processes in Q1, over those in Q2. When a process is created or returned from an I/O operation, it enters Q1. A process enters Q2 if it just finished running and it used up its whole time quantum. A process returning from I/O enters Q1 and has precedence over a process which did not start running. In our problem, we have the following processes: Process P1 – arrival time = 0, req.: 1 TU CPU, 1 TU IO, 3 TU CPU. Process P2 – arrival time = 2, req.: 2 CPU, 2 IO, 2 CPU. Process P3 – arrival time = 3, req.: 1 CPU, 3 IO, 3 CPU. Draw the Gantt table and compute the average TA and RT (turnaround and response time). Assume that the time quantum in Q1=1 TU, and the time quantum in Q2=2 TU. Further assume that the system has preemption. Computing the RT is based on the start of each I/O operation (you may think of the I/O as printing to stdout and that the user is waiting for this printout). 25 Dynamic Multi Level scheduling The Gantt table: Time 0 1 2 P1 CPU I/O CPU P2 P3 . 3 4 5 6 CPU CPU CPU . CPU I/O I/O 7 8 9 10 CPU I/O I/O CPU I/O CPU CPU 11 12 CPU CPU Avg. TA: (7+11+9)/3=9 Avg. RT: (1+6+2)/3=3 26 Guaranteed scheduling (Quiz ‘05) Three processes run on a preemptive (switching is done on every round), guaranteed scheduling (ties are broken with preference to the lower ID process) OS. They require the use of both the CPU and I/O: P1 – 1 TU CPU, 4 TU I/O, 2 TU CPU P2 – 1 CPU, 2 I/O, 2 CPU P3 – 2 CPU, 1 I/O, 2 CPU For questions 1-3 draw a Gantt chart and compute the avg. TA time: 1. 2. 3. 4. Identical arrival time, I/O is done on separate devices. Identical arrival time, I/O is done on the same device with FCFS scheduling. Process arrival time is: P1 at 0, P2 at 1 and P3 at 2. I/O is done on separate devices. Would Round Robin produce the same results? Explain your answer. 27 Guaranteed scheduling 1. The Gantt table: Time 0 1 2 3 4 5 6 P1 CPU I/O I/O I/O I/O CPU CPU CPU I/O I/O CPU CPU CPU I/O P2 P3 7 8 9 CPU CPU 10 CPU Avg. TA: (7+8+10)/3=8.33 28 Guaranteed scheduling 2. The Gantt table: Time 0 1 2 3 4 5 6 P1 CPU I/O I/O I/O I/O CPU CPU I/O I/O P2 P3 CPU CPU CPU 7 8 CPU CPU I/O 9 10 CPU CPU 11 Avg. TA: (7+9+11)/3=9 29 Guaranteed scheduling 3. The Gantt table: Time 0 1 2 3 4 5 6 P1 CPU I/O I/O I/O I/O CPU CPU CPU I/O I/O CPU CPU CPU I/O P2 P3 7 8 9 CPU CPU 10 CPU Avg. TA: (7+7+8)/3=7.33 30 Guaranteed scheduling 4. No, RR implements fairness only between processes waiting in ready state, and hence no process will receive two consecutive time slices while another process is waiting. This is in contrast to Guaranteed Scheduling, which takes into account all the time the process spent in the system. 31 Multi-core Scheduling נניח כי עומד לרשותנו מחשב בעל שני מעבדים ( .)C2 ,C1בכל רגע נתון עובדים שני המעבדים אלא אם כן אין יותר עבודות ממתינות. למערכת מגיעים בו זמנית אוסף של 13תהליכים משלושה טיפוסים כמפורט להלן: • • • – – – תהליכי – Aתהליכים קצרים שמסתיימים לאחר יחידת זמן אחת (תהליך יחיד) תהליכי – Bתהליכים ארוכים מעט יותר שמסתיימים לאחר שתי יחידות זמן (7 תהליכים) תהליכי – Cתהליכים שמסתיימים לאחר שלוש יחידות זמן ( 5תהליכים). עבור שני האלגוריתמים הבאים חשבו: מה יהיה ה turnaround time-הממוצע ואיזה אלגוריתם מוצלח יותר עפ"י מדד זה? בכמה זמן CPUנעשה שימוש ואיזה אלגוריתם מוצלח יותר עפ"י מדד זה? מהו משך הזמן הנדרש לסיום החישוב ואיזה אלגוריתם מוצלח יותר עפ"י מדד זה? – – 32 תהליכים מטיפוס Aו B-מופנים למעבד C1שמפעיל SJFעל תהליכים אלו .כל תהליכי C מופנים למעבד .C2במידה ומעבד מסוים סיים את עבודתו לפני השני הוא מטפל בתהליכים הנותרים עפ"י עיקרון .SJF התהליכים מופנים למעבדים השונים עפ"י .SJF Multi-core Scheduling נצייר טבלת Ganttלשני המעבדים: עבור האלגוריתם הראשון: ועבור האלגוריתם השני: כעת ניתן לענות על השאלות בקלות: – – – 33 לראשון – avg. TA=(64+45)/13=109/13=8.38 לשני – avg. TA=(55+45)/13=100/13=7.69 ולכן ברור כי SJFיהיה מוצלח יותר כאשר נשתמש באלגוריתם השני( .עדיף על פני .)Affinity ברור שבשתי השיטות משך החישוב הנדרש יהיה זהה .30 :בשיטה אחת 15+15ואילו בשיטה השניה .16+14 במפתיע ,למרות שהאלגוריתם השני מוצלח יותר מבחינת ,turnaround timeהוא מסיים מאוחר יותר ,כעבור 16יחידות זמן ,בעוד שהאלגוריתם הראשון מסיים לאחר 15יחידות זמן.