Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 5.CPU Scheduling 5.1 Algorithms.

Similar presentations


Presentation on theme: "Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 5.CPU Scheduling 5.1 Algorithms."— Presentation transcript:

1 Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra pmarques@dei.uc.pt 2006/2007 5.CPU Scheduling 5.1 Algorithms

2 2 Disclaimer This slides and notes are heavily based on the companion material of [Silberschatz05].The original material can be found at: http://codex.cs.yale.edu/avi/os-book/os7/slide-dir/index.html In some cases, material from [Stallings04] may also be used. The original material can be found at: http://williamstallings.com/OS/OS5e.html The respective copyrights belong to their owners.

3 3 What’s the Best Scheduler? It all depends on what you are optimizing for! 146 132 470 375 Capacity (Passengers) 2,1606,400Concorde 87013,952Douglas DC8 9766,640Boeing 747 9767,408Boeing 777 Velocity (Km/h) Autonomy (Km) Plane 127,020 285,120 458,720 366,000 Throughput (Passengers x Km/h) What’s the Best Plane?

4 4 Process Scheduling Let’s see how to choose which process/thread should the CPU be allocated to!

5 5 System Queues

6 6 Preemptive vs. Non-Preemptive Non-Preemptive Scheduling Once the CPU is allocated to a process, the process keeps the CPU until it terminates or it blocks in a I/O operations and switches to the waiting state (e.g. in Windows 98). Preemptive Scheduling The operating system may decide (and is able to) take the CPU away from a running process (e.g. in WindowsXP, Linux)

7 7 Scheduling Criteria CPU utilization Keep the CPU as busy as possible Throughput Number of processes that complete their execution per time unit Turnaround time Amount of time to complete 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

8 8 Optimization Criteria… As seen before, it all depends on what type of system we are aiming at… But, we would like: Maximum CPU utilization Maximum throughput Minimum turnaround time Minimum waiting time Minimum response time That’s a multi-objective function, not really possible…

9 9 Why can we optimize? CPU-burst Times CPU-IO Cycle

10 10 Algorithm 1: First-Come First-Served (FCFS) ProcessBurst 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 This is a non-preemptive algorithm P1P1 P2P2 P3P3 2427300

11 11 First-Come First-Served (FCFS) – Cont. 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 I/O bound vs. CPU bound problem P1P1 P3P3 P2P2 63300

12 12 Algorithm 2: 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. How do you determine that? Can you guess the future? Also known as Shortest-Job-Next (SJN), which is more correct. Two schemes: SJF (non-premptive): once CPU given to the process it cannot be preempted until completes its CPU burst. SRTF (preemptive): if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time First (SRTF).

13 13 (Non-Preemptive) Shortest-Job-First (SJF) ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 = 4 Can be proved optimal (minimizes waiting time) for a set of processes Used frequently in long-term scheduling P1P1 P3P3 P2P2 73160 P4P4 812

14 14 (Preemptive) Shortest-Remaining-Time-First (SRTF) ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SRTF (preemptive) Average waiting time = (9 + 1 + 0 +2)/4 = 3 P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16

15 15 Predicting the next CPU burst time Based on an exponential moving average based on the past.

16 16 Priority Scheduling A priority number is associated with each process The CPU is allocated to the process with the highest priority (smallest integer  highest priority) 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

17 17 Starvation @MIT IBM 7094 When the system managers shutdown the IBM 7094 at MIT in 1973, they found a low-priority process that had been submitted in 1967 and had not yet run…

18 18 Priority-Based Scheduler

19 19 Algorithm 3: 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. New processes are added to the tail of the ready queue If there are n processes in the ready queue and the time quantum is q, then 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  FCFS q small  q must be large with respect to context switch, otherwise overhead is too high. If so, having N processes is like having a machine running at 1/N of the speed.

20 20 RR with a quantum of 20 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 time

21 21 Timeline charts are relevant!

22 22 Time Quantum and Context Switches

23 23 Turnaround Time vs. Time Quantum Turnaround time depends on time quantum but the relation is not direct. In general, turnaround time can be improved if most processes finish on a time quantum

24 24 Multilevel Queue Scheduling

