Operating System Examples - Scheduling. References r Silberschatz et al, Chapter 5.6, Chapter...

53
Operating System Examples - Scheduling

Transcript of Operating System Examples - Scheduling. References r Silberschatz et al, Chapter 5.6, Chapter...

Operating System Examples - Scheduling

Note: Process/Thread

A unit of execution is a process Processes can create a unit of execution

called a thread Threads share data with processes Operating systems schedule threads in the

same way processes are scheduled We will discuss threads in much greater

detail later in the course

Solaris

Solaris Solaris uses priority-

based scheduling Each process

belongs to one of six classes Time sharing Interactive Real time System File share Fixed priority

Default scheduling class time sharing

Within each class: Different priorities Different scheduling

algorithms Priority levels from 0

to 169

Priorities

Priorities

Interrupt Real-time class

Highest priority Guaranteed response time Kernel processes (very few) assigned this

class System class

Kernel processes are assigned to this class e.g., scheduler

Priorities are static

Priorities

Time-sharing Processes can have their priorities adjusted

Fixed-priority Introduced in Solaris 9 No dynamic adjustment of priorities

Fair-share Uses CPU shares instead of priorities A share is an indicator of entitlement

Real-Time Scheduling

Real-time priority queues can be configured to operate with a FIFO-strategy or a Round Robin strategy

Real-time processes can preempt running processes that have lower priority Tend to be few real-time processes

Time Sharing Scheduling Algorithm

Default priority for a user process is time sharing

Priorities are dynamic Use of a multilevel feedback queue

Each queue represents a different priority level

Time slices (quantum) are associated with a priority queue

Higher priorities smaller time slice

Time Sharing Scheduling Algorithm

Solaris uses a dispatch table This is used to assign a new priority to a

process. Two cases: Time quantum expired without blocking

Could be a CPU intensive process Process blocked before time quantum

expired This new priority will be used to

determine the process in a priority queue when the process returns to a READY state

Solaris Dispatch Table

For time-sharing &Interactiveprocesses

Solaris Dispatch Table Priority: A higher number indicates a

higher priority Time quantum: There is an inverse

relationship between priorities and time quanta

Time quantum expired: The new priority of a process that has used its entire time quantum without blocking

Return from sleep: The priority of a process that is returning from the blocked state (such as waiting for I/O)

Scheduling Algorithm

Time sharing scheduling algorithm If a priority has used its entire time

quantum without blocking its priority is changed (with one exception)

The priority of a priority that is returning from sleeping (e.g. waiting for I/O) has its priority increased• This allows for better support of interactive

processes

Interactive Processes

Interactive processes (e.g., window managers) typically have higher priority (good response time)

CPU-bound processes have lower priority (good throughput)

Windows 7

Priorities Similar to that used in Windows XP The scheduler is called a dispatcher 32 priorities Priorities are divided into two classes:

User class: priorities 1 to 15 Real-time class: priorities 16 to 31

Priority 0 is used for memory management processes

There is a queue for each priority

Selecting a Process The dispatcher traverses the set of

queues from highest to lowest until it finds a process that is ready to run

If there are no processes ready to run the dispatcher executes the idle process

Priority of a preempted process may be modified before being returned to a ready state

Round robin

Adjusting Priority If process was in user class

Time quantum expires: • If process is in the user class the priority is

lowered Process switches from blocked to running:

• Priority is increased• The amount depends on what the process was

doing• Keyboard I/O gets a large increase while disk I/O

gets a moderate increase

Some processes always have a low priority e.g., disk fragmenter

Adjusting Priority The priority of a process cannot be

lowered passed the base priority (lower threshold value) of the process

Windows 7 distinguishes between the foreground process that is currently selected on the screen and the background processes that are not currently selected Tends to give good response times to

interactive processes that are using the mouse and windows

Linux

Linux

Used for both servers and workstations Linux provides flexibility so that

performance is optimized

Linux Scheduling Goals

Implement scheduling algorithms in O(1) time.

Provide good interactive performance. Optimize for the common case of only one

or two runnable processes, yet scale well to multiple processors, each with many processes.

Linux uses the term task to refer to a process/thread

Linux priority levels are on the next slide

Priority Levels

Priorities and Time-slice length

25

Priorities

Priorities 0-99 for real-time processes

Priorities 100-139 for normal (user) processes

Linux Scheduling

A process is considered for execution on the CPU as long as it has time remaining in its time slice (quantum)

When a task has exhausted its time slice, it is considered expired and is not eligible for execution again until all other tasks have all exhausted their time slice

Linux Data Structures The kernel maintains a list of all

runnable tasks in a runqueue data structure

A runqueue consists of two priority arrays: Active: Contains all tasks with time

remaining in their time slices Expired: Contains all expired tasks

The active, expired queues are indexed according to priority

Linux Data Structures

List of Tasks Indexed According to Priorities

Linux Scheduling How is a process selected for execution?

The scheduler finds the highest-priority queue with a runnable process

Finds the first process on the queue Determine its quantum size (more later)

What happens to a running process that does not complete its time quantum? When that process returns to the ready

