Chapter 5 CPU Scheduling Chapter 5 CPU Scheduling

  • Slides: 46
Download presentation
Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling

Chapter 5: CPU Scheduling n Basic Concepts n Scheduling Criteria(기준, 척도) n Scheduling Algorithms

Chapter 5: CPU Scheduling n Basic Concepts n Scheduling Criteria(기준, 척도) n Scheduling Algorithms n Multiple-Processor Scheduling n Thread Scheduling n Operating Systems Examples n Algorithm Evaluation 1. 2 Silberschatz, Galvin and Gagne 2002

Levels of Scheduling Running Ready Blocked Short Term Blocked, Suspend Ready, Suspend Medium Term

Levels of Scheduling Running Ready Blocked Short Term Blocked, Suspend Ready, Suspend Medium Term Exit New Long Term 1. 3 Silberschatz, Galvin and Gagne 2002

Levels of Scheduling § § § Long-term Scheduling ü Long term scheduling is performed

Levels of Scheduling § § § Long-term Scheduling ü Long term scheduling is performed when a new process is created Medium-term Scheduling ü Medium-term scheduling is a part of the swapping function Short-term Scheduling ü Short-term scheduler is invoked whenever an event occurs, that may lead to the interruption of the current running process. For example clock interrupts, I/O interrupts, operating system calls, signals, etc. 1. 4 Silberschatz, Galvin and Gagne 2002

Basic Concepts n Goal of processor scheduling F Maximum CPU utilization(활용) obtained with multiprogramming

Basic Concepts n Goal of processor scheduling F Maximum CPU utilization(활용) obtained with multiprogramming n Burst (time) F CPU Burst : Time it takes for the CPU to execute an operation(작업) F I/O Burst : Time it takes for the CPU to wait for I/O task n CPU–I/O Burst Cycle F Process execution consists of a cycle of CPU execution and I/O wait • CPU excution(CPU burst) I/O wait(I/O burst) F Two types of CPU burst • CPU bound program • I/O bound program n CPU burst distribution 1. 5 Silberschatz, Galvin and Gagne 2002

Basic Concepts n CPU burst vs I/O burst 1. 6 Silberschatz, Galvin and Gagne

Basic Concepts n CPU burst vs I/O burst 1. 6 Silberschatz, Galvin and Gagne 2002

Alternating Sequence of CPU And I/O Bursts Foreground jobs (interactive) – keyboard/mouse Background jobs

Alternating Sequence of CPU And I/O Bursts Foreground jobs (interactive) – keyboard/mouse Background jobs (batch – no human interaction) I/O bound jobs ü CPU priority : I/O bound > CPU bound Compute-bound(CPU bound) jobs 1. 7 Silberschatz, Galvin and Gagne 2002

Histogram of CPU-burst Times 12 4 milliseconds = 1/1000 second § § Short CPU

Histogram of CPU-burst Times 12 4 milliseconds = 1/1000 second § § Short CPU burst § I/O-oriented program would have a lot of short CPU bursts. Long CPU burst § CPU-oriented program would be able to have many long CPU bursts. 1. 8 Silberschatz, Galvin and Gagne 2002

Scheduling 1 3 n Case A – Total amount of work changes F (more

Scheduling 1 3 n Case A – Total amount of work changes F (more Throughput(처리량)) F disk scheduling F Postman scheduling 5 4 6 2 n Case B – Total amount of work is not affected 3 F (no Throughput change) 1 14 13 F (Response time distribution changes) 3 F CPU scheduling, cafeteria line, bus line 14 F For case B 10 10 1 11 1 3 1 F If homogeneous(동종의) jobs? – no need to do scheduling F If heterogeneous(이종의) jobs – scheduling needed F F eg – some jobs are I/O bound jobs, others are CPU bound jobs terminal (foreground) jobs, realtime jobs, batch jobs Operating System Concepts 1. 9 Silberschatz, Galvin and Gagne 2002

CPU Scheduler n Multiprogramming environment n CPU Scheduler selects processes in memory that are

CPU Scheduler n Multiprogramming environment n CPU Scheduler selects processes in memory that are ready to execute, and allocates the CPU to one of them. n CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state. (eg I/O request) 2. Switches from running to ready state. (eg timerunout) 3. Switches from waiting to ready. (eg I/O finished interrupt) 4. Terminates. F Scheduling under 1 and 4 is non-preemptive. ex) Nonpreemptible resource - printer, tape, … F All other scheduling is preemptive. ex) Preemptible resource - CPU (both are applicable) Operating System Concepts 1. 10 Silberschatz, Galvin and Gagne 2002

