Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operating Systems Scheduling. Scheduling Short term scheduler (CPU Scheduler) –Whenever the CPU becomes idle, a process must be selected for execution.

Similar presentations


Presentation on theme: "Operating Systems Scheduling. Scheduling Short term scheduler (CPU Scheduler) –Whenever the CPU becomes idle, a process must be selected for execution."— Presentation transcript:

1 Operating Systems Scheduling

2 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

3 When to select a new process to Run Four circumstances New Exit Admit Release Running Ready Dispatch Time-out Blocked Event occurs Event wait 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc 2. Interrupt occurs, move from Running to Ready 3. Event I/O Completion/ exit(0) / V() / Release() 4. A Process terminates

4 Only the case 1 and 4 Must select a new process, if any, from the Ready Queue Non Preemptive Scheduling New Exit Admit Release Running Ready Dispatch Time-out Blocked Event occurs Event wait 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc 4. A Process terminates

5 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

6 All four cases, 1,2,3 and 4 Preemptive Scheduling New Exit Admit Release Running Ready Dispatch Time-out Blocked Event occurs Event wait 1. Wait for I/O/ waitpid()/ P()/ Acquire() etc 4. A Process terminates 2. Interrupt occurs, move from Running to Ready 3. Event I/O Completion/ exit(0) / V() / Release()

7 All four cases, 1,2,3 and 4 Preemptive Scheduling New Exit Admit Release Running Ready Dispatch Time-out Blocked Event occurs Event wait 2. Interrupt occurs, move from Running to Ready 3. Event I/O Completion/ exit(0) / V() / Release() 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

8 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

9 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

10 Turnaround Time –How long it take to execute a Process –Turnaround = Completion_Time – Submission_Time –Turnaround = Wait_Time GetIntoMemory + Wait_Time ReadyQueue + Wait_Time BlockQueue + CPU_Execution_Time Optimization Criteria

11 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

12 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

13 Optimization Criteria - Summary We would like to Maximize –CPU Utilization –Throughput And Minimize –Turnaround Time –Waiting Time –Response Time

14 Scheduling Algorithms First come, First serve Shortest Job First Priority Scheduling Round-Robin Scheduling Multi-level Queue Scheduling Multi-level Feed back queue Scheduling

15 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

16 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 time 100 101 103 cpu A BC Average Waiting Time = (0 + 100 + 101) / 3 = 67

17 First come, First serve Average Waiting Time = (0 + 1 + 3) / 3 = 1.33 Now if they arrive in the order B, C, A cpu time 1 3 103 A BC

18 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

19 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…)

20 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 time A B C A C A What is avg completion time? = (103 + 2 + 5) / 3

21 Round Robin’s Disadvantage Good for Varying sized jobs But what about same-sized jobs? Assume 2 jobs of time =100 each: time 1 2 3 4 5 199 200 CPU A B A B A B A B A Avg completion time? (200 + 200) / 2 = 200 How does this compare with FCFS for same two jobs? (100 + 200) / 2 = 150

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

23 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

24 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

25 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

26 Shortest Job First (SJF) Consider 4 jobs, a, b, c, d, run in lexical order CPU time D ACB a a+b a+b+c a+b+c+d  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

27 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) cpu time A BC 1 3 103 Average completion = (1+3+103) / 3 = ~35

28 ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SJF (non-preemptive) Example of Non-Preemptive SJF P1P1 P3P3 P2P2 73160 P4P4 812 Average waiting time = (0 + 6 + 3 + 7)/4 = 4

29 ProcessArrival TimeBurst Time P 1 0.07 P 2 2.04 P 3 4.01 P 4 5.04 SJF (preemptive) P1P1 P3P3 P2P2 42 11 0 P4P4 57 P2P2 P1P1 16 Example of Preemptive SJF Average waiting time = (9 + 1 + 0 + 2)/4 = 3

30 I/O idle ~90% RR with 100ms time slice: SJF vs. RR Two processes P1, P2 P1 P2 I/O idle P2 100ms 1ms P2 100ms 1ms P1 SJF Offers better I/O utilization blocked 10ms 1ms 10ms 1ms 10ms 1ms …. blocked I/O busy I/O idle...

31 Shortest Job First The most important issue in SJF –Accuracy in estimation of Job length

32 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

33 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

34 Multilevel Queue Scheduling Example: Foreground Processes Background Processes FCFS RR Priority Scheduling

35 Multilevel Queue Scheduling Example: System Processes Interactive Processes Interactive editing Processes Student Processes Batch Processes

36 Multilevel Queue Scheduling System Processes Interactive Processes Interactive editing Processes Student Processes Batch Processes 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

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

38 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

39 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

40 Multilevel Feedback Queue Scheduling Example: Three queues:  Q 0 – RR time quantum 8 milliseconds  Q 1 – RR time quantum 16 milliseconds  Q 2 – FCFS Scheduling  A new job enters queue Q 0 served by RR.  Then job receives 8 milliseconds.  If not finished in 8 milliseconds, moved to Q 1.  At Q 1 job served by RR.  Then receives 16 milliseconds.  If not complete, preempted and moved to Q 2.


Download ppt "Operating Systems Scheduling. Scheduling Short term scheduler (CPU Scheduler) –Whenever the CPU becomes idle, a process must be selected for execution."

Similar presentations


Ads by Google