Operating Systems Slides 3 - CPU Scheduling

24
CPU Scheduling Wang Xiaolin September 28, 2013 [email protected] 1 / 24

description

Handouts version: http://cs2.swfu.edu.cn/~wx672/lecture_notes/os/slides/cpu-sched-a.pdf

Transcript of Operating Systems Slides 3 - CPU Scheduling

Page 1: Operating Systems Slides 3 - CPU Scheduling

CPU Scheduling

Wang Xiaolin

September 28, 2013

) [email protected]

1 / 24

Page 2: Operating Systems Slides 3 - CPU Scheduling

Scheduling Queues

Job queue consists all the processes in the systemReady queue A linked list consists processes in the main

memory ready for executeDevice queue Each device has its own device queue

2 / 24

Page 3: Operating Systems Slides 3 - CPU Scheduling

3.2 Process Scheduling 105

queue header PCB7

PCB3

PCB5

PCB14 PCB6

PCB2

head

head

head

head

head

readyqueue

disk unit 0

terminal unit 0

magtape

unit 0

magtape

unit 1

tail registers registers

tail

tail

tail

tail

•••

•••

•••

Figure 3.6 The ready queue and various I/O device queues.

The system also includes other queues. When a process is allocated theCPU, it executes for a while and eventually quits, is interrupted, or waits forthe occurrence of a particular event, such as the completion of an I/O request.Suppose the process makes an I/O request to a shared device, such as a disk.Since there are many processes in the system, the disk may be busy with theI/O request of some other process. The process therefore may have to wait forthe disk. The list of processes waiting for a particular I/O device is called adevice queue. Each device has its own device queue (Figure 3.6).

A common representation of process scheduling is a queueing diagram,such as that in Figure 3.7. Each rectangular box represents a queue. Two typesof queues are present: the ready queue and a set of device queues. The circlesrepresent the resources that serve the queues, and the arrows indicate the flowof processes in the system.

A new process is initially put in the ready queue. It waits there until it isselected for execution, or is dispatched. Once the process is allocated the CPUand is executing, one of several events could occur:

• The process could issue an I/O request and then be placed in an I/O queue.

• The process could create a new subprocess and wait for the subprocess’stermination.

• The process could be removed forcibly from the CPU as a result of aninterrupt, and be put back in the ready queue.

3 / 24

Page 4: Operating Systems Slides 3 - CPU Scheduling

Queueing Diagram106 Chapter 3 Processes

ready queue CPU

I/O I/O queue I/O request

time sliceexpired

fork achild

wait for aninterrupt

interruptoccurs

childexecutes

Figure 3.7 Queueing-diagram representation of process scheduling.

In the first two cases, the process eventually switches from the waiting stateto the ready state and is then put back in the ready queue. A process continuesthis cycle until it terminates, at which time it is removed from all queues andhas its PCB and resources deallocated.

3.2.2 Schedulers

A process migrates among the various scheduling queues throughout itslifetime. The operating system must select, for scheduling purposes, processesfrom these queues in some fashion. The selection process is carried out by theappropriate scheduler.

Often, in a batch system, more processes are submitted than can be executedimmediately. These processes are spooled to a mass-storage device (typically adisk), where they are kept for later execution. The long-term scheduler, or jobscheduler, selects processes from this pool and loads them into memory forexecution. The short-term scheduler, or CPU scheduler, selects from amongthe processes that are ready to execute and allocates the CPU to one of them.

The primary distinction between these two schedulers lies in frequencyof execution. The short-term scheduler must select a new process for the CPUfrequently. A process may execute for only a few milliseconds before waitingfor an I/O request. Often, the short-term scheduler executes at least once every100 milliseconds. Because of the short time between executions, the short-termscheduler must be fast. If it takes 10 milliseconds to decide to execute a processfor 100 milliseconds, then 10/(100 + 10) = 9 percent of the CPU is being used(wasted) simply for scheduling the work.

The long-term scheduler executes much less frequently; minutes may sep-arate the creation of one new process and the next. The long-term schedulercontrols the degree of multiprogramming (the number of processes in mem-ory). If the degree of multiprogramming is stable, then the average rate ofprocess creation must be equal to the average departure rate of processes

4 / 24

Page 5: Operating Systems Slides 3 - CPU Scheduling

Scheduling

▶ Scheduler uses scheduling algorithm to choose aprocess from the ready queue

▶ Scheduling doesn’t matter much on simple PCs, because

1. Most of the time there is only one active process2. The CPU is too fast to be a scarce resource any more

▶ Scheduler has to make efficient use of the CPU becauseprocess switching is expensive

1. User mode → kernel mode2. Save process state, registers, memory map...3. Selecting a new process to run by running the scheduling