Dispatcher n Dispatcher module gives control of the CPU to the process selected by

Dispatcher n Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; n this involves: F switching context F switching to user mode F jumping to the proper location in the user program and restarting the program. n Dispatch latency F time which it takes for the dispatcher to stop one process and start another running. F have to be short. F (context switch overhead is the majority) Operating System Concepts 1. 11 Silberschatz, Galvin and Gagne 2002

Scheduling Criteria (Performance Index) n CPU utilization(이용률) F keep the CPU as busy as

Scheduling Criteria (Performance Index) n CPU utilization(이용률) F keep the CPU as busy as possible n Throughput(처리율) F number of processes that complete their execution per time unit n Turnaround time(반환시간) F amount of time to execute a particular process n Waiting time(대기시간) F amount of time a process has been waiting in the ready queue n Response time(응답시간) F amount of time it takes from when a request was submitted until the first response is produced, not completion of output (for timesharing environment) Operating System Concepts 1. 12 Silberschatz, Galvin and Gagne 2002

Scheduling Algorithms n Criteria : Average waiting time n Single processor algorithm 1. First-Come,

Scheduling Algorithms n Criteria : Average waiting time n Single processor algorithm 1. First-Come, First-Served Scheduling 2. Shortest-Job-First Scheduling 3. Priority Scheduling 4. Round-Robin Scheduling 5. Multilevel Queue Scheduling 6. Multilevel Feedback Queue n Thread scheduling n Multiple-Processor Scheduling n Realtime Scheduling F EDF (Earliest Deadline First) F Rate Monotonic vs. EDF n Examples F Solaris 2 Scheduling, F Windows XP Scheduling, F Linux Scheduling Operating System Concepts 1. 13 Silberschatz, Galvin and Gagne 2002

First-Come, First-Served (FCFS) Process Burst Time P 1 24 P 2 3 P 3

First-Come, First-Served (FCFS) Process Burst Time P 1 24 P 2 3 P 3 3 n Suppose that the processes arrive in the order: P 1 , P 2 , P 3 P 1 P 2 0 24 P 3 27 30 n Waiting time F P 1 = 0, P 2 = 24, P 3 = 27 n Average waiting time: (0 + 24 + 27)/3 = 17 51 / 3 = 17 n Convoy effect : short process behind long process 1. 14 Silberschatz, Galvin and Gagne 2002

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order P 2

FCFS Scheduling (Cont. ) Suppose that the processes arrive in the order P 2 , P 3 , P 1 n The Gantt chart for the schedule is: P 2 0 P 3 3 Process Burst Time P 1 24 P 2 3 P 3 3 P 1 6 30 n Waiting time F P 1 = 6, P 2 = 0, P 3 = 3 (0 + 24 + 27)/3 = 17 n Average waiting time: (6 + 0 + 3)/3 = 3 n Much better than previous case. P 1: KT telephone bill prcoessing – hours P 2, P 3: KT homepage click – 1 second Operating System Concepts 1. 15 Silberschatz, Galvin and Gagne 2002

Shortest-Job-First (SJF) Scheduling n Associate with each process the length of its next CPU

Shortest-Job-First (SJF) Scheduling n Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time. n Two approaches: F Non-preemptive 4 once CPU given to the process it cannot be preempted until completes its CPU burst. F Preemptive 4 if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. 4 This scheme is known as the Shortest-Remaining-Time-First (SRTF). n SJF is optimal F gives minimum average waiting time for a given set of processes. n Demerit of SJF : Starvation Operating System Concepts 1. 16 Silberschatz, Galvin and Gagne 2002

Example of Non-Preemptive SJF Process Arrival Time P 1 0. 0 P 2 2.

