operating systems cs3013 / cs502

68
WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS Operating Systems CS3013 / CS502

Upload: calida

Post on 12-Jan-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Operating Systems CS3013 / CS502. WEEK 3 SCHEDULING POLICIES SYNCHRONIZATION DEADLOCKS. Agenda. Scheduling Policies Synchronization Deadlock. Objectives. Identify Scheduling Criteria Explain Different Scheduling Policies Describe Measurements for Evaluation - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Operating Systems CS3013 / CS502

WEEK 3

SCHEDULING POLICIESSYNCHRONIZATION

DEADLOCKS

Operating SystemsCS3013 / CS502

Page 2: Operating Systems CS3013 / CS502

Agenda

Scheduling Policies

Synchronization

Deadlock

2

Page 3: Operating Systems CS3013 / CS502

Objectives3

Identify Scheduling Criteria

Explain Different Scheduling Policies

Describe Measurements for Evaluation

Give Examples of Evaluation Methods

Page 4: Operating Systems CS3013 / CS502

CPU Burst Duration4

Page 5: Operating Systems CS3013 / CS502

Scheduler5

Invoked whenever the operating system must select a user-level process to execute After process creation/termination A process blocks on I/O I/O interrupt occurs Clock interrupt occurs

Type of processes Interactive jobs CPU bound jobs Somewhere inbetween

Page 6: Operating Systems CS3013 / CS502

Scheduling Criteria6

What should we consider in developing a scheduling policy?

Page 7: Operating Systems CS3013 / CS502

Scheduling Criteria7

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 process has been waiting in the ready queue

Response time – amount of time from request submission until first response is produced

Page 8: Operating Systems CS3013 / CS502

Scheduling Issues8

Fairness – don’t starve process

Priorities – most important first

Deadlines – task X must be done by time t

Optimization – throughput, response time, etc.

Page 9: Operating Systems CS3013 / CS502

Scheduling Evaluation9

Simplicity – easy to implementJob latency – time from start to completionInteractive latency – time from action start to

expected system responseThroughput – number of jobs completedUtilization – keep processor and/or subset of

I/O devices busyDeterminism – insure that jobs get done

before some time or eventFairness – every job makes progress

Page 10: Operating Systems CS3013 / CS502

Policies10

First-Come-First-Serve (FCFS)

Round Robin

Shorted Process/Job First (SPF/SJF)

Pre-emptive Shorted Process/Job First (PSPF/PSJF)

Multiple-level Feedback (FB)

Page 11: Operating Systems CS3013 / CS502

First-Come-First-Serve (FCFS)11

Process Burst Time

A 5

B 7

C 3

A CB

0 5 12 15

Page 12: Operating Systems CS3013 / CS502

Shortest Process/Job First12

Process Burst Time

A 5

B 7

C 3

AC B

0 3 8 15

Page 13: Operating Systems CS3013 / CS502

Exponential Average13

b(t+1) = a b(t) + (1-a) b(t)

a=0 Current value has no effect

a=1 Only current value matters

b(0) = system average, a=0.5

Page 14: Operating Systems CS3013 / CS502

Priority Scheduling14

Process Burst Time Priority

A 5 3

B 7 1

C 3 2

ACB

0 7 10 15

Page 15: Operating Systems CS3013 / CS502

Priority Scheduling Issues15

Starvation – low priority processes may never execute Use of aging

Priority Inversion – some processes may never run to release resources required by other processes Increase the priority to match level of resource of

level of waiting process

Page 16: Operating Systems CS3013 / CS502

Round Robin16

Process Burst Time

A 5

B 7

C 3

A A A A AC C CB B B B BB B

0 9 12 15

Page 17: Operating Systems CS3013 / CS502

Round Robin17

Play with Quantum q = 1 q = 2 q = 5

Process Burst Time

A 5

B 7

C 3

Page 18: Operating Systems CS3013 / CS502

Round Robin18

Avg. Turnaround Time Quantum from 1 to 5 How does it change?

Process Burst Time

A 5

B 3

C 1

D 7

Page 19: Operating Systems CS3013 / CS502

Exercise19

Process Burst Time Priority

A 10 2

B 1 1

C 2 3

Page 20: Operating Systems CS3013 / CS502

Exercise20

Turnaround time FCFS SJF (Preemptive vs. Non-preemptive) RR q= 1 RR q = 0.5

Process Arrival Time Burst Time

A 0.0 8

B 0.4 4

C 1.0 1

Page 21: Operating Systems CS3013 / CS502

Multilevel Queues21

Multilevel Ready Queue Foreground (interactive) Background (batch)

Each queue has its own scheduling algorithm Foreground – RR Background – FCFS

