410 likes | 559 Views
Operating Systems Scheduling. Scheduling. Short term scheduler (CPU Scheduler) Whenever the CPU becomes idle, a process must be selected for execution The Process is selected from the Ready queue Ready queue is not necessarily a FIFO queue It can be Priority based A Tree
E N D
Scheduling • Short term scheduler (CPU Scheduler) • Whenever the CPU becomes idle, a process must be selected for execution • The Process is selected from the Ready queue • Ready queue is not necessarily a FIFO queue • It can be • Priority based • A Tree • Unordered linked list etc
When to select a new process to Run 2. Interrupt occurs, move from Running to Ready • Four circumstances Dispatch Admit Release New Ready Running Exit Time-out 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc 3. Event I/O Completion/ exit(0) / V() / Release() Event occurs Event wait Blocked 4. A Process terminates
Non Preemptive Scheduling • Only the case 1 and 4 • Must select a new process, if any, from the Ready Queue Dispatch Release Admit New Ready Running Exit Time-out 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc Event occurs Event wait Blocked 4. A Process terminates
Non Preemptive Scheduling • Once the CPU has been allocated to a process • The process keeps it until • It Terminates • Or has to wait for: • I/O • Mutex • Child process • Semaphore • Conditional Variables etc • There is no way, to get the CPU back, FORCEFULLY
Preemptive Scheduling 2. Interrupt occurs, move from Running to Ready • All four cases, 1,2,3 and 4 Dispatch Release Admit New Ready Running Exit Time-out 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc 3. Event I/O Completion/ exit(0) / V() / Release() Event occurs Event wait Blocked 4. A Process terminates
Preemptive Scheduling 2. Interrupt occurs, move from Running to Ready • All four cases, 1,2,3 and 4 Dispatch Release Admit New Ready Running Exit Time-out In case of 2 and 3, there is a choice Whether to continue, with the same process or select a new one from the ready queue 3. Event I/O Completion/ exit(0) / V() / Release() Event occurs Event wait Blocked
Scheduling Issues • Fairness • Don’t starve process • Priorities • Most important first • Deadlines • Task X must be done by time t • Optimization • Throughput, response time • Reality - No universal scheduling policy • Many models
Optimization Criteria • CPU Utilization • Keep the CPU as busy as possible • May range from 0% to 100% • Throughput • Number of processes completed per unit time • E.g. long processes • 1 process / hr • Short processes • 10 processes / hr
Turnaround Time How long it take to execute a Process Turnaround = Completion_Time – Submission_Time Turnaround = Wait_TimeGetIntoMemory + Wait_TimeReadyQueue + Wait_TimeBlockQueue + CPU_Execution_Time Optimization Criteria
Optimization Criteria • Scheduling Algorithm does not effect the waiting time in Block Queue • It only effect the Waiting Time in the Ready Queue • Waiting Time • Sum of the periods spent waiting in the Ready Queue
Optimization Criteria • Turnaround Time is not a good criteria for Interactive Systems • A process may • Produce “Some” output • Computes new results, while previous results are output to the user • Response Time • Response_Time = First_Response_Start_Time – Submission_Time
Optimization Criteria - Summary • We would like to Maximize • CPU Utilization • Throughput • And Minimize • Turnaround Time • Waiting Time • Response Time
Scheduling Algorithms • First come, First serve • Shortest Job First • Priority Scheduling • Round-Robin Scheduling • Multi-level Queue Scheduling • Multi-level Feed back queue Scheduling
First come, First serve • Simplest scheduling algorithm: • Run jobs in order that they arrive • Uni-programming: • Run until done • Multi-programming: • Run until done or Blocks on I/O • Nonpreemptive • A Process keeps CPU until done or I/O • Advantage: • Simplicity
First come, First serve • Disadvantage • Wait time depends on arrival order • Unfair to later jobs • (worst case: long job arrives first) • Three jobs (times: A=100, B=1, C=2) arrive in the order A, B, C B C cpu A time 100 101 103 = 67 = (0 + 100 + 101) / 3 Average Waiting Time
B C A time 1 3 103 First come, First serve • Now if they arrive in the order B, C, A cpu = (0 + 1 + 3) / 3 = 1.33 Average Waiting Time
FCFS Convoy effect • A CPU bound job will hold CPU until • Terminates • Or it causes an I/O burst • Rare occurrence, since the thread is CPU-bound • Long periods where no I/O requests issued, and CPU held • Result: • Poor I/O device utilization
FCFS Convoy effect : Example • One CPU bound job, many I/O bound • CPU bound runs • I/O jobs blocked in ready queue • I/O devices idle • CPU bound blocks • I/O bound job(s) run, quickly block on I/O • CPU bound runs again • I/O of the I/O bound jobs completes • CPU bound still runs while I/O devices idle (continues…)
Round robin (RR) • Solution to job monopolizing CPU? • Interrupt it. • Run job for some “time slice,” • When time is up, or it blocks • It moves to back of a FIFO queue • Advantage: • Fair allocation of CPU across jobs • Low average waiting time when job lengths vary 1 2 3 4 5 103 CPU B C C A A A time What is avg completion time? = (103 + 2 + 5) / 3
1 2 3 4 5 199 200 CPU B B B B A A A A A Round Robin’s Disadvantage • Good for Varying sized jobs • But what about same-sized jobs? • Assume 2 jobs of time =100 each: time • Avg completion time? • (200 + 200) / 2 = 200 • How does this compare with FCFS for same two jobs? • (100 + 200) / 2 = 150
RR Time slice tradeoffs • Performance depends on length of the time slice • Context switching isn’t a free operation. • If timeslice time is set too high (attempting to amortize context switch cost) • You get FCFS. • i.e. Processes will finish or block before their slice is up anyway • If it’s set too low you’re spending all of your time context switching between threads.
Priority scheduling • Not all jobs equal • So: rank them. • Each process has a priority • Run highest priority ready job in system • Priorities can be static or dynamic or both • Among the Processes of equal priority • Round robin • FCFS
Priority scheduling • Priority scheduling can be Preemptive or Non-Preemptive • When a process arrives and enters the Ready Queue • Its priority is compared with the currently Running Process • If Higher • Preemptive Scheduling • Run the New Thread • Non-Preemptive Scheduling • Continue running the Current Thread
Priority scheduling • High priority always runs over low priority. • Starvation • A low Priority process may indefinitely wait for the CPU • Solution: Aging • Gradually increase the Priority of processes that wait in the system for a long time. • Which type of processes should be given Higher Priority: • I/O Bound??? • CPU Bound??? • In order to keep I/O busy increase priority for jobs that often block on I/O
Shortest Job First (SJF) • Consider 4 jobs, a, b, c, d, run in lexical order a a+b a+b+c a+b+c+d A B C CPU D time • The first (a) finishes at time a • The second (b) finishes at time a+b • The third (c) finishes at time a+b+c • The fourth (d) finishes at time a+b+c+d • Therefore average completion = (a + ( a + b) + (a +b+c) + (a + b + c + d))/4 = (4a+3b+2c+d)/4 • Minimizing this requires a <= b <= c <= d. • or Shortest Job First
Shortest Job First (SJF) • Run whatever job has smallest next CPU burst • Can be pre-emptive or non-pre-emptive • Example: same jobs (given jobs A, B, C) 1 3 103 cpu B C A time Average completion = (1+3+103) / 3 = ~35
P1 P3 P2 P4 0 3 7 8 12 16 Example of Non-Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (non-preemptive) Average waiting time = (0 + 6 + 3 + 7)/4 = 4
P1 P2 P3 P2 P4 P1 11 16 0 2 4 5 7 Example of Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • SJF (preemptive) Average waiting time = (9 + 1 + 0 + 2)/4 = 3
10ms 1ms10ms1ms10ms1ms …. blocked blocked blocked blocked 100ms 1ms 100ms 1ms P1 P1 P2 P2 I/O idle I/O busy SJF vs. RR • Two processes P1, P2 P1 P2 • RR with 100ms time slice: I/O idle ... • I/O idle ~90% • SJF Offers better I/O utilization
Shortest Job First • The most important issue in SJF • Accuracy in estimation of Job length
Multilevel Queue Scheduling • Sometimes processes are classified into groups • One classification can be: • Foreground (or Interactive) processes • Background (or batch) processes • Different response time requirement • => Different scheduling requirements • Foreground processes usually have higher priorities
Multilevel Queue Scheduling (MQS) • Partition the Ready queue into a number of queues • Processes are permanently assigned to one of the queues • Each queue may have its own scheduling algorithm • In addition, there must be scheduling between the queues
Multilevel Queue Scheduling • Example: FCFS Foreground Processes Priority Scheduling RR Background Processes
Multilevel Queue Scheduling • Example: System Processes Interactive Processes Interactive editing Processes Batch Processes Student Processes
System Processes Interactive Processes Interactive editing Processes Batch Processes Student Processes Multilevel Queue Scheduling • Each queue may have absolute priority over the other queue • Alternatively, Time slice between the queues • Time slots can be equal • Or • 80% time for Foreground processes • 20% time for Background processes
Multilevel Feedback Queue Scheduling • In Multilevel Queue a process is permanently assigned to a queue • The queue to which a process should belong is decided statically • Multilevel Feedback Queue Scheduling: • A Process may move between the Queues • Aging can be implemented this way.
Multilevel Feedback Queue Scheduling • Multilevel-feedback-queue scheduler defined by: • Number of queues • Scheduling algorithms for each queue • Method used to select when upgrade process • Method used to select when demote process • Method used to determine which queue a process will enter when that process needs service
Multilevel Feedback Queue Scheduling • Example • If a process used too much CPU time, then move it to a lower-priority queue • If a process waits too long in a lower priority queue, then move it to a higher priority queue
Multilevel Feedback Queue Scheduling • Example: Three queues: • Q0 – RR time quantum 8 milliseconds • Q1 – RR time quantum 16 milliseconds • Q2 – FCFS • Scheduling • A new job enters queue Q0served by RR. • Then job receives 8 milliseconds. • If not finished in 8 milliseconds, moved to Q1. • At Q1 job served by RR. • Then receives 16 milliseconds. • If not complete, preempted and moved to Q2.