52
1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Embed Size (px)

Citation preview

Page 1: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

1

Chapter 5: Process Scheduling

Lecture 5 10.14.2008

Hao-Hua Chu

Page 2: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Announcement

• Nachos assignment #2 out on course webpage– Due in two weeks– TA will talk about this assignment today.

2

Page 3: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Persuasive computing

• Using computing to change human behaviors– Baby Think It Over– Textrix VR Bike– Slot Machine

Page 4: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

4

• How to encourage good eating habit in young children?

• Sense to recognize behavior– Weight sensor underneath the tray to

sense eating actions – Eating actions as game input

• Play to engage behavior change– Interactive games: coloring cartoon

character or penguin fishing

Example: Playful Tray (NTU)

Page 5: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Outline

• The scheduling problem• Defining evaluation criteria

– CPU utilization, throughput, turnaround time, waiting time, response time

• Scheduling algorithms– FCFS, SJF, RR, Multilevel queue, Multilevel feedback queue– Preemptive vs. Non-preemptive

• Evaluating scheduling algorithms– Deterministic model, queueing model, simulation, implementation

• Others– Multiple-Processor scheduling, process affinity, load balancing, thread

Scheduling5

Page 6: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

The Scheduling Problem for the CPU Scheduler

• How to maximize CPU utilization in multiprogramming?– Multiprogramming– CPU utilization – other performance parameters?

• Consider that a process follows the CPU–I/O Burst Cycle– Process execution consists of a cycle of CPU execution and I/O

wait– A typical CPU burst distribution (next slides): Web browser, web

server

6

Page 7: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Alternating sequence of CPU And I/O bursts

7

Page 8: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Histogram of CPU-burst Times

8

Page 9: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Problem solving skill

• Before designing your solutions ….

• Model the problem– Put the problem in a form that can be solved (using an algorithm)

• Define the evaluation metrics– “Measure” how successful is a solution

• Now design your solutions

9

Page 10: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Modeling the Scheduling Problem

• For the CPU scheduler– Selects from among the processes in memory (ready queue) that

are ready to execute, and allocates the CPU to one of them.• When does the CPU scheduler make a scheduling

decision?

10

Page 11: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

CPU Scheduling Decisions

• CPU scheduling decisions may take place when a process:1. Switches from running to waiting state: how does it happen?2. Switches from running to ready state3. Switches from waiting to ready4. Terminates

• Scheduling under 1 and 4 is nonpreemptive• All other scheduling is preemptive• Preemption: (chinese definition)

11

Page 12: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

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

12

Page 13: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Scheduling Criteria

• CPU utilization – keep the CPU as busy as possible• Throughput – # of processes that complete their execution

per time unit• Turnaround time – amount of time to execute a particular

process• Waiting time – amount of time a process has been waiting

in the ready queue• Response time – amount of time it takes from when a

request was submitted until the first response is produced, not output (for time-sharing environment)

13

Page 14: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

More Scheduling Criteria

• Max CPU utilization• Max throughput• Min turnaround time • Min waiting time • Min response time

Submitted time

P1 P2 P3

24 27 300

Waiting time

Turnaround time

Response time

For P2

14

Page 15: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

First-Come, First-Served (FCFS) Scheduling

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

15

Page 16: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

FCFS Scheduling (Cont.)

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• Why is this much better than previous case?

– Convoy effect short process behind long process

P1P3P2

63 300

16

Page 17: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Shortest-Job-First (SJR) Scheduling

• Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time

• Two schemes: – Nonpreemptive: once CPU given to the process it cannot be

preempted until completes its CPU burst– Preemptive: if a new process arrives with CPU burst length less

than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining-Time-First (SRTF)

• SJF is optimal: give minimum average waiting time for a given set of processes

17

Page 18: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Example of Non-Preemptive SJF

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

P1 P3 P2

73 160

P4

8 12

18

Page 19: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

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

520

0

0

0

19

Page 20: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Shortest-Job-First (SJR) is Optimal

• SJF is optimal– Give minimum average waiting time for a given set of processes

• What is the strong assumption in SJF?– Know CPU burst time a-priori

• How to estimate (guess) the CPU burst time?• Is SJR still optimal if the estimated CPU burst time is

