Round Robin (RR) ACSC 271 Operating Systems Lecture 9: Scheduling Algorithms 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. 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 Slide 1 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 2 C.Markides ACSC 271: Operating Systems 03/12/2017 RR Example Process Burst Time P 1 24 P 2 3 P 3 3 Consider a time quantum q of 4ms. The Gantt Chart is thus given P 1 P 2 P 3 P 1 P 1 P 1 P 1 P 1 0 4 7 10 14 18 22 26 30 Slide 3 C.Markides ACSC 271: Operating Systems 03/12/2017 RR Scheduling The waiting time for P1 is 10 4 = 6. The waiting time for P2 is 4 and the waiting time for P3 is 7. The average waiting time is 17/3 = 5.66 milliseconds. The average turnaround time is 30/3 = 10 milliseconds. Typically, higher average turnaround than SJF, but better response. Slide 4 C.Markides ACSC 271: Operating Systems 03/12/2017
RR Example II Process Burst Time P 1 53 P 2 17 P 3 68 P 4 24 Consider a time quantum q of 20 ms. The Gantt Chart is thus given Time Quantum and Context Switch Time Consider switching between processes. Result: context switch 10%, then 10% is spent on context switching. Slide 5 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 6 C.Markides ACSC 271: Operating Systems 03/12/2017 Turnaround Time Varies With The Time Quantum Avg. turnaround time, does not improve by increasing the timequantum. Rule: 80% of the CPU bursts should be shorter than the time quantum. Slide 7 C.Markides ACSC 271: Operating Systems 03/12/2017 Multilevel Queue Ready queue is partitioned into separate queues: foreground (interactive processes) background (batch processes) 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 Slide 8 C.Markides ACSC 271: Operating Systems 03/12/2017
Multilevel Queue Scheduling 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 Slide 9 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 10 C.Markides ACSC 271: Operating Systems 03/12/2017 Multilevel Feedback Queue Example Three queues: Q0 RR with time quantum 8 milliseconds Q1 RR 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 milliseconds. If it still does not complete, it is preempted and moved to queue Q2. Slide 11 C.Markides ACSC 271: Operating Systems 03/12/2017 Multilevel Feedback Queues Slide 12 C.Markides ACSC 271: Operating Systems 03/12/2017
Thread Scheduling Distinction between user-level and kernel-level threads Many-to-one and many-to-many models, thread library schedules user-level threads to run on LWP Known as process-contention scope (PCS) since scheduling competition is within the process Kernel thread scheduled onto available CPU is system-contention scope (SCS) competition among all threads in system Pthread Scheduling 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. Slide 13 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 14 C.Markides ACSC 271: Operating Systems 03/12/2017 Pthread Scheduling API #include <pthread.h> #include <stdio.h> #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, RT, 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); Pthread Scheduling API /* 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); } Slide 15 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 16 C.Markides ACSC 271: Operating Systems 03/12/2017
Multiple-Processor Scheduling CPU scheduling more complex when multiple CPUs are available 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 soft affinity hard affinity Slide 17 C.Markides ACSC 271: Operating Systems 03/12/2017 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 Slide 18 C.Markides ACSC 271: Operating Systems 03/12/2017 Windows 7/10 Scheduling Windows implements a priority-driven, preemptive scheduling system at least one of the highest priority runnable (ready) threads always runs Disadvantage Certain high-priority threads ready to run might be limited by the processors on which they might be allowed or preferred to run on, a phenomenon called processor affinity. Slide 19 C.Markides ACSC 271: Operating Systems 03/12/2017 Windows 7/10 Thread Scheduling After a thread is selected to run, it runs for an amount of time called a quantum. A quantum is the length of time a thread is allowed to run before another thread at the same priority level is given a turn to run. Quantum values can vary from system to system and process to process for any of three reasons: System configuration Foreground and background process Use of the job object to alter the quantum. Slide 20 C.Markides ACSC 271: Operating Systems 03/12/2017
Windows 7/10 Priorities Thread priority levels are assigned from two different perspectives: those of the Windows API and those of the Windows kernel. The Windows API first organizes processes by the priority class to which they are assigned at creation: Real-time (4), High (3), Above Normal (7), Normal (2), Below Normal (5), and Idle (1). It then assigns a relative priority of the individual threads within those processes. Priority delta that is applied to the process base priority: Time-critical (15), Highest (2), Above-normal (1), Normal (0), Below-normal ( 1), Lowest ( 2), and Idle ( 15). Windows 7/10 Kernel Priorities Slide 21 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 22 C.Markides ACSC 271: Operating Systems 03/12/2017 Windows 7/10 Priorities Client versions of Windows incorporate a service called the Multimedia Class Scheduler Service (MMCSS), whose purpose is to ensure glitch-free multimedia playback for applications that register with it. MMCSS works by defining several tasks, including the following: Audio Capture Distribution Games Playback Pro Audio Window Manager To remedy skipping and other audio glitches have been a common source of irritation among Windows users in the past, and the usermode audio stack in Windows makes the situation worse because it offers even more chances for pre-emption. Linux Scheduling Constant order O(1) scheduling time Two priority ranges: time-sharing and realtime Real-time range from 0 to 99 and nice value from 100 to 140. Slide 23 C.Markides ACSC 271: Operating Systems 03/12/2017 Slide 24 C.Markides ACSC 271: Operating Systems 03/12/2017