Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems CSE 411 CPU Management Sept. 18 2006 - Lecture 6 Instructor: Bhuvan Urgaonkar.

Similar presentations


Presentation on theme: "Operating Systems CSE 411 CPU Management Sept. 18 2006 - Lecture 6 Instructor: Bhuvan Urgaonkar."— Presentation transcript:

1 Operating Systems CSE 411 CPU Management Sept. 18 2006 - Lecture 6 Instructor: Bhuvan Urgaonkar

2 Ready Waiting Running Disk Lock OS (scheduler) Hmm.. Who should I pick to run?

3 First-Come, First-Served Scheduling (FCFS) Process Run 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: (0 + 24 + 27)/3 = 17 P1P1 P2P2 P3P3 2427300

4 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: (6 + 0 + 3)/3 = 3 Much better than previous case Convoy effect short process behind long process P1P1 P3P3 P2P2 63300

5 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 for avg. waiting time – gives minimum average waiting time for a given set of processes –In class: Compute average waiting time for the previous example with SJF –Prove it (Homework 1, Will be handed out next Friday)

6 Architecture-dependent part of the Scheduler: Dispatcher Dispatcher module gives control of the CPU to the process selected by the 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 –Also called the Context Switch time.

7 Costs/Overheads of a Context Switch Direct/apparent –Time spent doing the switch described in the last slide –Fixed (more or less) Indirect/hidden costs –Cache pollution –Effect of TLB pollution (will study this when we get to Virtual Memory Management) –Workload dependent

8 Example from Linux 2.6.x asmlinkage void __sched schedule(void) { [... ] prepare_arch_switch(rq, next); prev = context_switch(rq, prev, next); barrier(); finish_task_switch(prev); [... ] } task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next) { struct mm_struct *mm = next->mm; struct mm_struct *oldmm = prev->active_mm; /* Here we just switch the register state and the stack. */ switch_to(prev, next, prev); return prev; } #define switch_to(prev,next,last) \ asm volatile(SAVE_CONTEXT \ "movq %rsp,%P[threadrsp](%[prev])\n\t" /* saveRSP */ \ "movq %P[threadrsp](%[next]),%rsp\n\t" /* restore RSP */ \ "call __switch_to\n\t" \ ".globl thread_return\n" \ "thread_return:\n\t" \ "movq %gs:%P[pda_pcurrent],%rsi\n\t" \ "movq %P[thread_info](%rsi),%r8\n\t" \ LOCK "btr %[tif_fork],%P[ti_flags](%r8)\n\t" \ "movq %rax,%rdi\n\t" \ "jc ret_from_fork\n\t" \ RESTORE_CONTEXT \ : "=a" (last) \ : [next] "S" (next), [prev] "D" (prev), \ [threadrsp] "i" (offsetof(struct task_struct, thread.rsp)), \ [ti_flags] "i" (offsetof(struct thread_info, flags)),\ [tif_fork] "i" (TIF_FORK), \ [thread_info] "i" (offsetof(struct task_struct, thread_info)), \ [pda_pcurrent] "i" (offsetof(struct x8664_pda, pcurrent)) \ : "memory", "cc" __EXTRA_CLOBBER)

9 When is the scheduler invoked? 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 only under 1 and 4: nonpreemptive scheduling –E.g., FCFS and SJF All other scheduling is preemptive

10 Why Pre-emption is Necessary To improve CPU utilization –Most processes are not ready at all times during their lifetimes –E.g., think of a text editor waiting for input from the keyboard –Also improves I/O utilization To improve responsiveness –Many processes would prefer “slow but steady progress” over “long wait followed by fast process” Most modern CPU schedulers are pre-emptive

11 SJF: Variations on the theme Non-preemptive: once CPU given to the process it cannot be preempted until completes its CPU burst - the SJF we already saw Preemptive: if a new process arrives with CPU length less than remaining time of current executing process, preempt. This scheme is know as Shortest-Remaining-Time-First (SRTF)  Also called Shortest Remaining Processing Time (SRPT) Why SJF/SRTF may not be practical  CPU requirement of a process rarely known in advance

12 Choosing the Right Scheduling Algorithm/Scheduling Criteria CPU utilization – keep the CPU as busy as possible Throughput – # 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 output (for time-sharing environment) Fairness

13 Round Robin (RR) 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 => FCFS –q small => q must be large with respect to context switch, otherwise overhead is too high

14 Example of RR with Time Quantum = 20 ProcessCPU Time P 1 53 P 2 17 P 3 68 P 4 24 The Gantt chart is: Typically, higher average turnaround than SJF, but better response P1P1 P2P2 P3P3 P4P4 P1P1 P3P3 P4P4 P1P1 P3P3 P3P3 02037577797117121134154162

15 Time Quantum and Context Switch Time

16 Turnaround Time Varies With Time Quantum

17 Priority-based Scheduling Associate with each process a quantity called its CPU priority At each scheduling instant –Pick the ready process with the highest CPU priority –Update (usually decrement) the priority of the process last running Priority = Time since arrival => FCFS Priority = 1/Size => SJF Priority = 1/Remaining Time => SRPT Priority = Time since last run => Round-robin (RR) UNIX variants –Priority values are positive integers with upper bounds –Decreased every quantum Fairness, avoid starvation –Increased if the process was waiting, more wait => larger increase To make interactive processes more responsive –Problems Hard to analyze theoretically, so hard to giv e any guarantees May unfairly reward blocking processes

18 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

19 Multilevel Queue Scheduling

20 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

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

22 Multilevel Feedback Queues


Download ppt "Operating Systems CSE 411 CPU Management Sept. 18 2006 - Lecture 6 Instructor: Bhuvan Urgaonkar."

Similar presentations


Ads by Google