Scheduling between queues is required Fixed priority scheduling Time slice

Page 22: Operating Systems CS3013 / CS502

Multilevel Feedback Queue22

Process can move between queues

Parameters # of queues Scheduling algorithm for each queue Method to determine when to upgrade a process Method to determine when to demote a process Method to determine which queue a process will enter

when that process needs service

Page 23: Operating Systems CS3013 / CS502

Objectives23

Identify Scheduling Criteria

Explain Different Scheduling Policies

Describe Measurements for Evaluation

Give Examples of Evaluation Methods

Page 24: Operating Systems CS3013 / CS502

Agenda24

Page 25: Operating Systems CS3013 / CS502

Objectives25

Explain reasons for synchronization

Describe busy waiting solutions

Describe a semaphore and its use

Describe a sonitor and its use

Page 26: Operating Systems CS3013 / CS502

Why synchronization?26

Time John Jane

3:00 Look in fridge. Oh no, no pizza!

3:05 Leave for store. Look in fridge. Oh no, no pizza!

3:10 Arrive at store. Leave for store.

3:15 Buy pizza. Arrive at store.

3:20 Arrive home. Buy pizza.

3:25 Put away pizza. Arrive home.

3:30 Put away pizza.

Oh no!

Page 27: Operating Systems CS3013 / CS502

Cooperating Processes27

Consider a printer spooler Enter a file name into the spooler queue Printer daemon checks the queue and prints the file.

“Race Conditions”

Free 9

letter hw1 proj1 lecture1

5 76 8 9

A B

Page 28: Operating Systems CS3013 / CS502

Race Condition Demo28

Shared variableOne program increments the shared variable.The other decrements the shared variable.

// reads the value of shared memorysscanf(counter, "%d", &num);// incrememt/decrement the numbernum = num + incr;// writes the new value to the shared memorysprintf(counter, "%d\n", num);

Page 29: Operating Systems CS3013 / CS502

Producer / Consumer29

Model for cooperating processes

Producer “produces” an item that consumer “consumes”

Bounded buffer (shared memory)

item buffer[MAX]; /* queue */int counter; /* number of items */

Page 30: Operating Systems CS3013 / CS502

Producer30

item i; /* item produced */int in; /* put next item */while (1) {

produce an item;while (counter == MAX) {/* no-op */}buffer[in] = item;in = (in + 1) % MAX;counter = counter + 1;

}

Page 31: Operating Systems CS3013 / CS502

Consumer31

item i; /* item consumed */int out; /* take next item */while (1) {

while (counter == 0) {/* no-op */}item = buffer[out]out = (out + 1) % MAX;counter = counter - 1;

}

Page 32: Operating Systems CS3013 / CS502

Producer Consumer Trouble!32

P R1 = counter R1 = 5

P R1 = R1 + 1 R1 = 6

C R2 = counter R2 = 5

C R2 = R2 – 1 R2 = 4

C counter = R2 counter = 4

P counter = R1 counter = 6

Page 33: Operating Systems CS3013 / CS502

Critical Section33

Mutual Exclusion Only one process inside critical region

Progress No process outside critical region may block other

processes wanting in

Bounded Waiting No process should have to wait forever (starvation)

Page 34: Operating Systems CS3013 / CS502

First Try34

Shared variable turn is initialized to either A or B and both processes start execution concurrently.

…While (TRUE) {

…while (turn == B) ;// critical sectionturn = B;…

}

…While (TRUE) {

…while (turn == B) ;// critical sectionturn = B;…

}

…While (TRUE) {

…while (turn == A) ;// critical sectionturn = A;…

}

…While (TRUE) {

…while (turn == A) ;// critical sectionturn = A;…

}

Process A Process B

Page 35: Operating Systems CS3013 / CS502

Second Try35

Shared variables pAinside and pBinside are initialized to FALSE.

…While (TRUE) {

…while (pBinside) ;pAinside = TRUE;// critical sectionpAinside = FALSE;…

}

…While (TRUE) {

…while (pBinside) ;pAinside = TRUE;// critical sectionpAinside = FALSE;…

}

…While (TRUE) {

…while (pAinside) ;pBinside = TRUE;// critical sectionpBinside = FALSE;…

}

…While (TRUE) {

…while (pAinside) ;pBinside = TRUE;// critical sectionpBinside = FALSE;…

}

Process A Process B

Page 36: Operating Systems CS3013 / CS502

Third Try36

Shared variables pAtrying and pBtrying are initialized to FALSE.

…While (TRUE) {

…pAtrying = TRUE;while (pBtrying) ;// critical sectionpAtrying = FALSE;…

}

