Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: CPU Scheduling

Similar presentations


Presentation on theme: "Chapter 5: CPU Scheduling"— Presentation transcript:

1 Chapter 5: CPU Scheduling

2 Chapter 5: CPU Scheduling
Basic Concepts Scheduling Criteria(기준, 척도) Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating Systems Examples Algorithm Evaluation

3 Levels of Scheduling Running Ready Blocked Short Term Blocked, Suspend
Ready, Suspend Medium Term New Exit Long Term

4 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.

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

6 Basic Concepts CPU burst vs I/O burst

7 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

8 Histogram of CPU-burst Times
4 2 1 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.

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

10 CPU Scheduler Multiprogramming environment
CPU Scheduler selects processes in memory that are ready to execute, and allocates the CPU to one of them. 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. Scheduling under 1 and 4 is non-preemptive. ex) Nonpreemptible resource - printer, tape, … All other scheduling is preemptive. ex) Preemptible resource - CPU (both are applicable) Operating System Concepts

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

12 Scheduling Criteria (Performance Index)
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 execute 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, not completion of output (for time-sharing environment) Operating System Concepts

13 Scheduling Algorithms
Criteria : Average waiting time Single processor algorithm First-Come, First-Served Scheduling Shortest-Job-First Scheduling Priority Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Feedback Queue Thread scheduling Multiple-Processor Scheduling Realtime Scheduling EDF (Earliest Deadline First) Rate Monotonic Rate Monotonic vs. EDF Examples Solaris 2 Scheduling, Windows XP Scheduling, Linux Scheduling Operating System Concepts

14 First-Come, First-Served (FCFS)
Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 Waiting time P1 = 0, P2 = 24, P3 = 27 Average waiting time: ( )/3 = / 3 = 17 Convoy effect : short process behind long process P1 P2 P3 24 27 30

15 FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order P2 , P3 , P1 The Gantt chart for the schedule is: Waiting time P1 = 6, P2 = 0, P3 = 3 Average waiting time: ( )/3 = 3 Much better than previous case. Process Burst Time P P P P2 P3 P1 3 6 30 ( )/3 = 17 P1: KT telephone bill prcoessing – hours P2, P3: KT homepage click – 1 second Operating System Concepts

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

17 Example of Non-Preemptive SJF
Process Arrival Time Burst Time P P P P SJF (non-preemptive) Average waiting time = ( )/4 = 4 P3 (1) P2 (4) P1 (7) P4 (4) P1 P3 P2 P4 3 7 8 12 16 (0-0, 8-2, 7-4, 12-5) Operating System Concepts

18 Example of Preemptive SJF
Process Arrival Time Burst Time P P P P SJF (preemptive) Average waiting time = ( )/4 = 3 -2 -5 -2 -2 -1 -4 But how do you know this at the process arrival time? Estimate? P1 P2 P3 P2 P4 P1 (7-2) (4-2) (1-1) (2-2) (4-4) (5-5) P1 P3 P2 4 2 11 P4 5 7 16 P1 : (0-0)+(11-2) = 9 P2 : (2-2)+(5-4)=1 P3 : (4=4) = 0 P4 : (7-5) = 2 Operating System Concepts

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

20 Examples of Exponential Averaging
Define : Prediction of the Length of the Next CPU Burst t=0, t=1, t=2, t=3,

21 Priority Scheduling SJF (Shortest-Job-First)
A priority number(integer) is associated with process The CPU is allocated to the process with the highest priority (smallest integer  highest priority). Preemptive nonpreemptive 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 Operating System Concepts

22 Round Robin (RR) Time quantum expired Each process gets a small unit of CPU time (time quantum, slice), usually 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: q large  FIFO (good for compute bound jobs) q small  q must be large with respect to context switch, (문맥전환이 빈번해짐) otherwise overhead is too high. (good for I/O bound jobs & realtime jobs) Single q value cannot satisfy both  multilevel feedback queue Operating System Concepts

23 Example of RR with Time Quantum = 4
Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: Average waiting time : (6+4+7)/3 = in case of SJF=3 Average turnaround time : ( )/3 = 15.6 in case of SJF=3 Typically, higher average turnaround than SJF, but better response P1 P2 P3 4 7 10 14 18 22 26 30

24 Example of RR with Time Quantum = 20
Process Burst Time P1 53 P2 17 P3 68 P4 24 The Gantt chart is: -20 -20 -13 -17 -20 -20 -20 -8 -20 -4 P1 P2 P3 P4 20 37 57 77 97 117 121 134 154 162

25 Time Quantum and Context Switch Time

26 Turnaround Time vs. Time Quantum
At Q=5, averageTurnaround time = ( ) / 4 = 12.25

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

28 Multilevel Queue Scheduling

29 Multilevel Feedback Queue
A process can move between the various 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 Operating System Concepts

30 Example of Multilevel Feedback Queue
Three queues: Q0 – time quantum 8 milliseconds Q1 – time quantum 16 milliseconds Q2 – FCFS Scheduling A new job enters queue Q0 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 Q1. At Q1 job is again served FCFS and receives 16 additional ms. If it still does not complete, it is preempted and moved to queue Q2. Lower level queue executes only when higher level queues are empty at all time(preemptive) Q0 Q1 Q2 Operating System Concepts

31 Thread Scheduling Two types of thread scheduling
User Level : process local scheduling How the threads library decides which thread to put onto an available LWP Kernel Level : system global scheduling How the kernel decides which kernel thread to run next

32 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 scope\n”); 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 the threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); /* now join on each thread */ pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param) { /* do some work */ printf("I am a thread\n"); pthread exit(0); pthread scheduling process-contention scope; PCS : user-level thread system-contention scope; CSC : kernel-level thread

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

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

35 NUMA and CPU Scheduling
NUMA(Non-Uniform Memory Access and CPU Scheduling

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

37 Operating System Examples
Solaris scheduling Windows XP scheduling Linux scheduling

38 Solaris 2 Scheduling

39 Solaris Dispatch Table
Low High

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

41 Priorities and Time-slice length

42 List of Tasks Indexed According to Priorities
Linux Scheduling(Linux kernel 2.5) List of Tasks Indexed According to Priorities

43 Algorithm Evaluation Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload Queueing models Simulation Implementation

44 Deterministic Modeling
P1, …,P5 = (10, 29, 3, 7, 12) FCFS SJF RR

45 Evaluation by simulation

46 End of Chapter 5


Download ppt "Chapter 5: CPU Scheduling"

Similar presentations


Ads by Google