state it goes into the active array

Linux Scheduling When there are no more processes in

the active array, the expired array becomes the active array and the active array becomes the expired array.

A process’s priority (queue) is recalculated when the process has exhausted its time quantum and is to be moved to the expired array

To Discuss

Linux Efficiency Calculating time quantum using static

priorities Determining the queue that a process

should be placed using dynamic priority

Linux Efficiency

We will now discuss efficiency. Choice of next process to run:

Process with the highest priority A bitmap is created such that there is 1

bit for every priority level. 140 priority levels Bitmap size:

• 140 bits/32 bits per word = 4.375 words = 5 words

If a running process exists at a level with quantum left, the corresponding bit is true

Linux Efficiency

If a priority N has a process that can be run, bitmap[N] = 1

The first bit position in bitmap that is 1 represents the priority level from which a process is chosen

A priority level is a queue Pick the first process

Linux Efficiency

Find the first bit with a 1 Hardware instruction to find the first 1-bit

• bsfl on Intel Time depends on the number of priority

levels, not the number of processes

Linux Efficiency

There are pointers to the expired and active arrays

Making the expired array the active array requires update of the pointers Can be done quickly

No need for an explicit aging mechanism

Scheduling Components

Scheduler chooses a process based on dynamic priority.

Dynamic priority is a function of Static priority and bonus

Bonus is derived from sleep average Now to explain this

Static Priorities Each process has a static priority (SP)

Value is between 0 and 139

Real-time processes have a static priority between 0 and 99

All other processes (referred to as conventional) are assigned a static priority between 100-139

Static Priorities The static priority is set based on the

nice value Range of nice values: [-20,20] By default 0

Mapping of nice values to static priorities 0 maps to 120 -20 maps to 100 20 maps to 140

Static Priorities

Base time quantum: The time quantum assigned by the scheduler to a process if it has exhausted its previous time quantum If (SP < 120):Base time quantum = (140-

SP)* 20 If (SP ≥ 120):Base time quantum = (140-

SP)*5

Typical Quanta

Priority Static Priority

Quantum

Highest 100 800 ms

High 110 600 ms

Normal 120 100 ms

Low 130 50 ms

Lowest 139 5 ms

Dynamic Priority

Besides a static priority, a user process also has a dynamic priority Ranges from 100 (highest priority) to 139

(lowest priority) The dynamic priority is the number actually

used by the scheduler when selecting the new process to run. • This number is the queue

Adjusting Priorities

Dynamic priority (DP) is calculated from static priority (SP) and bonus value DP = max(100, min(SP-bonus+5,139))

The bonus is based on average sleep time Measure of interactiveness

Calculating Sleep Time The scheduler keeps track of the

average amount of time a task sleeps vs average amount of time task uses CPU Use the sleep_avg variable

When a process wakes the time spent sleeping is added to its sleep_avg

When a process is switched out, the run time is subtracted from sleep_avg

Calculating Sleep Time Sleep ratio

Mostly sleeping: I/O bound Mostly running: CPU bound

sleep_avg scaled to a bonus value

Sleep Time and Bonus Values

Sleep Time Bonus>=0 but < 100 ms 0

>= 100 ms but < 200 ms 1

>=200 ms but < 300 ms 2

>=300 ms but < 400 ms 3

>=400 ms but < 500 ms 4

>=500 ms but < 600 ms 5

>=600 ms but < 700 ms 6

>=700 ms but < 800 ms 7

>= 800 ms but < 900 ms 8

>=900 ms but < 1000 ms 9

1 second 10

Bonus and Dynamic Priority

Dynamic priority (DP) is calculated from static priority (SP) and bonus DP = max(100, min(SP-bonus+5,139))

Bonus: 0-10 <5: penalty

• Less sleeping >5: premium

• More sleeping (highly interactive)

The higher the bonus the task is getting and the higher its static priority, the more likely it is to be considered interactive

Using Quanta Assume a process has consumed its

quanta (time slice) Check interactive status:

If non-interactive, put it aside on the expired array

If interactive, put it at the end of the active array

Using Quanta

Exceptions to putting on higher-priority array: If higher-priority process is on expired list If expired task has been waiting more than

some specified amount of time If there’s nothing else at that priority, it

will run again immediately Of course, by running so much, its

bonus will go down, and so will its priority and its interactive status

Other

MAC OS X

Based on MACH and Unix BSD Priorities are categorized into priority

bands Normal: Applications System high priority: Processes with higher

priority then Normal Kernel mode: Reserved for kernel processes Real-time: For processes that must be

guaranteed a slice of CPU time by a particular deadline

MAC OS X

Priorities change dynamically Based on wait time and amount of time

that the process has had the processor Stay within the same priority band

Reschedules every tenth of a second and recomputes priorities once every second

Prrocess will relinquish CPU after time quantum or when it must wait for an I/O completion

Feedback prevents starvation

Android

For mobile devices Today it is the most commonly used

platform Uses Linux for device managers,

memory management, process management

Summary

We have examined scheduling in several contemporary operating systems