incorrect?• What is the other assumption in SJF?

– All processes have equal importance to users.

20

Page 21: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

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 lenght actual 1.

1

n

th

n nt

.1 1 nnn t

21

Page 22: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Prediction of the Length of the Next CPU Burst

.1 1 nnn t

5.0

22

Page 23: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Examples of Exponential Averaging

=0 n+1 = n

– Recent history does not count =1

– n+1 = tn

– Only the actual last CPU burst counts• If we expand the formula, we get:

n+1 = tn+(1 - ) tn-1 + … +(1 - )j tn -j + … +(1 - )n +1 0

• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor

.1 1 nnn t

23

Page 24: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Priority Scheduling

• A priority number (integer) is associated with each process

• The CPU is allocated to the process with the highest priority (smallest integer = highest priority)– Non-preemptive– Preemptive -> when …

• SJF is actually a priority scheduling algorithm– What is the used priority? – The predicted next CPU burst time

24

Page 25: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Priority Scheduling

• What are the potential problems with priority scheduling?– Can a process starve (although it is ready but never had a chance

to be executed)?– Starvation – low priority processes may never execute

• What to solve starvation?– Aging – as time progresses increase the priority of the process

• Priority scheduling is unfair to all processes– How to create a (preemptive) scheduling algorithm that is fair to

all processes? – That is, share the processor fairly among all processes

25

Page 26: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Round Robin (RR)

• Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. – After this time has elapsed, the process is preempted and added

to the end of the ready queue.• If there are n processes in the ready queue and the time

quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. – What is the maximum waiting time for a process? – (n-1)q time units

26

Page 27: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Example of RR with Time Quantum = 20

Process Burst TimeP1 53 P2 17 P3 68 P4 24

• The Gantt chart is:

• How does preemptive RR compare with preemptive SJF?– Average turnaround time? Average waiting time?

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

33

484

13

280

0

8

27

Page 28: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Round Robin (RR)

• How does the size of time quanta (q) affect performance?– Effects on context switching time & turnaround time?– Large q?

• q large FIFO, smaller context switching time– Small q?

• q small larger context switching time, turnaround time may increase.

• For example: 3 processes of 10 time units each. q=1 (avg turnaround time = 29) vs. q=10 (avg turnaround time = 20)

28

Page 29: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Effect of Time Quanta Size on Context Switch Time

• Smaller time quantum size leads to more context switches

29

Page 30: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Effect of Time Quanta Size on Turnaround Time

• Smaller time quanta “may” increase turnaround time.

30

Page 31: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Round Robin & Shortest-Job-First

• Their strong assumption is that all processes have equal importance to users.– background (batch - virus checking or video compressing) vs.

foreground processes (interactive processes - word document). • Foreground processes: waiting/response time is important (excessive

context switching is okay).• Background processes: waiting time is not important (excessive

context switching decreases throughput)

• What is the solution?– Is it possible to have multiple scheduling algorithms (running at

the same time) for different types of processes?

31

Page 32: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multilevel Queue

• Ready queue is partitioned into separate queues:– foreground (interactive)– background (batch)

• Each queue has its own scheduling algorithm– foreground – RR, smaller time quantum – background – FCFS, larger time quantum

• Scheduling must be done between the queues– Fixed priority scheduling: serve all from foreground then from

background.– Time slice: each queue gets a certain amount of CPU time

• 80% to foreground in RR and 20% to background in FCFS

32

Page 33: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multilevel Queue Scheduling

33

Page 34: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multilevel Queue

• Multilevel queue assumes processes are permanently assigned to a queue at the start.

• Two problems: not flexible & starvation– It is difficult to predict the nature of a process at the start (the

level of interactivity, CPU-bound, I/O-bound, etc.) – In the fixed-priority scheduling, low priority process may never be

executed. • Good thing: low overhead• How to solve these two problems with a bit more

overhead?

34

Page 35: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multilevel Feedback Queue

• A process can move between the various queues; aging can be implemented this way

• Multilevel-feedback-queue scheduler defined by the following parameters:– number of queues– scheduling algorithms for each queue– method used to determine when to upgrade a process– method used to determine when to demote a process– method used to determine which queue a process will enter

when that process needs service

35

