CGS 3763 Operating Systems Concepts Spring 2013 Dan

  • Slides: 19
Download presentation
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office

CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: 30 - 12: 30 AM

Lecture 9 – Monday, January 28, 2013 n n Last time: Today: ¨ n

Lecture 9 – Monday, January 28, 2013 n n Last time: Today: ¨ n CPU scheduling Next time 10/3/2020 2

Scheduling n n Basic concepts; scheduling objectives. Scheduling policies ¨ ¨ ¨ n n

Scheduling n n Basic concepts; scheduling objectives. Scheduling policies ¨ ¨ ¨ n n First-Come First-Serve (FCFS) Shortest Job First (SJF) Round Robin (RR) Preemptive/non-preemptive scheduling Priority inversion Schedulers CPU burst. Estimation of the CPU burst ¨ Multi-level queues with feedback ¨ Example: the UNIX scheduler ¨ Lecture 20 3

Scheduling – basic concepts n n Scheduling assigning jobs to machines. A schedule S

Scheduling – basic concepts n n Scheduling assigning jobs to machines. A schedule S a plan on how to process N jobs using one or machines. Scheduling in the general case in a NP complete problem. A job 1 <= j <- N is characterized by ¨ ¨ ¨ n Ci S completion time of job j under schedule S pi processing time ri release time; the time when the job is available for processing di due time ; the time when the job should be completed. ui =0 if Ci S <= di and ui =1 otherwise Lj = Ci S - di lateness A schedule S is characterized by The makespan Cmax = max Ci S ¨ Average completion time ¨ Lecture 20 4

Scheduling objectives n n n Performance metrics: ¨ CPU Utilization Fraction of time CPU

Scheduling objectives n n n Performance metrics: ¨ CPU Utilization Fraction of time CPU does useful work over total time ¨ Throughput Number of jobs finished per unit of time ¨ Turnaround time Time spent by a job in the system ¨ Response time Time to get the results ¨ Waiting time Time waiting to start processing All these are random variables we are interested in averages!! The objectives - system managers (M) and users (U): ¨ Maximize CPU utilization M ¨ Maximize throughput M ¨ Minimize turnaround time U ¨ Minimize waiting time U ¨ Minimize response time U Lecture 20 5

Scheduling policies n n Burst time required by a thread to use the processor/core

Scheduling policies n n Burst time required by a thread to use the processor/core Time slice/quantum time a thread is allowed to use the processor/core Preemptive scheduling A thread could be forced to release the control of the processor Policies First-Come First-Serve FCFS ¨ Shortest Job First SJF ¨ Round Robin RR ¨ Lecture 20 6

First-Come First-Served (FCFS) n Thread Burst Time P 1 24 P 2 3 P

First-Come First-Served (FCFS) n Thread Burst Time P 1 24 P 2 3 P 3 3 Processes arrive in the order: P 1 P 2 P 3 Gantt Chart for the schedule: P 1 0 n n n P 2 24 P 3 27 30 Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17 Convoy effect short process behind long process Lecture 20 7

The effect of the release time on FCFS scheduling n n Now threads arrive

The effect of the release time on FCFS scheduling n n Now threads arrive in the order: P 2 P 3 P 1 Gantt chart: P 2 0 n n n P 3 3 P 1 6 30 Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better!! Lecture 20 8

Shortest-Job-First (SJF) n n n Use the length of the next burst to schedule

Shortest-Job-First (SJF) n n n Use the length of the next burst to schedule thread/process with the shortest time. SJF is optimal minimum average waiting time for a given set of threads/processes Two schemes: Non-preemptive the thread/process cannot be preempted until completes its burst ¨ Preemptive if a new thread/process arrives with burst length less than remaining time of current executing process, preempt. known as Shortest-Remaining-Time-First (SRTF) ¨ Lecture 20 9

Example of non-preemptive SJF Thread n Release time Burst Time 0. 0 2. 0