…While (TRUE) {

…pAtrying = TRUE;while (pBtrying) ;// critical sectionpAtrying = FALSE;…

}

…While (TRUE) {

…pBtrying = TRUE;while (pAtrying) ;// critical sectionpBtrying = FALSE;…

}

…While (TRUE) {

…pBtrying = TRUE;while (pAtrying) ;// critical sectionpBtrying = FALSE;…

}

Process A Process B

Page 37: Operating Systems CS3013 / CS502

Dekker’s Algorithm37

turn needs to be initialized to A or B.

…While (TRUE) { … pAtrying = TRUE; while (pBtrying)

if (turn == B) { pAtrying = FALSE; while (turn == B) ; pAtrying = TRUE;}

// critical section turn = B; pAtrying = FALSE; …}

…While (TRUE) { … pAtrying = TRUE; while (pBtrying)

if (turn == B) { pAtrying = FALSE; while (turn == B) ; pAtrying = TRUE;}

// critical section turn = B; pAtrying = FALSE; …}

Process A Process B…While (TRUE) { … pBtrying = TRUE; while (pAtrying)

if (turn == A) { pBtrying = FALSE; while (turn == A) ; pBtrying = TRUE;}

// critical section turn = A; pBtrying = FALSE; …}

…While (TRUE) { … pBtrying = TRUE; while (pAtrying)

if (turn == A) { pBtrying = FALSE; while (turn == A) ; pBtrying = TRUE;}

// critical section turn = A; pBtrying = FALSE; …}

Page 38: Operating Systems CS3013 / CS502

Peterson’s Algorithm38

turn does not need any initialization.

…While (TRUE) { … pAtrying = TRUE; turn = B; while (pBtrying && turn == B); // critical section pAtrying = FALSE; …}

…While (TRUE) { … pAtrying = TRUE; turn = B; while (pBtrying && turn == B); // critical section pAtrying = FALSE; …}

Process A Process B…While (TRUE) { … pBtrying = TRUE; turn = A; while (pAtrying && turn == A); // critical section pBtrying = FALSE; …}

…While (TRUE) { … pBtrying = TRUE; turn = A; while (pAtrying && turn == A); // critical section pBtrying = FALSE; …}

Page 39: Operating Systems CS3013 / CS502

Dekker’s and Peterson’s Algorithm39

Both of them are correct.

Limited to 2 processes

Can be generalized to N processes N must be known and fixed. Algorithm becomes too complicated and expensive.

Page 40: Operating Systems CS3013 / CS502

Synchronization Hardware40

Test-and-Set: returns and modifies atomically

int Test_and_Set(int &target) {int temp;temp = target;target = true;return temp;

}

Page 41: Operating Systems CS3013 / CS502

Using Test_and_Set41

All solutions so far requires “busy waiting”.

while (1) {while (Test_and_Set(lock)) ;// critical sectionlock = false;// remainder section

}

Page 42: Operating Systems CS3013 / CS502

Semaphore42

Page 43: Operating Systems CS3013 / CS502

Semaphore43

Do not require “busy waiting”Semaphore S (shared, often initially = 1)

Integer variable Accessed via two (indivisible) atomic operations

wait(S) : S = S – 1if (S < 0) then block(S)

signal(S) : S = S + 1;if (S <= 0) then wakeup(S)

Page 44: Operating Systems CS3013 / CS502

Critical Section with Semaphore44

semaphore mutex; /* shared */

while (1) {wait(mutex);// critical sectionsignal(mutex);// remainder section

}

Page 45: Operating Systems CS3013 / CS502

Classical Synchronization Problems45

Bounded Buffers

Readers/Writers

Dining Philosophers

Page 46: Operating Systems CS3013 / CS502

Readers/Writers46

Readers can only read the content

Writers read and write the object

Critical Region One or more readers (no writers) One writer (nothing else)

Solutions favor Reader or Writer

Page 47: Operating Systems CS3013 / CS502

Readers/Writers47

Shared

Writer

semaphore mutex, wrt;int readcount;

wait(wrt);// write stuffsignal(wrt)

Page 48: Operating Systems CS3013 / CS502

Reader

Readers-Writers48

wait(mutex);readcount = readcount + 1;if (readcount == 1) wait(wrt);signal(mutex);// read stuffwait(mutex);readcount = readcount – 1;if (readcount == 0) signal(wrt);signal(mutex);

Page 49: Operating Systems CS3013 / CS502

Dining Philosophers49

Philosophers Think Eat

Need 2 chopsticks to eat

Page 50: Operating Systems CS3013 / CS502

Dining Philosophers50

Philosopher i

while (1) {// thinkwait(chopstick[i]);wait(chopstick[i+1 % 5]);// eatsignal(chopstick[i]);signal(chopstick[i+1 % 5]);

}

Page 51: Operating Systems CS3013 / CS502

Dining Philosophers51

Other solutions

Allow at most N-1 to go hungry at a time

Allow to pick up chopsticks only if both are available

Asymmetric solution

Page 52: Operating Systems CS3013 / CS502

Monitors52

High-level constructCollection of

Variables Data structures Functions Like C++ class

One process active inside“Condition” variable

Not like semaphore

Page 53: Operating Systems CS3013 / CS502

Producer-Consumer Monitor53

monitor ProducerConsumer {condition full, empty;integer count;

// functionsvoid enter(item i);item remove();

}

void producer();void consumer();

Page 54: Operating Systems CS3013 / CS502

Producer-Consumer Monitor54

void producer() {item i;while (1) {

// produce item iProducerConsumer.enter(i);

}}void consumer() {

item i;while (1) {

i = ProducerConsumer.remove();// consume item i

}}

Page 55: Operating Systems CS3013 / CS502

Producer-Consumer Monitor55

void enter(item i) {if (count == N) sleep(full);// add item icount = count + 1;if (count == 1) then wakeup(empty);

}void remove() {

if (count == 0) then sleep(empty);// remove item into icount = count – 1;if (count == N-1) then wakeup(full);return i;

}

Page 56: Operating Systems CS3013 / CS502

Objectives56

Explain reasons for synchronization

Describe busy waiting solutions

Describe a semaphore and its use

Describe a sonitor and its use

Page 57: Operating Systems CS3013 / CS502

Agenda57

Page 58: Operating Systems CS3013 / CS502

Objectives58

Identify deadlocks

Describe ways to prevent deadlocks

Describe ways to detect deadlocks

Describe ways to recover from deadlocks

Page 59: Operating Systems CS3013 / CS502

Deadlock59

Examples “It takes money to make money” You can’t get a job without experience; you can’t get

experience without a job.

Background The cause of deadlocks: Each process needing what

another process has. This results from sharing resources such as memory, devices, links.

Under normal operation, a resource allocation proceeds like this: Request a resource (suspend until available if necessary) Use the resource Release the resource

Page 60: Operating Systems CS3013 / CS502

Conditions for Deadlock60

Mutual Exclusion Each resource is assigned to a process or is available

Hold and Wait Processes holding resources can request new

resources

No Preemption Resources cannot be taken from a process

Circular Wait A set of processes are in a circular wait

Page 61: Operating Systems CS3013 / CS502

Resource Allocation Graph61

G = (V, E)

V : Nodes consist of processes and resource types

E : Edges are pairs of processes and resources

PiRj

PiRj

Pi

Page 62: Operating Systems CS3013 / CS502

No cycle No deadlock Cycle

If resource types have multiple instances, then deadlock MAY exist

If each resource type has 1 instance, then deadlock has occurred

Resource Allocation Graph62

P2 Requests P3

R3 Assigned to P3

Page 63: Operating Systems CS3013 / CS502

Resource Allocation Graph63

Page 64: Operating Systems CS3013 / CS502

Handling Deadlocks64

Ignore Deadlocks

Ensure deadlock never occurs Prevention Avoidance

Allow deadlock to happen Detection Recovery

Page 65: Operating Systems CS3013 / CS502

Deadlock Prevention65

Mutual Exclusion Not really possible

Hold and Wait Collect all resources before execution Only request resource when no others are being held

No Preemption Release any resource being held if the process can’t

get additional resource Allow preemption

Circular Wait Number resources and only request in ascending

order

Page 66: Operating Systems CS3013 / CS502

Deadlock Avoidance66

Possible States Deadlock: No forward progress can be made Unsafe: A state that may cause deadlock Safe: A state is safe if a sequence of processes exist

such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish.

If a request allocation would cause an unsafe state, do not honor that request.

Page 67: Operating Systems CS3013 / CS502

Deadlock Detection67

Resource Allocation Graph Wait-for Graph

Page 68: Operating Systems CS3013 / CS502

Deadlock Recovery68

PROCESS TERMINATION: Could delete all the processes in the deadlock -- this is expensive. Delete one at a time until deadlock is broken ( time consuming ). Select who to terminate based on priority, time executed, time to

completion, needs for completion, or depth of rollback In general, it's easier to preempt the resource, than to terminate

the process.

RESOURCE PREEMPTION:  Select a victim - which process and which resource to preempt. Rollback to previously defined "safe" state. Prevent one process from always being the one preempted

( starvation ).