Page 36: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Example of Multilevel Feedback Queue

• Feedback: based on the observed process’s CPU burst, enables upgrade or downgrade between queues. – CPU-bound process downgrades to low priority queue.– I/O-bound process upgrades to high priority queue.

• Three queues: – Q0 – RR with time quantum 8 milliseconds

– Q1 – RR time quantum 16 milliseconds

– Q2 – FCFS

36

Page 37: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Example of Multilevel Feedback Queue

• Scheduling– A new job enters queue Q0 which is served FCFS. When it gains

CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1.

– At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

• Comparison to Multilevel Queue:– Benefits: flexibility & no starvation– Cost: scheduling overhead

37

Page 38: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multilevel Feedback Queues

38

Page 39: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Multiple-Processor Scheduling

• Scheduling more complex for multi-processor systems– Asymmetric multiprocessing vs. symmetric multiprocessing (SMP)

• Asymmetric: master vs. slave processor, only the master processor controls CPU scheduler

• Symmetric: each processor has its own CPU scheduler• Tradeoff: synchronization overhead (running queue) vs. concurrency

– Processor Affinity• A higher cost in process migration to another processor, why?

– A process’s cache memory on a specific processor

– Load Balancing• Keep load evenly distributed on processors• Counteracts with processor affinity

– Hyperthreading – logical CPUs, similar to physical CPUs

39

Page 40: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Thread Scheduling

• Two scopes of scheduling– Global Scheduling: the CPU scheduler decides which kernel thread

to run next– Local Scheduling: the user-level threads library decides which

thread to put onto an available LWP

40

Page 41: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

How to evaluate scheduling algorithms?

• Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload

• Queueing models – analytical model based on probability– Distributions on process arrival time, CPU burst, I/O time,

completion time, tec.• Simulation

– Randomly generated CPU load or collect real traces• Implementation

41

Page 42: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Pthread Scheduling API

#include <pthread.h>#include <stdio.h>#define NUM THREADS 5int main(int argc, char *argv[]){

int i;pthread t tid[NUM THREADS];pthread attr t attr;/* get the default attributes */pthread attr init(&attr);/* set the scheduling algorithm to PROCESS or SYSTEM */pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);/* set the scheduling policy - FIFO, RT, or OTHER */pthread attr setschedpolicy(&attr, SCHED OTHER);/* create the threads */for (i = 0; i < NUM THREADS; i++)

pthread create(&tid[i],&attr,runner,NULL);

42

Page 43: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Pthread Scheduling API/* now join on each thread */for (i = 0; i < NUM THREADS; i++)

pthread join(tid[i], NULL);} /* Each thread will begin control in this function */void *runner(void *param){

printf("I am a thread\n");pthread exit(0);

}

43

Page 44: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Operating System Examples

• Solaris scheduling• Windows XP scheduling• Linux scheduling

44

Page 45: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Solaris 2 Scheduling

45

Page 46: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Solaris Dispatch Table

• Multilevel feedback queue for the Interactive and time-sharing class

46

Page 47: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Windows XP Priorities

47

Page 48: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Linux Scheduling

• Two algorithms: time-sharing and real-time• Time-sharing

– Prioritized credit-based – process with most credits is scheduled next– Credit subtracted when timer interrupt occurs– When credit = 0, another process chosen– When all processes have credit = 0, recrediting occurs

• Based on factors including priority and history• Real-time

– Soft real-time– Posix.1b compliant – two classes

• FCFS and RR• Highest priority process always runs first

48

Page 49: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

The Relationship Between Priorities and Time-slice length

49

Page 50: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

List of Tasks Indexed According to Prorities

50

Page 51: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

Review

• The scheduling problem• Defining evaluation criteria

– CPU utilization, throughput, turnaround time, waiting time, response time

• Scheduling algorithms– FCFS, SJF, RR, Multilevel queue, Multilevel feedback queue– Preemptive vs. Non-preemptive

• Evaluating scheduling algorithms– Deterministic model, queueing model, simulation, implementation

• Others– Multiple-Processor scheduling, process affinity, load balancing, thread

Scheduling51

Page 52: 1 Chapter 5: Process Scheduling Lecture 5 10.14.2008 Hao-Hua Chu

End of Chapter 5

52