Summary

These lecture notes provide an overview of CPU scheduling algorithms, including FCFS, SJF, and RR. They discuss concepts like CPU bursts, I/O bursts, and dispatch latency, and cover various scheduling criteria. The notes also touch upon preemptive and non-preemptive scheduling, and the prediction of CPU burst lengths.

Full Transcript

Basic Concepts Maximum CPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait CPU burst followed by I/O burst CPU burst distribution is of main concern Histogram of CPU-burst Times CPU Scheduler...

Basic Concepts Maximum CPU utilization obtained with multiprogramming CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait CPU burst followed by I/O burst CPU burst distribution is of main concern Histogram of CPU-burst Times CPU Scheduler Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them Queue may be ordered in various ways Scheduling may be non-preemptive (e.g. FCFS, SJF and priority scheduling). Thus, CPU scheduling decisions may take place when a process: 1. Switches from running to waiting state (i.e. blocked for a resource/device, e.g. I/O read or a semaphore) Instigated by a system request (software interrupt) 2. Terminates CPU Scheduler In preemptive scheduling (e.g. round robin), the scheduler may be invoked when a process: 1. Switches from running to waiting state (i.e. blocked for a resource/device, e.g. I/O read or a semaphore) Instigated by a system request (software interrupt) 2. Switches from running to ready state Instigated by a timer interrupt (i.e. the timer tick, commonly 10 ms) 3. Switches from waiting to ready state Instigated by a hardware interrupt (e.g. an I/O completion interrupt) OR A software event (e.g. a semaphore is signalled) 4. Terminates Dispatcher Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves: switching context (process context switching) 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 pause one process and resume another Scheduling Criteria (metrics targeted for optimization) CPU utilization – keep the CPU as busy as possible executing user-mode applications, the payload (not the scheduler, which is an overhead). Throughput – # of processes that complete their execution per time unit Turnaround time – amount of time to execute a particular process (since the time it arrived/ready) Completion time – arrival time Waiting time – amount of time a process has been waiting in the ready queue Turnaround time – burst time Response time – amount of time it takes from when a request/event was submitted until the first response is produced First- Come, First-Served (FCFS) Scheduling Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P1 P2 P3 0 24 27 30 Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 FCFS Scheduling (Cont.) Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt chart for the schedule is: P2 P3 P1 0 3 6 30 Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect - short process behind long process Consider one CPU-bound (long burst) and many I/O-bound processes (short bursts) 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 SJF is optimal – gives minimum average waiting time for a given set of processes The difficulty is knowing the burst length of the next CPU request Preemptive version called shortest-remaining-time-first Example of SJF ProcessArriva l TimeBurst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 SJF scheduling chart P4 P1 P3 P2 0 3 9 16 24 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 Determining Length of Next CPU Burst Can only estimate the burst length May predict based on prior burst lengths (of same process of course) Then pick process with shortest predicted next CPU burst Can be implemented using the length of previous CPU bursts and exponential averaging 𝑡𝑛 = actual length of 𝑛𝑡ℎ CPU burst 𝜏𝑛+1 = predicted value for the next CPU burst 𝛼, 0 ≤ 𝛼 ≤ 1 𝜏𝑛+1 = 𝛼 𝑡𝑛 + 1 − 𝛼 𝜏𝑛 Commonly, α (the weight) is set to ½ Prediction of the Length of the Next CPU Burst CPU burst (𝑡𝑖 ) 6 4 6 4 13 13 13 … “guess” (𝜏𝑖 ) 10 8 6 6 5 9 11 12 Examples of Exponential Averaging  =0 n+1 = n Recent history does not count  =1 n+1 =  tn Only the actual last CPU burst counts If we expand the formula, we get: n+1 =  tn+(1 - ) tn -1 + … +(1 -  )j  tn -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 Example of Shortest-remaining-time-first Now we add the concepts of varying arrival times and preemption to the analysis ProcessAarri Arrival TimeTBurst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 Preemptive SJF Gantt Chart P1 P2 P4 P1 P3 0 1 5 10 17 26 Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec OR (turnaround time – burst time) = [(17-0-8) + (5-1-4) + (26-2-9) + (10-3-5)]/4=6.5 Priority Scheduling 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 SJF is priority scheduling where priority is the inverse of predicted next CPU burst time Problem  Starvation – low priority processes may never execute Solution  Aging – as time progresses increase the priority of the process Example of Priority Scheduling ProcessAarri Burst TimeT Priority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 Priority scheduling Gantt Chart Average waiting time = 8.2 msec Round Robin scheduling(RR) Each process gets a small unit of CPU time (time quantum q), usually 1-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. Timer interrupts every quantum to schedule next process Performance q must be large with respect to context switch, otherwise overhead is too high Example of RR with Time Quantum = 4 Process Burst Time P1 24 P2 3 P3 3 The Gantt chart is: P1 P2 P3 P1 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30 Typically, higher average turnaround than SJF, but better response q should be large compared to context switch time q usually 1ms to 100ms, context switch < 10 usec Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum 80% of CPU bursts should be shorter than q Proportional Share Scheduling Preemptive scheduling: The scheduler is invoked every timer interrupt, at an interval (time quantum) T shares (of time or virtual time) are allocated among all processes in the system An application receives N shares where N < T This ensures each application will receive N / T of the total processor time Multilevel Queue Ready queue is partitioned into separate queues (2 or more), e.g.: foreground (interactive) background (non-interactive or batch) Processes are permanently assigned to a given queue Each queue may have its own scheduling algorithm, e.g.: 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. Proportional scheduling – 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 Thread Scheduling Distinction between user-level and kernel-level threads When threads are supported, threads are scheduled, not processes. In some many-to-one and many-to-many models, the thread library schedules user-level threads (i.e. they are user-managed threads): Known as process-contention scope (PCS) since scheduling competition is within the process Typically done via priority set by programmer Kernel-managed threads are in 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 Can be limited by OS – Linux and Mac OS X only allow PTHREAD_SCOPE_SYSTEM Pthread Scheduling API #include pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); #define NUM_THREADS 5 int main(int argc, char *argv[]) { for (i = 0; i < NUM_THREADS; i++) int i, scope; pthread_t tid[NUM THREADS]; pthread_create(&tid[i],&attr,runner,NULL); pthread_attr_t attr; for (i = 0; i < NUM_THREADS; i++) pthread_attr_init(&attr); pthread_join(tid[i], NULL); } if (pthread_attr_getscope(&attr, &scope) != 0) scope\n"); else { void *runner(void *param) { if (scope == PTHREAD_SCOPE_PROCESS) printf("PTHREAD_SCOPE_PROCESS"); pthread_exit(0); else if (scope == PTHREAD_SCOPE_SYSTEM) } printf("PTHREAD_SCOPE_SYSTEM"); else fprintf(stderr, "Illegal scope value.\n"); } Real-Time CPU Scheduling Can present obvious challenges Soft real-time systems – guarantees a real-time process will be given preference over other non real-time processes. No guarantee as to when critical real-time process will be scheduled Hard real-time systems – task must be serviced by its deadline Two types of latencies affect performance 1.Interrupt latency – time from arrival of interrupt to start of interrupt service routine (ISR): Hardware stops current instruction – may be delayed if interrupts were disabled. Hardware and/or software perform interrupt context switching and then start executing the interrupt handler for the particular event or external pin. 2.Dispatch latency – time for scheduler to take current process off CPU and switch to another (process context switching) Real-Time CPU Scheduling (Cont.) Conflict phase of dispatch latency: 1.Release by a non-real-time or low-priority process of resources needed by high- priority processes Please recall the priority inversion problem we studied earlier!! 2. Preemption of any currently running process if required Priority-based Scheduling For real-time scheduling, scheduler must support preemptive, priority- based scheduling But this only guarantees soft real-time For hard real-time must also provide ability to meet deadlines Processes have new characteristics: periodic ones require CPU at constant intervals (i.e. they have periodic deadlines) Has processing time t, deadline d, period p 0≤t≤d≤p Rate of periodic task is 1/p Rate Monotonic Scheduling A priority is assigned based on the inverse of its required period (e.g. a task may require processing every 100 time units) Shorter periods = higher priority; Longer periods = lower priority Ex: P1 is assigned a higher priority than P2. Rate Monotonic scheduling P1: p=50, t=20 P2: p=100, t=35 Missed Deadlines (on P2) P1: p=50, t=25 P2: p=80, t=35

Use Quizgecko on...
Browser
Browser