Saurav Karmakar. Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling

Embed Size (px)

DESCRIPTION

Objectives  To introduce CPU scheduling, which is the basis for multiprogrammed operating systems  To describe various CPU-scheduling algorithms  To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system

Citation preview

Saurav Karmakar Chapter 5: CPU Scheduling Basic Concepts Scheduling Criteria Scheduling Algorithms Thread Scheduling Multiple-Processor Scheduling Operating Systems Examples Algorithm Evaluation Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various CPU-scheduling algorithms To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system Basic Concepts Uniprocessor system one process may run at a time Objective of multiprogramming some process running at all times to maximize CPU utilization When one process has to wait, the operating system takes the CPU away from that process and gives the CPU to another process Almost all computer resources are scheduled before use CPU-I/O Burst Cycle Success of CPU scheduling follows : Process execution CPUI/O Burst Cycle Consists of a cycle of CPU execution and I/O wait Basically While a process waits for I/O, CPU sits idle if no multiprogramming Instead the OS can give CPU to another process CPU burst distribution Distribution for frequency vs duration of CPU bursts. Exponential or hyperexpoinential in nature Alternating Sequence of CPU And I/O Bursts Histogram of CPU-burst Times CPU Scheduler Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them : Short-term Scheduler The ready queue is not necessarily a FIFO queue CPU scheduling decisions may take place when a process: 1.Switches from running to waiting state 2.Switches from running to ready state 3.Switches from waiting to ready 4. Terminates Scheduling under 1 and 4 leaves us no choice But option 2 and 3 does. CPU Scheduler Nonpreemptive : once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU either Scheduling under 1 and 4 is Nonpreemptive/Cooperative All other scheduling is Preemptive Preemptive Scheduling incurs some costs : Access to shared data Effects on the design of operating system kernel Effects of interrupts 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 to restart that program Dispatch latency time it takes for the dispatcher to stop one process and start another running First-Come, First-Served (FCFS) Scheduling 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: ( )/3 = 17 P1P1 P2P2 P3P FCFS Scheduling (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: ( )/3 = 3 Much better than previous case So Average waiting time vary substantially if the burst time for processes vary Generally quite long. This is non-preemptive in nature troublesome for time sharing system P1P1 P3P3 P2P 5: CPU-Scheduling17 Process Arrival Service Time Time P1P2P3P4 FCFS Average wait = ( (8-0) + (12-1) + (21-2) + (26-3) )/4 = 61/4 = FCFS Algorithm Residence Time at the CPU Shortest-Job-First (SJF) Scheduling Associate with each process the length of its next CPU burst. Using these lengths to schedule the process with the shortest time If two processes have the same length next CPU burst, FCFS scheduling is used to break the tie. Scheduling depends on the length of the next CPU burst(lower the better) SJF is optimal gives minimum average waiting time for a given set of processes Difficulty knowing the length of the next CPU request Shortest-Job-First (SJF) Scheduling Example of SJF ProcessBurst Time P16 P16 P 2 8 P37 P37 P43 P43 SJF scheduling chart Average waiting time = ( ) / 4 = 7 By moving a short process before a long one, the waiting time of the short process decreases more than it increases the waiting time of the long process. Consequently, the average waiting time decreases. SJF scheduling is used frequently in long-term scheduling. P4P4 P3P3 P1P P2P2 24 Determining Length of Next CPU Burst How to implement it at short-term scheduler ? Can only estimate the length Can be done by using the length of previous CPU bursts, using exponential averaging Examples of Exponential Averaging =0 n+1 = n Recent history does not count =1 n+1 = t n Only the actual last CPU burst counts If we expand the formula, we get: n+1 = t n +(1 - ) t n -1 + +(1 - ) j t n -j + +(1 - ) n +1 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor Prediction of the Length of the Next CPU Burst 5: CPU-Scheduling26 Process Arrival Service Time Time P2P4P1P3 Preemptive Shortest Job First Average wait = ( (10-1) + (1-1) + (17-2) + (5-3) )/4 = 26/4 = 6.5 P1 1 Preemptive SJF Algorithm Shortest Remaining Time First Algorithm Priority Scheduling SJF is a special case of the general priority schedule algorithm Priority inverse of the next CPU Burst A priority number (integer) is associated with each process The CPU is allocated to the process with the highest priority (smallest integer highest priority) Preemptive Nonpreemptive Problem Starvation low priority processes may never execute Solution Aging as time progresses increase the priority of the process Priority Scheduling Average Waiting Time = ( )/5 = 8.2 What about fairness ? What about it? Strict fixed-priority scheduling between queues is unfair (run highest, then next, etc): long running jobs may never get CPU In Multics, shut down machine, found 10-year-old job Must give long-running jobs a fraction of the CPU even when there are shorter jobs to run Tradeoff: fairness gained by hurting avg response time! How to implement fairness? Could give each queue some fraction of the CPU What if one long-running job and 100 short-running ones? Like express lanes in a supermarketsometimes express lanes get so long, get better service by going into one of the other lines Fair Share Scheduling Problems with priority-based systems Priorities are absolute: no guarantees when multiple jobs with same priority No encapsulation and modularity Behavior of a system module is unpredictable: a function of absolute priorities assigned to tasks in other modules Solution: Fair-share scheduling Each job has a share: some measure of its relative importance denotes users share of system resources as a fraction of the total usage of those resources e.g., if user As share is twice that of user B then, in the long term, A will receive twice as many resources as B Traditional implementations keep track of per-process CPU utilization (a running average) reprioritize processes to ensure that everyone is getting their share are slow! Lottery Scheduling Yet another alternative here. Give each job some number of lottery tickets On each time slice, randomly pick a winning ticket On average, CPU time is proportional to number of tickets given to each job How to assign tickets? To approximate SRTF, short running jobs get more, long running jobs get fewer To avoid starvation, every job gets at least one ticket (everyone makes progress) Advantage over strict priority scheduling: behaves gracefully as load changes Adding or deleting a job affects all jobs proportionally, independent of how many tickets each job possesses Lottery Scheduling Example Assume short jobs get 10 tickets, long jobs get 1 ticket Round Robin (RR) Designed specially for time sharing system. Each process gets a small unit of CPU time (time quantum), usually milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue. The ready queue generally is circular queue. Avg wait time is often long 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 FIFO q small q must be large with respect to context switch, otherwise overhead is too high ; processor sharing Example of RR with Time Quantum = 4 ProcessBurst Time P 1 24 P 2 3 P 3 3 The Gantt chart is: Avg Waiting Time = ((10-4)+4+7)/3 =5.66 Typically, higher average turnaround than SJF, but no better response P1P1 P2P2 P3P3 P1P1 P1P1 P1P1 P1P1 P1P Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum Multilevel Queue Ready queue is partitioned into separate queues: 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 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 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 Example of Multilevel Feedback Queue Three queues: Q 0 RR with time quantum 8 milliseconds Q 1 RR time quantum 16 milliseconds Q 2 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. Thread Scheduling : Contention Scope The contention scope of a user thread defines how it is mapped to a kernel thread. System contention scope/global contention scope user thread is a user thread that is directly mapped to one kernel thread. All user threads in a 1:1 thread model have system contention scope. Process contention scope/local contention scope user thread is a user thread that shares a kernel thread with other (process contention scope) user threads in the process. All user threads in a M:1 thread model have process contention scope. Contention Scope In an M:N thread model, user threads can have either system or process contention scope Mixed Scope The concurrency level is a property of M:N threads libraries. It defines the number of VPs used to run the process contention scope user threads. This number cannot exceed the number of process contention scope user threads, and is usually dynamically set by the threads library. Pthread Scheduling Pthread API allows specifying either PCS or SCS during thread creation PTHREAD_SCOPE_ PROCESS schedules threads using PCS scheduling PTHREAD_SCOPE_SYSTEM schedules threads using SCS scheduling. Two functions for setting and getting : pthread_attr_setscope(pthread_attr_t *attr, int scope) pthread_attr_getscope(pthread_attr_t *attr, int scope) #include #define NUM THREADS 5 int main(int argc, char *argv[]) { int i; pthread_t tid[NUM THREADS]; pthread _attr_t attr; /* get the default attributes */ pthread_attr_init(&attr); /* set the scheduling algorithm to PROCESS or 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 */ for (i = 0; i < NUM THREADS; i++) pthread_join(tid[i], NULL); } /* Each thread will begin control in this function */ void *runner(void *param){ printf("I am a thread\n"); pthread_exit(0); } Multiple-Processor Scheduling CPU scheduling more complex when multiple CPUs are available Here we consider homogeneous processors within a multiprocessor Asymmetric multiprocessing only one processor accesses the system data structures, alleviating the need for data sharing Symmetric multiprocessing (SMP) each processor is self- scheduling, all processes in common ready queue, or each has its own private queue of ready processes Processor affinity Process has affinity for processor on which it is currently running For avoiding to invalidate and repopulate caches due to migration Soft Affinity (Ex : Solaris) Hard Affinity (Ex : Linux, Solaris) NUMA and CPU Scheduling Multiple-Processor Scheduling : Load Balancing Distributing the load Necessary for system, where each processor has its own private queue. Approaches : Push Migration Pull Migration Generally implemented together It often counteracts the benefit of processor affinity. Multicore 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 Virtualization It creates fake impression of the CPU(resources) for the guest operating systems running in virtual machines. E.g Running a time sharing system or real time OS in virtual machine Thus it can undo the good scheduling efforts. Algorithm Evaluation Need to make our criteria more specific by adding constraints Approaches : Analytic Evaluation Deterministic modeling takes a particular predetermined workload and defines the performance of each algorithm for that workload It requires exact input and outcome is bound to the defined cases. Deterministic Modelling ProcessBurst Time P 1 10 P 2 29 P 3 3 P 4 7 P 5 12 FIFO Non preemptive SJF RR with TQ=10 Queueing Models Basically analysis comes from different distributions Considers a computer system as a network of servers where each server has a queue off waiting processes. Computes utilization, avg queue length, avg wait time through arrival and service rates This is queueing network analysis Littles Formula : n= *W Has limitations on the classes of algorithm it could be applied. Mathematical equations does not always represent realistic situations Evaluation of CPU schedulers by Simulation Implementation : High Cost for evaluation Dynamic nature of coders/users Best to have options for fine tuning. Linux Scheduling Preemptive priority based implementation Two priority ranges: time-sharing and real-time Real-time range from 0 to 99 and nice value from 100 to 140 List of Tasks Indexed According to Priorities n Kernel maintains list of all runnable tasks in a runqueue data structure n Each process maintains own runqueue n Each runqueue contains two priority arrays : active & expired Solaris Scheduling End of Lecture 5