Example of non-preemptive SJF Thread n Release time Burst Time 0. 0 2. 0 4. 0 5. 0 7 4 1 4 P 1 P 2 P 3 P 4 SJF (non-preemptive) P 1 0 n 3 P 3 7 P 2 8 Average waiting time = (0 + 6 + 3 + 7)/4 Lecture 20 P 4 12 16 =4 10

Example of Shortest-Remaining-Time-First (SRTF) (Preemptive SJF) Thread n Burst time P 1 0. 0

Example of Shortest-Remaining-Time-First (SRTF) (Preemptive SJF) Thread n Burst time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 Shortest-Remaining-Time-First P 1 0 n Release time P 2 2 P 3 4 7 4 1 4 P 2 5 P 4 7 P 1 11 16 Average waiting time = (9 + 1 + 0 +2)/4 = 3 Lecture 20 11

Round Robin (RR) n n n Each process gets a small unit of CPU

Round Robin (RR) n n n Each process gets a small unit of CPU time (time quantum), usually 10 -100 milliseconds. After this time has elapsed, the thread/process is preempted and added to the end of the ready queue. If there are n threads/processes in the ready queue and the time quantum is q, then each thread/process gets 1/n of the processor time in chunks of at most q time units at once. No thread/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 Lecture 20 12

RR with time slice q = 20 Thread P 1 P 2 P 3

RR with time slice q = 20 Thread P 1 P 2 P 3 P 4 P 1 0 P 2 20 37 P 3 Burst Time 53 17 68 24 P 4 57 P 1 77 P 3 P 4 P 1 P 3 97 117 121 134 154 162 Typically, higher average turnaround than SJF, but better response Lecture 20 13

Time slice (quantum) and context switch time Lecture 20 14

Time slice (quantum) and context switch time Lecture 20 14

Turnaround time function of time quantum Lecture 20 15

Turnaround time function of time quantum Lecture 20 15

Job Release time Work Start time Finish time Wait time Time in system till

Job Release time Work Start time Finish time Wait time Time in system till start A 0 3 0 3 B 1 5 3 3+5=8 3– 1=2 8– 1=7 C 3 2 8 8 + 2 = 10 8– 3=5 10 – 3 = 7 A 0 3 0 3 B 1 5 5 5 + 5 = 10 4 10 – 1 = 9 C 3 2 3 3+2=5 0 5– 3=2 A 0 3 0 6– 0=6 B 1 5 1 10 1– 1=0 10 – 1 = 9 C 3 2 5 8 5– 3=2 8– 3=5 Lecture 20 16

Scheduling policy Average waiting time till the job started Average time in system FCFS

Scheduling policy Average waiting time till the job started Average time in system FCFS 7/3 17/3 SJF 4/3 14/3 RR 3/3 20/3 Lecture 20 17

Priority scheduling n Each thread/process has a priority and the one with the highest

Priority scheduling n Each thread/process has a priority and the one with the highest priority (smallest integer highest priority) is scheduled next. Preemptive ¨ Non-preemptive ¨ n n SJF is a priority scheduling where priority is the predicted next CPU burst time Problem Starvation – low priority threads/processes may never execute Solution to starvation Aging – as time progresses increase the priority of the thread/process Priority my be computed dynamically Lecture 20 18

Priority inversion n n n A lower priority thread/process prevents a higher priority one

Priority inversion n n n A lower priority thread/process prevents a higher priority one from running. T 3 has the highest priority, T 1 has the lowest priority; T 1 and T 3 share a lock. T 1 acquires the lock, then it is suspended when T 3 starts. Eventually T 3 requests the lock and it is suspended waiting for T 1 to release the lock. T 2 has higher priority than T 1 and runs; neither T 3 nor T 1 can run; T 1 due to its low priority, T 3 because it needs the lock help by T 1. Allow a low priority thread holding a lock to run with the higher priority of the thread which requests the lock Lecture 20 19