14b Scheduling

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

Scheduling Policies

Copyright : Nahrstedt, Angrave, Abdelzaher

Copyright : Nahrstedt, Angrave, Abdelzaher

Announcements

Midterm vote:

Keep current date (10/17)? Advance prior to 10/12 (drop off deadline)?

Quiz 2 graded. Check out your grades on compass periodically

Request re-grades promptly (by e-mailing cs241help or asking a grader, TA, or instructor during office hours) Check that you dont have missing (not recorded) grades
2

Copyright : Nahrstedt, Angrave, Abdelzaher

Solutions to Quiz 2
Q1) Which pthreads function is the closest analogy to fork(), applied to threads? pthread_create() (1 point)
Q2) Which pthreads function is the closest analogy to wait(), applied to threads? pthread_join() (1 point) Q3) What does the Test-and-Set instruction do? It tests a Boolean (returning its current value), and sets it to one atomically (1 point) Q4) Answer true or false. Correct if wrong. (2 points) A) All UNIX processes running on the same computer are descendants of the same process. TRUE B) A child process becomes a zombie if it executes exit() but the parent never issues a wait(). TRUE C) If a parent process sets some global variable x equal to 1 then creates a child process who increments x, the parent will see the incremented value of x only after the child returns. FALSE. It never sees the update. D) fork() always returns 0 if the operation is successful. FALSE: It can 3 also return child PID.

Copyright : Nahrstedt, Angrave, Abdelzaher

Content of This Lecture

Basic scheduling algorithms


Problems with FIFO (FCFS) Shortest job first Round Robin Priority Scheduling Understand how your program is executed on the machine together with other programs
4

Goals:

Copyright : Nahrstedt, Angrave, Abdelzaher

First Come First Serve (FCFS)


Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Is it preemptive or non-preemptive? Used in batch systems Implementation

FIFO queues A new process enters at the tail of the queue An unblocked process re-enters at the tail of the queue The schedule selects from the head of the queue.

Typical performance metric: Average Waiting Time.


5

Copyright : Nahrstedt, Angrave, Abdelzaher

Problems with FCFS


Non-preemptive Not optimal AWT Cannot utilize resources in parallel:

Assume 1 process CPU bounded and many I/O bounded processes Result: Convoy effect, low CPU and I/O Device utilization Why?
6

Copyright : Nahrstedt, Angrave, Abdelzaher

Why Convoy Effects?

Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound. I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O. CPU bound job arrives at head of queue and executes until complete. I/O bound jobs re-join ready queue and wait for CPU bound job to complete. I/O devices idle until CPU bound job completes. When CPU bound job completes, other processes rush to wait on I/O again. CPU becomes idle. 7

Copyright : Nahrstedt, Angrave, Abdelzaher

Interactive Scheduling Algorithms

Usually preemptive

Performance Criteria

Time is sliced into quantum (time intervals) Scheduling decision is also made at the beginning of each quantum Average response time Fairness (or proportional resource allocation) Priority-based Round-robin

Representative algorithms:

Copyright : Nahrstedt, Angrave, Abdelzaher

Round-robin

One of the oldest, simplest, most commonly used scheduling algorithm Select process/thread from ready queue in a round-robin fashion (take turns)

Problems: Does not consider priority Context switch overhead

Copyright : Nahrstedt, Angrave, Abdelzaher

Round-robin: Example
Process Duration Order Arrival Time

P1 3 1 P2 4 2 3 SupposeP3 quantum is: 1 3 time unit, P1, P2 & P3 never block


P1 P2 P3 P1 P2 P3 P1 P2 P3 P2

0 0 0

10

P1 waiting time: 4 P2 waiting time: 6 P3 waiting time: 6

The average waiting time (AWT): (4+6+6)/3 = 5.33


10

Copyright : Nahrstedt, Angrave, Abdelzaher

Choosing the Time Quantum

Time slice too large


FIFO behavior Poor response time

Time slice too small

Too many context switches (overheads) Inefficient CPU utilization 70-80% of jobs block within time-slice
10-100 ms (depends on job priority)
11

Heuristic:

Typical time-slice

Copyright : Nahrstedt, Angrave, Abdelzaher

Shortest Job First (SJF)

Schedule the job with the shortest computation time first Scheduling in Batch Systems Two types:

Non-preemptive Preemptive

Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time)

12

Copyright : Nahrstedt, Angrave, Abdelzaher

Non-preemptive SJF: Example


Process P1 P2 P3
P4 (3) 0 3

Duration 6 8 7
P1 (6) 9

Order 1 2 3
P3 (7)4 16

Arrival Time 0 0 0 0 P2 (8)


24

P4

P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16

The total time is: 24 The average waiting time (AWT): (0+3+9+16)/4 = 7
13

Copyright : Nahrstedt, Angrave, Abdelzaher

Comparing to FCFS
Process P1 P2 P3 Duration 6 8 7
P2 (8)3 6 14

Order 1 2 3 4

Arrival Time 0 0 0
P3 (7)

Do it yourself P4 P1 (6) 0

0
21

P4 (3) 24

P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21

The total time is the same. The average waiting time (AWT): (0+6+14+21)/4 = 10.25 (comparing to 7)
14

Copyright : Nahrstedt, Angrave, Abdelzaher

Preemptive SJF

Shortest job runs first. A job that arrives and is shorter than the running job will preempt it

15