25 25 Multilevel Queue Scheduling (2) Ready queue is partitioned into separate queues. E.g. Foreground (interactive) Background (batch) Each queue has its own scheduling algorithm: Foreground – RR Background – FCFS Scheduling must be done between the queues. Fixed priority scheduling (i.e., serve all processes from foreground; then from background). Possibility of starvation. Time slice: each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR; 20% to background in FCFS

26 26 Multilevel Feedback Queue Scheduling Processes can move among queues! Aging can be implemented this way Multilevel-feedback-queue scheduler defined by the following parameters: number of queues scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to demote a process method used to determine which queue a process will enter when that process needs service

27 27 Example of a scheduler with three queues Note that queue serving is priority-based!

28 28 Example of a scheduler with three queues (2) Three queues: Q0 – time quantum 8 milliseconds Q1 – time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. Gives high-priority to any process with CPU bursts of 8ms or less. Grabs CPU, do processing, move along to next IO cycle… Process with bursts of >8ms and <16ms are served quickly but with less priority Long running processes since to Q2 and are served on any CPU cycles remaining.

29 29 Characteristics of Scheduling Algorithms from [Stallings05]

30 Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra pmarques@dei.uc.pt 2006/2007 5.CPU Scheduling 5.2 Example from Stallings

31 31 Example This example is taken from [Stallings04] Homework Calculate Finish-Time, Waiting-Time, Turnaround Time for each process Calculate Average Waiting Time and Average Turnaround Time

32 32 FCFS Short process may have to wait a very long time before it can execute Favors CPU-bound processes I/O processes have to wait until CPU-bound process completes

33 33 Round-Robin (q=1) Uses preemption based on a clock When an interrupt occurs, the running process is placed in the ready queue Next ready job is selected Known as time slicing

34 34 SPN / SJF Non-preemptive policy Process with shortest expected processing time is selected next Short process jumps ahead of longer processes

35 35 SRTF Preemptive version Must estimate processing time

36 36 Multilevel Feedback Penalize jobs that are longer

37 37 Comparison of Scheduling Algorithms

38 38 Comparison of Scheduling Algorithms (2)

39 Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra pmarques@dei.uc.pt 2006/2007 5.CPU Scheduling 5.3 Operating Systems

40 40 Traditional UNIX Scheduling Multilevel feedback using Round-Robin within each of the priority queues If a process does not block or complete within a second, it is preempted Priorities are recomputed once per second Base priority divides all processes into fixed bands of priority levels

41 41 Windows XP Scheduling Priority-based Preemptive Scheduling The highest priority queue is always served first 32 levels of priorities, two classes of threads: Real-time, having a priority from 16 to 31 Variable, having a priority from 1 to 15 The priorities from all threads vary except if they are in a REAL_TIME_PRIORITY_CLASS Within each class, there are relative priorities “Normal” preempted threads have their priority lowered; “Normal” threads released from a wait() have their priority boosted. Time quantum is different for “foreground” and “background” threads.

42 42 Linux 2.6 Scheduling Preemptive Priority-Based Scheduler Two separate priority ranges: Real-time (0-99) and Nice (100-140) Two algorithms: time-sharing and real-time Scheduling (Time-Sharing Algorithm  Fair preemptive): Active Array and Expired Array Scheduling is done from the active array. The scheduler chooses the task from the active array for execution. It runs until its time slice is gone, or it is blocked. Whenever a task has used all its time slice, it’s moved to the expired array. Recalculation of its priority is done at that time. Execution goes on until the active array is empty. When the active array is empty, it’s switched with the expired array. Real-time tasks (Soft Real time) are POSIX.1b compliant and are assigned fixed priorities. Two scheduling algorithms are used: FCFS and RR.

43 43 Linux 2.6 Scheduling (2) Higher priority tasks are assigned a longer time quantum Two arrays are used for scheduling

44 44 Reference Chapter 5: CPU Scheduling 5.1, 5.2, 5.3, 5.6, 5.8


Download ppt "Operating Systems Paulo Marques Departamento de Eng. Informática Universidade de Coimbra 2006/2007 5.CPU Scheduling 5.1 Algorithms."

Similar presentations


Ads by Google