algorithm4. Load the memory map of the new process5. The process switch usually invalidates the entire memory

cache

5 / 24

Page 6: Operating Systems Slides 3 - CPU Scheduling

Scheduling Algorithm Goals

.All systems..

.

Fairness giving each process a fair share of the CPUPolicy enforcement seeing that stated policy is carried out

Balance keeping all parts of the system busy

6 / 24

Page 7: Operating Systems Slides 3 - CPU Scheduling

.Batch systems..

.

Throughput maximize jobs per hourTurnaround time minimize time between submission and

terminationCPU utilization keep the CPU busy all the time

.Interactive systems..

.Response time respond to requests quicklyProportionality meet users’ expectations

.Real-time systems..

.

Meeting deadlines avoid losing dataPredictability avoid quality degradation in multimedia

systems

7 / 24

Page 8: Operating Systems Slides 3 - CPU Scheduling

Process Behavior— CPU-bound vs. I/O-bound

Types of CPU bursts:▶ long bursts – CPU bound (i.e. batch work)▶ short bursts – I/O bound (i.e. emacs)

Long CPU burst

Short CPU burst

Waiting for I/O

(a)

(b)

Time

Fig. 2-37. Bursts of CPU usage alternate with periods of waitingfor I/O. (a) A CPU-bound process. (b) An I/O-bound process.

As CPUs get faster, processes tend to get more I/O-bound.

8 / 24

Page 9: Operating Systems Slides 3 - CPU Scheduling

Process Classification.Traditionally,...

CPU-bound processes vs. I/O-bound processes

.Alternatively,..

.

Interactive processes: responsiveness▶ command shells, editors, graphical apps

Batch processes: no user interaction, run inbackground, often penalized by the scheduler

▶ programming language compilers, database searchengines, scientific computations

Real-time processes:▶ should never be blocked by lower-priority processes▶ should have a short guaranteed response time with a

minimum variance▶ video and sound apps, robot controllers, programs that

collect data from physical sensors

9 / 24

Page 10: Operating Systems Slides 3 - CPU Scheduling

Schedulers

Long-term scheduler (or job scheduler) selects whichprocesses should be brought into the readyqueue.

Short-term scheduler (or CPU scheduler) selects whichprocess should be executed next and allocatesCPU.

Midium-term scheduler swapping.

▶ LTS is responsible for a good process mix of I/O-boundand CPU-bound process leading to best performance.

▶ Time-sharing systems, e.g. UNIX, often have nolong-term scheduler.

10 / 24

Page 11: Operating Systems Slides 3 - CPU Scheduling

Nonpreemptive vs. preemptive

A nonpreemptive scheduling algorithm lets a process run aslong as it wants until it blocks (I/O or waitingfor another process) or until it voluntarilyreleases the CPU.

A preemptive scheduling algorithm will forcibly suspend aprocess after it runs for sometime. — clockinterruptable

11 / 24

Page 12: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Batch Systems— First-Come First-Served

▶ nonpreemptive▶ simple▶ also has a disadvantage

What if a CPU-bound process (e.g. runs 1s at a time)followed by many I/O-bound processes (e.g. 1000 diskreads to complete)?

▶ In this case, a preemptive scheduling is preferred.

12 / 24

Page 13: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Batch Systems— Shortest Job First

(a)

8

A

4

B

4

C

4

D

(b)

8

A

4

B

4

C

4

D

Fig. 2-39. An example of shortest job first scheduling. (a) Run-ning four jobs in the original order. (b) Running them in shortestjob first order.

.Average turnaround time..

.

(a) (8 + 12 + 16 + 20)÷ 4 = 14

(b) (4 + 8 + 12 + 20)÷ 4 = 11

How to know the length of the next CPU burst?▶ For long-term (job) scheduling, user provides▶ For short-term scheduling, no way

13 / 24

Page 14: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Interactive Systems— Round-Robin Scheduling

(a)

Currentprocess

Nextprocess

B F D G A

(b)

Currentprocess

F D G A B

Fig. 2-41. Round-robin scheduling. (a) The list of runnableprocesses. (b) The list of runnable processes after B uses up itsquantum.

▶ Simple, and most widely used;▶ Each process is assigned a time interval, called itsquantum;

▶ How long shoud the quantum be?▶ too short — too many process switches, lower CPU

efficiency;▶ too long — poor response to short interactive requests;▶ usually around 20-50ms.

14 / 24

Page 15: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Interactive Systems— Priority Scheduling

Priority 4

Priority 3

Priority 2

Priority 1

Queueheaders

Runable processes

(Highest priority)

(Lowest priority)

Fig. 2-42. A scheduling algorithm with four priority classes.▶ SJF is a priority scheduling;▶ Starvation — low priority processes may never execute;

▶ Aging — as time progresses increase the priority of theprocess;

