48
Cosc 4740 Chapter 5 Process Scheduling

Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Embed Size (px)

Citation preview

Page 1: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Cosc 4740

Chapter 5

Process Scheduling

Page 2: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

CPU Scheduling

• Short-term Scheduler– Selects a process from the ready queue when

current process releases the CPU– Very Fast to avoid wasting CPU time– Very efficient

• the problem: selecting a CPU algorithm for a particular system

• Also updates PCBs and controls the queues.

Page 3: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Basic concept of a Process

• CPU–I/O Burst Cycle– Process execution

consists of a cycle of CPU execution and I/O wait

Page 4: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• How to decide which ready process to put on the CPU next?

Page 5: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Goals

1. Fairness or Waiting time• amount of time a process has been waiting in the ready queue• – No process will wait “too” long

2. CPU utilization – keep it busy most of the time• normally about 40-90% of the time

3. Response time – for interactive users4. Turnaround time

• the time of completion from submission

5. Throughput: • # of processes that complete their execution per time unit

Consider if it is possible to Max all 5 goals

Page 6: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• We want to optimize all the goals– This is not possible:

• Fairness vs CPU utilization

• All schedulers favor a couple of goals.– Normally those goals that help the kind of

system it written for.

Page 7: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

CPU Scheduler

• Selects from among the processes in memory that are ready to execute, and allocates the CPU to one of them

• 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 under 1 and 4 is nonpreemptive• All other scheduling is preemptive

Page 8: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Dispatcher• Dispatcher module gives control of the CPU to

the process selected by the short-term scheduler; this involves:– switching context– switching to user mode– jumping to the proper location in the user program to

restart that program

• Dispatch latency – time it takes for the dispatcher to stop one process and start another running

Page 9: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Types of Scheduling Algorithms

• Non-preemptive Scheduling– A running process is only suspended when it

blocks or terminates– Pros:

• Decreases turnaround time, high CPU utilization• Does not require special HW (like a timer)

– Cons:• Poor performance for response time and fairness• Limited choice of scheduling Algorithms

Page 10: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Preemptive scheduling– A Running process can be taken off the CPU for any

(or no) reason. Suspended by the scheduler

– Pros:• No limitations on the choice of scheduling algorithms

• Increased Fairness and response time

– Cons• Addition overheads (frequent context switching)

• deceased CPU utilization and longer turnaround time

Page 11: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

FCFS scheduling

• First-Come First Served (non-preemptive)– CPU is allocated to the process that requests it first

• Pros:– Simple to understand and implement

– Very fast selection for scheduling

• Cons:– Avg. waiting time is long and varies

– Not suitable for time-sharing OS

Page 12: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

FCFS Example 1

Process Burst Time

P1 24

P2 3

P3 3

• Suppose that the processes arrive in the order: P1 , P2 , P3

The Gantt Chart for the schedule is:

• Waiting time for P1 = 0; P2 = 24; P3 = 27• Average waiting time: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

Page 13: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

FCFS Example 2

Suppose that the processes arrive in the order P2 , P3 , P1

• The Gantt chart for the schedule is:

• Waiting time for P1 = 6; P2 = 0; P3 = 3• Average waiting time: (6 + 0 + 3)/3 = 3• Much better than previous case• Convoy effect short process behind long process

P1P3P2

63 300

Page 14: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

SJF Scheduling

• Shortest Job Next: Preemptive or Non-Preemptive

• Pros:– Yields minimum avg. waiting time for a given

set of processes

• Cons:– Difficult to estimate CPU burst time– Starvation (indefinite blocking)

Page 15: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Process Arrival Time Burst TimeP1 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

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

Page 16: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Example of Preemptive SJF

Process Arrival Time Burst TimeP1 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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 17: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Determining Length of Next CPU Burst

• Can only estimate the length

• Can be done by using the length of previous CPU bursts, using exponential averaging

:Define 4.

10 , 3.

burst CPU next the for value predicted 2.

burst CPU of length actual 1.

1n

thn nt

.1 1 nnn t

Page 18: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Round Robin Scheduling

• Preemptive scheduling

• Most widely used algorithm

• Very simple – CPU isn’t taxed by scheduler

• Fair

• Each Process is allowed a time interval call a “quantum” or “time slice”– Max time process can run on CPU

Page 19: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• If a process is still running at the end of a quantum, the scheduler preempts it.

• If a process blocks or terminates before it quantum expires, another is allocated immediately

CPU Ready queue

O PQRS

(Then quantum expiries)

P QRSO

Page 20: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Example of RR

Time Quantum = 20Process Burst Time

P1 53 P2 17 P3 68 P4 24

• The Gantt chart is:

• Typically, higher average turnaround than SJF, but better response time

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 21: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Quantum

• How long should a quantum be?

• Problems:– quantum is too small (20 ms)

• CPU is under utilized – to much context switching

– quantum is too large (1 second)• Not fair, poor response time

Page 22: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Typical quantum between 100ms and 200ms– When O/S is installed it is tested and adjusted

for specific environment

• OS could monitor itself, but it is complex– scheduler would not be simple– So normally done by “hand”.

Page 23: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Priority Scheduling

• Not all processes are equally important

• Each process is assigned a priority (integer value)

• Process with highest priority runs

• If more than 1 with same highest priority, then Round Robin among those processes

Page 24: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Priority Scheduling (2)• A priority number (integer) is associated with each

process• The CPU is allocated to the process with the highest

priority (smallest integer highest priority)– Preemptive

– nonpreemptive

• SJF is a priority scheduling where priority is the predicted next CPU burst time

• Problem Starvation – low priority processes may never execute

Page 25: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Priority Scheduling (3)

• Array of priority queuesHighest 0 PQR

1 OS23 A

Lowest 4 C• Priority must change over time, or low priority

