Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems CMPSC 473 Processes (contd.) September 01, 2010 - Lecture 4 Instructor: Bhuvan Urgaonkar.

Similar presentations


Presentation on theme: "Operating Systems CMPSC 473 Processes (contd.) September 01, 2010 - Lecture 4 Instructor: Bhuvan Urgaonkar."— Presentation transcript:

1 Operating Systems CMPSC 473 Processes (contd.) September 01, 2010 - Lecture 4 Instructor: Bhuvan Urgaonkar

2 Process Switch Suspend the current process and resume a previously suspended process –Also called context switch or task switch

3 Process Switch Process 0 Process 1 A1 A2 B1 B2 B3 B4 Context_switch() { } Involuntary/Voluntary

4 Process Switch What does the kernel need to save when suspending a process? –Hint: The entire address space is already saved (either in memory or on swap space). What else would the process need when it has to be resumed? –CPU registers This is called the hardware context of the process Execution context, PC, pointers to elements within address space, page table, etc.

5 Context_switch() { push R0, R1, …// save regs on its stack PCB[curr].SP = SP // save stack pointer PCB[curr].PT = PT // save ptr(s) to address space next = schedule() // find next process to run PT = PCB[next].PT SP = PCB[next].SP pop Rn, … R0 return// NOTE: Ctrl returns to another process }

6 Overheads of Process Switch Direct –The time spent switching context Indirect –Cache pollution –TLB flush

7 CPU Scheduling

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

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

11 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

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 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 –Exercise: Prove the optimality claimed above

14 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

15 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

16 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

17 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

18 Time Quantum and Context Switch Time

19 Turnaround Time Varies With Time Quantum

20 Proportional-Share Schedulers A generalization of round robin Process P i given a CPU weight w i > 0 The scheduler needs to ensure the following –forall i, j, |T i (t 1, t 2 )/T j (t 1,t 2 ) - w i /w j | ≤ e –Given P i and P j were backlogged during [t 1,t 2 ] Who chooses the weights and how? Application modeling problem: non-trivial Many PS schedulers developed in the 90s –E.g., Start-time Fair Queueing (Qlinux UT-Austin/Umass-Amherst)

21 Lottery Scheduling [Carl Waldspurger, MIT, ~1995] Perhaps the simplest proportional-share scheduler Create lottery tickets equal to the sum of the weights of all processes –What if the weights are non-integral? Draw a lottery ticket and schedule the process that owns that ticket –What if the process is not ready? Draw tickets only for ready processes –Exercise: Calculate the time/space complexity of the operations Lottery scheduling will involve

22 Lottery Scheduling Example 14 25 36 710 811 912 13 14 15 9 P1=6P2=9 Schedule P2

23 14 25 36 710 811 912 13 14 15 3 P1=6P2=9 Schedule P1 Lottery Scheduling Example

24 14 2511 6 710 8 3912 13 14 15 11 As t ∞, processes will get their share (unless they were blocked a lot) Problem with Lottery scheduling: Only probabilistic guarantee What does the scheduler have to do –When a new process arrives? –When a process terminates? P1=6P2=9 Schedule P2 Lottery Scheduling Example


Download ppt "Operating Systems CMPSC 473 Processes (contd.) September 01, 2010 - Lecture 4 Instructor: Bhuvan Urgaonkar."

Similar presentations


Ads by Google