Example of Non-Preemptive SJF Process Arrival Time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 n SJF (non-preemptive) P 2 (4) P 1 P 3 (7) (1) P 1 0 3 P 3 7 Burst Time 7 4 1 4 P 4 (4) P 2 8 P 4 12 16 n Average waiting time = (0 + 6 + 3 + 7)/4 = 4 (0 -0, 8 -2, 7 -4, 12 -5) Operating System Concepts 1. 17 Silberschatz, Galvin and Gagne 2002

Example of Preemptive SJF Process P 1 P 2 P 3 P 4 n

Example of Preemptive SJF Process P 1 P 2 P 3 P 4 n SJF (preemptive) P 1 (7 -2) P 1 0 P 2 (2 -2) P 2 P 3 (4 -2) (1 -1) P 2 2 Arrival Time 0. 0 2. 0 4. 0 5. 0 P 3 4 P 2 5 Burst Time -2 -5 7 -2 -2 4 -1 1 -4 4 P 1 (5 -5) P 4 (4 -4) P 4 7 But how do you know this at the process arrival time? Estimate? P 1 11 16 n Average waiting time = (9 + 1 + 0 +2)/4 = 3 Operating System Concepts P 1 : (0 -0)+(11 -2) = 9 P 2 : (2 -2)+(5 -4)=1 P 3 : (4=4) = 0 P 4 : (7 -5) = 2 1. 18 Silberschatz, Galvin and Gagne 2002

Determining Length of Next CPU Burst n But how do you know the length

Determining Length of Next CPU Burst n But how do you know the length of next CPU burst? (input data, branch, user …) n Can only estimate the length. n Can be done by using the length of previous CPU bursts, using exponential averaging(지수평균). 1. tn = actual lenght of nth CPU burst 2. tn +1 = predicted value for the next CPU burst 3. a, 0 £ a £ 1 4. Define : Operating System Concepts : taf, 타우 1. 19 Silberschatz, Galvin and Gagne 2002

Examples of Exponential Averaging n Define : n Prediction of the Length of the

Examples of Exponential Averaging n Define : n Prediction of the Length of the Next CPU Burst t=0, t=1, t=2, t=3, 1. 20 Silberschatz, Galvin and Gagne 2002

Priority Scheduling SJF (Shortest-Job-First) n A priority number(integer) is associated with process n The

Priority Scheduling SJF (Shortest-Job-First) n A priority number(integer) is associated with process n The CPU is allocated to the process with the highest priority (smallest integer highest priority). F Preemptive F nonpreemptive n SJF is a priority scheduling where priority is the predicted next CPU burst time. n Problem n F Starvation: low priority processes may never execute. n Solution F Aging: as time progresses increase the priority Operating System Concepts 1. 21 Silberschatz, Galvin and Gagne 2002

Time quantum expired Round Robin (RR) n Each process gets a small unit of

Time quantum expired Round Robin (RR) n Each process gets a small unit of CPU time (time quantum, slice), n n n 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 the time quantum (slice) is q sec, then each process gets 1/n of CPU time in chunks of at most q sec at once. No process waits more than (n-1)q sec. -- short job – after (n-1)q sec Waiting time is proportional(비례하는) to it’s own execution time Performance: F q large FIFO (good for compute bound jobs) F q small q must be large with respect to context switch, F – (문맥전환이 빈번해짐) otherwise overhead is too high. (good for I/O bound jobs & realtime jobs) n Single q value cannot satisfy both multilevel feedback queue Operating System Concepts 1. 22 Silberschatz, Galvin and Gagne 2002

Example of RR with Time Quantum = 4 Process P 1 P 2 P

Example of RR with Time Quantum = 4 Process P 1 P 2 P 3 Burst Time 24 3 3 n The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 14 18 P 1 P 1 22 26 30 n Average waiting time : (6+4+7)/3 = 5. 66 in case of SJF=3 n Average turnaround time : (30+7+10)/3 = 15. 6 in case of SJF=3 n Typically, higher average turnaround than SJF, but better response 1. 23 Silberschatz, Galvin and Gagne 2002

Example of RR with Time Quantum = 20 Process P 1 P 2 P