processes will suffer (starve)– Solution Aging – as time progresses increase the

priority of the process

Page 26: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Multiple Queue with Dynamic Priority

• Priority of processes changes dynamically

• Processes are divided into 2 classes– CPU bound – computation intensive– I/O bound – most of time I/O is used

• Who should be giving higher priority?

Page 27: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• If you know which the process is:– I/O bound uses processor for short time, then

I/O operations– So I/O bound will leave the CPU sooner, then

CPU bound processes– So give I/O bound higher priority.

Page 28: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Should quantum size be the same for I/O and CPU bound processes?

Page 29: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Vary quantum size w/ dynamic priority– Higher priority will get lower quantum, since it

is I/O bound and won’t need it for longPriority Quantum

0 1 Unit Highest priority/lowest quantum

1 2 Units

2 4 Units

3 8 Units Lowest priority/highest quantum

Page 30: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• How do you know if the process is I/O or CPU bound??

• It can even change as a processes runs!

Page 31: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• We figure it dynamically– If a running process uses it’s quantum, then

lower the priority by 1– If a running process blocks, the raise priority by

1– Start new process with priority of 0 (highest)

• If I/O bound – we did it right

• If CPU bound – quantum is small anyway.

Page 32: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Interactive process respond quickly and are usually I/O bound.– So good response time

• Fair, since CPU bound process get a longer quantum as they run.

• Works well when a process changes from CPU to I/O bound or vise versa.

Page 33: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Multi-Processor Scheduling

• Very complex• Depends on the underlying HW structure• Heterogeneous processors: Difficult!

– Process may have code compiled for only 1 CPU.

• Homogeneous processors with separated ready queueso load balancing problem

Page 34: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Multi-Processor Scheduling (2)

• Homogeneous processors with shared ready queueo Symmetric: Processors are self-scheduling

o Need to be synchronized to access the shared ready queue to prevent multiple processors trying to load the same process in the queue.

o Asymmetric: There is one master processor who distributes next processes to the other processors.

• Simpler than symmetric approach

Page 35: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

NUMA and CPU Scheduling

Note that memory-placement algorithms can also consider affinity

Page 36: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Multi-Processor Scheduling (3)

• Processor affinity – process has affinity for processor on which it is currently running– soft affinity– hard affinity– Variations including processor sets

Page 37: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Real Time Systems

• An absolute deadline for process execution must be met.

• these take the additional parameter of deadline into account in scheduling

ie: P1 P2 P3 P4 P5deadline 10:00 10:05 9:00 11:00 11:10

• Assign CPU to the process that is in the greatest danger of missing it’s deadline.

Page 38: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

• Hard real-time systems– guaranteed amount of time. Special purpose

hardware/software machines

• Soft real-time systems– Based on priority based scheduling. – Even kernel must be pre-emptive so a critical process can

run. – Resource allocation is “pre-emptive”. If a critical process

(A) needs a resourced owned by another process (B), that process B gets process A priority, so it can release the resource. Once processes B released the resource it priority is returned to “normal”.

Page 39: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Windows Scheduling

• Windows uses priority-based preemptive scheduling• Highest-priority thread runs next• Thread runs until (1) blocks, (2) uses time slice, (3)

preempted by higher-priority thread• Real-time threads can preempt non-real-time• 32-level priority scheme• Variable class is 1-15, real-time class is 16-31• Priority 0 is memory-management thread• Queue for each priority• If no run-able thread, runs idle thread

Page 40: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Windows Priority Classes

• Win32 API identifies several priority classes to which a process can belong– REALTIME_PRIORITY_CLASS, HIGH_PRIORITY_CLASS,

ABOVE_NORMAL_PRIORITY_CLASS,NORMAL_PRIORITY_CLASS, BELOW_NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS

– All are variable except REALTIME• A thread within a given priority class has a relative priority

– TIME_CRITICAL, HIGHEST, ABOVE_NORMAL, NORMAL, BELOW_NORMAL, LOWEST, IDLE

• Priority class and relative priority combine to give numeric priority• Base priority is NORMAL within the class• If quantum expires, priority lowered, but never below base• If wait occurs, priority boosted depending on what was waited for• Foreground window given 3x priority boost

Page 41: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Windows XP Priorities

Page 42: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Linux Scheduling

• Constant order O(1) scheduling time• Preemptive, priority based• Two priority ranges: time-sharing and real-time• Real-time range from 0 to 99 and nice value from 100 to 140• Map into global priority with numerically lower values indicating

higher priority• Higher priority gets larger q• Task run-able as long as time left in time slice (active)• If no time left (expired), not run-able until all other tasks use their

slices• All run-able tasks tracked in per-CPU runqueue data structure

– Two priority arrays (active, expired)– Tasks indexed by priority– When no more active, arrays are exchanged

Page 43: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Linux Scheduling (Cont.)

• Real-time scheduling according to POSIX.1b– Real-time tasks have static priorities

• All other tasks dynamic based on nice value plus or minus 5– Interactivity of task determines plus or minus

• More interactive -> more minus

– Priority recalculated when task expired– This exchanging arrays implements adjusted

priorities

Page 44: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Priorities and Time-slice length

Page 45: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

List of Tasks Indexed According to Priorities

Page 46: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Choosing an Algorithm

• There is no “best” algorithm, just better suited.

• Need to choose the goals that are more important to the environment.

Page 47: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

Memory and Processes

• When main memory is insufficient, some processes will be “swapped out” and ready to run.

• Short-term schedulers works with medium-term scheduler to bring these process back into memory.– Why not just have the Short-term Scheduler do

the work?

Page 48: Cosc 4740 Chapter 5 Process Scheduling. CPU Scheduling Short-term Scheduler –Selects a process from the ready queue when current process releases the

QA&