▶ ∼$ man nice15 / 24

Page 16: Operating Systems Slides 3 - CPU Scheduling

Thread SchedulingProcess A Process B Process BProcess A

1. Kernel picks a process 1. Kernel picks a thread

Possible: A1, A2, A3, A1, A2, A3Also possible: A1, B1, A2, B2, A3, B3

Possible: A1, A2, A3, A1, A2, A3Not possible: A1, B1, A2, B2, A3, B3

(a) (b)

Order in whichthreads run

2. Runtime system picks a thread

1 2 3 1 3 2

Fig. 2-43. (a) Possible scheduling of user-level threads with a 50-msec process quantum and threads that run 5 msec per CPU burst.(b) Possible scheduling of kernel-level threads with the samecharacteristics as (a).

▶ With kernel-level threads, sometimes a full contextswitch is required

▶ Each process can have its own application-specificthread scheduler, which usually works better thankernel can

16 / 24

Page 17: Operating Systems Slides 3 - CPU Scheduling

Process Scheduling In LinuxA preemptive, priority-based algorithm with two separatepriority ranges:1. real-time range from 0 to 99, for tasks where absolute

priorities are more important than fairness2. nice value range from 100 to 139, for fair preemptive

scheduling among multiple processes

17 / 24

Page 18: Operating Systems Slides 3 - CPU Scheduling

In Linux, Process Priority is Dynamic.The scheduler keeps track of what processes aredoing and adjusts their priorities periodically..

.

▶ Processes that have been denied the use of a CPU for along time interval are boosted by dynamically increasingtheir priority (usually I/O-bound)

▶ Processes running for a long time are penalized bydecreasing their priority (usually CPU-bound)

▶ Priority adjustments are performed only on user tasks,not on real-time tasks

.Tasks are determined to be I/O-bound orCPU-bound based on an interactivity heuristic..

.

A task’s interactiveness metric is calculated based onhow much time the task executes compared to howmuch time it sleeps

18 / 24

Page 19: Operating Systems Slides 3 - CPU Scheduling

Problems With The Pre-2.6 Scheduler

▶ an algorithm with O(n) complexity▶ a single runqueue for all processors

▶ good for load balancing▶ bad for memory caches, when a task is rescheduled from

one CPU to another▶ a single runqueue lock — only one CPU working at a

time

19 / 24

Page 20: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Linux 2.6 Kernel

▶ O(1) — Time for finding a task to execute depends noton the number of active tasks but instead on the numberof priorities

▶ Each CPU has its own runqueue, and schedules itselfindependently; better cache efficiency

▶ The job of the scheduler is simple — Choose the task onthe highest priority list to execute

.How to know there are processes waiting in apriority list?..

.

A priority bitmap (5 32-bit words for 140 priorities) is usedto define when tasks are on a given priority list.

▶ find-first-bit-set instruction is used to find the highestpriority bit.

20 / 24

Page 21: Operating Systems Slides 3 - CPU Scheduling

Scheduling In Linux 2.6 Kernel

.Each runqueue has two priority arrays..

.

21 / 24

Page 22: Operating Systems Slides 3 - CPU Scheduling

Completely Fair Scheduling (CFS)

.Linux’s Process Scheduler..

.

up to 2.4: simple, scaled poorly

▶ O(n)▶ non-preemptive

▶ single run queue(cache? SMP?)

from 2.5 on: O(1) scheduler© 140 priority lists — scaled well© one run queue per CPU — true SMP support© preemptive© ideal for large server workloads§ showed latency on desktop systems

from 2.6.23 on: Completely Fair Scheduler (CFS)© improved interactive performance

22 / 24

Page 23: Operating Systems Slides 3 - CPU Scheduling

Completely Fair Scheduler (CFS)

For a perfect (unreal) multitasking CPU▶ n runnable processes can run at the same time▶ each process should receive 1

n of CPU powerFor a real world CPU

▶ can run only a single task at once — unfair© while one task is running

§§ the others have to wait▶ p->wait_runtime is the amount of time the task should

now run on the CPU for it becomes completely fair andbalanced.© on ideal CPU, the p->wait_runtime value would always be

zero▶ CFS always tries to run the task with the largestp->wait_runtime value

23 / 24

Page 24: Operating Systems Slides 3 - CPU Scheduling

CFS

In practice it works like this:▶ While a task is using the CPU, its wait_runtime decreases

wait_runtime = wait_runtime - time_runningif: its wait_runtime ̸= MAXwait_runtime (among all processes)

then: it gets preempted▶ Newly woken tasks (wait_runtime = 0) are put into the

tree more and more to the right▶ slowly but surely giving a chance for every task to

become the “leftmost task” and thus get on the CPUwithin a deterministic amount of time

24 / 24