Copyright : Nahrstedt, Angrave, Abdelzaher

A Problem with Preemptive SJF

Starvation

A job may keep getting preempted by shorter ones Example


Process A with elapse time of 1 hour arrives at time 0 But every 1 minute, a short process with elapse time of 2 minutes arrives Result of SJF: A never gets to run

Whats the difference between starvation and 16 a deadlock?

Copyright : Nahrstedt, Angrave, Abdelzaher

Priority Scheduling

Each job is assigned a priority. FCFS within each priority level. Select highest priority job over lower ones. Rational: higher priority jobs are more mission-critical

Example: DVD movie player vs. send email May not give the best AWT Starvation of lower priority processes

Problems:

17

Copyright : Nahrstedt, Angrave, Abdelzaher

Priority Scheduling: Example


Process P1 P2 P3 Duration 6 8 7
P4 (3) 3 8 11

Priority 4 1 3
P3 (7)2 18

Arrival Time 0 0 0 0 P1 (6)


24

Do it yourself P2 (8) P4 0

P2 waiting time: 0 P4 waiting time: 8 P3 waiting time: 11 P1 waiting time: 18

The average waiting time (AWT): (0+8+11+18)/4 = 9.25 (worse than SJF)
18

Copyright : Nahrstedt, Angrave, Abdelzaher

Set Priority

Every process has a default priority User can also change a process priority

The nice command

19

Copyright : Nahrstedt, Angrave, Abdelzaher

Set/Get Process Priority


int getpriority(int which, id_t who); int setpriority(int which, id_t who, int value);

20

Copyright : Nahrstedt, Angrave, Abdelzaher

Introduction to Signals

What is Signal? A signal is a software

notification to a process of an event. Why do we need Signals? In systems we need to enable asynchronous events
Examples of asynchronous events:

Email message arrives on my machine mailing agent (user) process should retrieve Invalid memory access happens OS should inform scheduler to remove process from the processor Alarm clock goes on process which sets the alarm should catch it
21

Copyright : Nahrstedt, Angrave, Abdelzaher

Basic Signal Concepts

Signal is generated when the event that causes it occurs. Signal is delivered when a process receives it. The lifetime of a signal is the interval between its generation and delivery. Signal that is generated but not delivered is pending. Process catches signal if it executes a signal handler when the signal is delivered. Alternatively, a process can ignore a signal when it is delivered, that is to take no action. Process can temporarily prevent signal from being delivered by blocking it. Signal Mask contains the set of signals currently blocked. 22

Copyright : Nahrstedt, Angrave, Abdelzaher

How Signals Work


Process Signal Mask Signal Generated

Signal delivered Signal not blocked

Signal Caught by handler

Signal Handler

Signal Mask

Signal Mask Process Resumed

Return from Signal Handler


23

Copyright : Nahrstedt, Angrave, Abdelzaher

Examples of POSIX Required Signals


Signal SIGABRT Description process abort default action implementation dependent

SIGALRM
SIGBUS SIGCHLD SIGILL SIGINT SIGKILL

alarm clock
access undefined part of memory object child terminated, stopped or continued invalid hardware instruction interactive attention signal (usually ctrl-C) terminated (cannot be caught or ignored)

abnormal termination
implementation dependent ignore implementation dependent abnormal termination abnormal termination
24

Copyright : Nahrstedt, Angrave, Abdelzaher

Examples of POSIX Required Signals


Signal
SIGSEGV SIGSTOP SIGTERM SIGTSTP SIGTTIN SIGTTOU SIGURG

Description
Invalid memory reference Execution stopped termination Terminal stop Background process attempting read Background process attempting write High bandwidth data available on socket

default action
implementation dependent stop Abnormal termination stop stop stop ignore

SIGUSR1

User-defined signal 1

abnormal termination

25

Copyright : Nahrstedt, Angrave, Abdelzaher

Generating Signals

Signal has a symbolic name starting with SIG Signal names are defined in signal.h Users can generate signals (e.g., SIGUSR1) OS generates signals when certain errors occur (e.g., SIGSEGV invalid memory reference) Specific calls generate signals such as alarm (e.g., SIGALARM)
26

Copyright : Nahrstedt, Angrave, Abdelzaher

Command Line Generates Signals

You can send a signal to a process from the command line using kill kill -l will list the signals the system understands kill [-signal] pid will send a signal to a process.

The optional argument may be a name or a number (default is SIGTERM).

To unconditionally kill a process, use:

kill -9 pid which is kill -SIGKILL pid.


27

Copyright : Nahrstedt, Angrave, Abdelzaher

Command Line Generates Signals


CTRL-C is SIGINT (interactive attention signal CTRL-Z is SIGSTOP (execution stopped cannot be ignored) CTRL-Y is SIGCONT (execution continued if stopped) CTRL-D is SIGQUIT (interactive termination: core dump)

28

Copyright : Nahrstedt, Angrave, Abdelzaher

Timers Generate SIGALRM Signals


#include <unistd.h> unsigned alarm (unsigned seconds);

alarm(20) creates SIGALRM to calling process after 20 real time seconds. Calls are not stacked alarm(0) cancels alarm
29

Copyright : Nahrstedt, Angrave, Abdelzaher

Summary
Important Issues to remember: How to compare different policies? What are the pros and cons of each scheduling policy? What are signals and why are they important? What does it mean to catch a signal? What are the different ways to generate signals?

30

You might also like