Example of RR with Time Quantum = 20 Process P 1 P 2 P 3 P 4 Burst Time 53 17 68 24 -20 -13 -17 -20 -20 -4 -20 -8 n The Gantt chart is: P 1 0 P 2 20 37 P 3 57 P 4 P 1 P 3 P 3 77 97 117 121 134 154 162 1. 24 Silberschatz, Galvin and Gagne 2002

Time Quantum and Context Switch Time 1. 25 Silberschatz, Galvin and Gagne 2002

Time Quantum and Context Switch Time 1. 25 Silberschatz, Galvin and Gagne 2002

Turnaround Time vs. Time Quantum At Q=5, average. Turnaround time = (15 + 8

Turnaround Time vs. Time Quantum At Q=5, average. Turnaround time = (15 + 8 + 9 + 17) / 4 = 12. 25 1. 26 Silberschatz, Galvin and Gagne 2002

Multilevel Queue n Ready queue is partitioned into separate queues: F foreground (interactive) F

Multilevel Queue n Ready queue is partitioned into separate queues: F foreground (interactive) F background (batch – no human interaction) n Each queue has its own scheduling algorithm F eg: F foreground (interative) queue -- RR background (batch) queue -- FCFS n Scheduling must be done between the queues. F Fixed priority scheduling 4 serve all from foreground then serve from background. 4 Possibility of starvation. F Time slice 4 each queue gets a certain amount of CPU time 4 which it can schedule amongst its processes 4 Eg. , 80% to foreground in RR, 20% to background in FCFS Operating System Concepts 1. 27 Silberschatz, Galvin and Gagne 2002

Multilevel Queue Scheduling 1. 28 Silberschatz, Galvin and Gagne 2002

Multilevel Queue Scheduling 1. 28 Silberschatz, Galvin and Gagne 2002

Multilevel Feedback Queue n A process can move between the various queues; n aging

Multilevel Feedback Queue n A process can move between the various queues; n aging can be implemented this way. n Multilevel-feedback-queue scheduler defined by the following parameters: F number of queues F scheduling algorithms for each queue F method used to determine when to upgrade a process F method used to determine when to demote a process F method used to determine which queue a process will enter when that process needs service Operating System Concepts 1. 29 Silberschatz, Galvin and Gagne 2002

Example of Multilevel Feedback Queue n Three queues: Q 0 F Q 0 –

Example of Multilevel Feedback Queue n Three queues: Q 0 F Q 0 – time quantum 8 milliseconds F Q 1 – time quantum 16 milliseconds Q 1 F Q 2 – FCFS Q 2 n Scheduling F A new job enters queue Q 0 which is served FCFS. F When it gains CPU, job receives 8 milliseconds. F If it does not finish in 8 milliseconds, job is moved to queue Q 1. F At Q 1 job is again served FCFS and receives 16 additional ms. F If it still does not complete, it is preempted and moved to queue Q 2. F Lower level queue executes only when higher level queues are empty at all time(preemptive) Operating System Concepts 1. 30 Silberschatz, Galvin and Gagne 2002

Thread Scheduling n Two types of thread scheduling n User Level : process local

Thread Scheduling n Two types of thread scheduling n User Level : process local scheduling F How the threads library decides which thread to put onto an available LWP n Kernel Level : system global scheduling F How the kernel decides which kernel thread to run next 1. 31 Silberschatz, Galvin and Gagne 2002

Pthread Scheduling API for POSIX #include <pthread. h> #include <stdio. h> #define NUM_THREADS 5

Pthread Scheduling API for POSIX #include <pthread. h> #include <stdio. h> #define NUM_THREADS 5 int main(int argc, char *argv[]) { int I, scope; pthread_t tid[NUM_THREADS]; pthread_attr_t attr; /* get the default attributes */ pthread_attr_init(&attr); /* first inquire on the current scope */ if (pthread_attr_getscope(&attr, &scope) != 0) printf(stderr, “Unable to get scheduling scopen”); else { if (scope == PTHREAD_SCOPE_PROCESS) pritnf(“PTHREAD_SCOPE_PROCESS”); else if (scope == PTHREAD_SCOPE_SYSTEM) pritnf(“PTHREAD_SCOPE_SYSTEM”); else printf(stderr, ”Illegal scope value. n”); } /* set the scheduling algorithm to PCS(PROCESS) or (SCS)SYSTEM */ pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); /* set the scheduling policy - FIFO, RR, or OTHER pthread_attr_setschedpolicy(&attr, SCHED OTHER); */ /* create threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], &attr, runner, NULL); /* now join on each thread */ for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { /* do some work */ printf("I am a threadn"); pthread exit(0); } pthread scheduling • process-contention scope; PCS : user-level thread • system-contention scope; CSC : kernel-level thread 1. 32 Silberschatz, Galvin and Gagne 2002

Multiple-Processor Scheduling n CPU scheduling more complex when multiple CPUs are available. n Homogeneous(동종)

Multiple-Processor Scheduling n CPU scheduling more complex when multiple CPUs are available. n Homogeneous(동종) processors within a multiprocessor. n Load sharing(부하공유) n Asymmetric multiprocessing(AMP) F only one processor(master) accesses the system data structures, alleviating(완화하다) the need for data sharing. n Symmetric Multiprocessing (SMP) F each processor makes its own scheduling decisions. Operating System Concepts 1. 33 Silberschatz, Galvin and Gagne 2002

Processor affinity(친화성) n Processor affinity refers to binding a process or a set of

Processor affinity(친화성) n Processor affinity refers to binding a process or a set of processes to a specific CPU or a set of CPUs n Override the system’s built-in scheduler to force a process to only run on specified CPUs. F soft affinity F hard affinity n This can provide some performance gains in SMP and NUMA environments. 1. 34 Silberschatz, Galvin and Gagne 2002

NUMA and CPU Scheduling § NUMA(Non-Uniform Memory Access and CPU Scheduling 1. 35 Silberschatz,

NUMA and CPU Scheduling § NUMA(Non-Uniform Memory Access and CPU Scheduling 1. 35 Silberschatz, Galvin and Gagne 2002

Multiple-Processors n Recent trend to place multiple processor cores on same physical chip n

Multiple-Processors n Recent trend to place multiple processor cores on same physical chip n Faster and consume less power n Multiple threads per core also growing F Takes advantage of memory stall to make progress on another thread while memory retrieve happens • Memory stall • Multithread multicore system 1. 36 Silberschatz, Galvin and Gagne 2002

Operating System Examples n Solaris scheduling n Windows XP scheduling n Linux scheduling 1.

Operating System Examples n Solaris scheduling n Windows XP scheduling n Linux scheduling 1. 37 Silberschatz, Galvin and Gagne 2002

Solaris 2 Scheduling 1. 39 Silberschatz, Galvin and Gagne 2002

Solaris 2 Scheduling 1. 39 Silberschatz, Galvin and Gagne 2002

Solaris Dispatch Table Low High 1. 40 Silberschatz, Galvin and Gagne 2002

Solaris Dispatch Table Low High 1. 40 Silberschatz, Galvin and Gagne 2002

Windows XP Priorities Variable class : priority 1 to 15 Real-time class : priority

Windows XP Priorities Variable class : priority 1 to 15 Real-time class : priority 16 - 31 1. 41 Silberschatz, Galvin and Gagne 2002

Priorities and Time-slice length 1. 44 Silberschatz, Galvin and Gagne 2002

Priorities and Time-slice length 1. 44 Silberschatz, Galvin and Gagne 2002

Linux Scheduling(Linux kernel 2. 5) • List of Tasks Indexed According to Priorities 1.

Linux Scheduling(Linux kernel 2. 5) • List of Tasks Indexed According to Priorities 1. 45 Silberschatz, Galvin and Gagne 2002

Algorithm Evaluation n Deterministic modeling – takes a particular predetermined workload and defines the

Algorithm Evaluation n Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload n Queueing models n Simulation n Implementation 1. 46 Silberschatz, Galvin and Gagne 2002

Deterministic Modeling P 1, …, P 5 = (10, 29, 3, 7, 12) FCFS

Deterministic Modeling P 1, …, P 5 = (10, 29, 3, 7, 12) FCFS SJF RR 1. 47 Silberschatz, Galvin and Gagne 2002

Evaluation by simulation 1. 48 Silberschatz, Galvin and Gagne 2002

Evaluation by simulation 1. 48 Silberschatz, Galvin and Gagne 2002

End of Chapter 5

End of Chapter 5