+ All Categories
Home > Documents > Operating Systems Scheduling

Operating Systems Scheduling

Date post: 01-Jan-2016
Category:
Upload: shana-logan
View: 27 times
Download: 0 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
Popular Tags:
40
Operating Systems Scheduling
Transcript
Page 1: Operating Systems Scheduling

Operating Systems Scheduling

Page 2: 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– Unordered linked list etc

Page 3: Operating Systems Scheduling

When to select a new process to Run

• Four circumstances

NewNew ExitExitAdmit Release

RunningRunningReadyReady

Dispatch

Time-out

BlockedBlocked

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

Page 4: Operating Systems Scheduling

• Only the case 1 and 4• Must select a new process, if any, from the Ready

Queue

Non Preemptive Scheduling

NewNew ExitExitAdmit Release

RunningRunningReadyReady

Dispatch

Time-out

BlockedBlocked

Event occurs

Event wait

1. Wait for I/O/ waitpid()/

P()/ Acquire() etc

4. A Process terminates

Page 5: Operating Systems Scheduling

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

Page 6: Operating Systems Scheduling

• All four cases, 1,2,3 and 4

Preemptive Scheduling

NewNew ExitExitAdmit Release

RunningRunningReadyReady

Dispatch

Time-out

BlockedBlocked

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

Page 7: Operating Systems Scheduling

• All four cases, 1,2,3 and 4

Preemptive Scheduling

NewNew ExitExitAdmit Release

RunningRunningReadyReady

Dispatch

Time-out

BlockedBlocked

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

Page 8: Operating Systems Scheduling

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

Page 9: Operating Systems Scheduling

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

Page 10: Operating Systems Scheduling

• 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

Page 11: Operating Systems Scheduling

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

Page 12: Operating Systems Scheduling

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

Page 13: Operating Systems Scheduling

Optimization Criteria - Summary

• We would like to Maximize – CPU Utilization– Throughput

• And Minimize – Turnaround Time– Waiting Time– Response Time

Page 14: Operating Systems Scheduling

Scheduling Algorithms

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

Page 15: Operating Systems 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

Page 16: Operating Systems Scheduling

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

Average Waiting Time = (0 + 100 + 101) / 3 = 67

Page 17: Operating Systems Scheduling

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

AB C

Page 18: Operating Systems Scheduling

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

Page 19: Operating Systems Scheduling

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

Page 20: Operating Systems Scheduling

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

time

A B CA CA

What is avg completion time? = (103 + 2 + 5) / 3

Page 21: Operating Systems Scheduling

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 200CPU A BA BA

BA BA

• Avg completion time?• (200 + 200) / 2 = 200• How does this compare with FCFS for same two jobs?• (100 + 200) / 2 = 150

Page 22: Operating Systems Scheduling

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.

Page 23: Operating Systems Scheduling

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

Page 24: Operating Systems Scheduling

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

Page 25: Operating Systems Scheduling

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

Page 26: Operating Systems Scheduling

Shortest Job First (SJF)• Consider 4 jobs, a, b, c, d, run in lexical order

CPU

time

DA CBa a+b a+b+c a+b+c+d

The first (a) finishes at time a The second (b) finishes at time a+bThe third (c) finishes at time a+b+cThe fourth (d) finishes at time a+b+c+dTherefore average completion = (a + ( a + b) + (a +b+c) + (a + b + c + d))/4 = (4a+3b+2c+d)/4Minimizing this requires a <= b <= c <= d.or Shortest Job First

Page 27: Operating Systems Scheduling

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

AB C

1 3 103

Average completion = (1+3+103) / 3 = ~35

Page 28: Operating Systems Scheduling

Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4

• SJF (non-preemptive)

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Average waiting time = (0 + 6 + 3 + 7)/4 = 4

Page 29: Operating Systems Scheduling

Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4

• SJF (preemptive)

P1 P3P2

42 110

P4

5 7

P2 P1

16

Example of Preemptive SJF

Average waiting time = (9 + 1 + 0 + 2)/4 = 3

Page 30: Operating Systems Scheduling

• I/O idle ~90%

• RR with 100ms time slice:

SJF vs. RR • Two processes P1, P2

P1

P2

I/O idle

P2100ms 1ms

P2100ms 1ms

P1 P1

• SJF Offers better I/O utilization

blocked blocked10ms 1ms 10ms 1ms 10ms 1ms ….

blockedblocked

I/O busy I/O idle ...

Page 31: Operating Systems Scheduling

Shortest Job First

• The most important issue in SJF – Accuracy in estimation of Job length

Page 32: Operating Systems Scheduling

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

Page 33: Operating Systems Scheduling

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

Page 34: Operating Systems Scheduling

Multilevel Queue Scheduling

• Example:

Foreground Processes

Background Processes

FCFS

RRPriorityScheduling

Page 35: Operating Systems Scheduling

Multilevel Queue Scheduling• Example:

System Processes

Interactive Processes

Interactive editing Processes

Student Processes

Batch Processes

Page 36: Operating Systems Scheduling

Multilevel Queue SchedulingSystem 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

Page 37: Operating Systems Scheduling

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.

Page 38: Operating Systems Scheduling

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

Page 39: Operating Systems Scheduling

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

Page 40: Operating Systems Scheduling

Multilevel Feedback Queue Scheduling

•Example: Three queues: Q0 – RR time quantum 8 millisecondsQ1 – RR time quantum 16 millisecondsQ2 – FCFS

•SchedulingA new job enters queue Q0 served 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.


Recommended