CS 241 Section Week #7 (10/22/09). Topics This Section Midterm Statistics MP5 Forward Classical...

33
CS 241 Section Week #7 (10/22/09)

description

Midterm Statistics Avg: 18.6 Median: 19 Max: 25 Min: 10 Standard Deviation: 3.3

Transcript of CS 241 Section Week #7 (10/22/09). Topics This Section Midterm Statistics MP5 Forward Classical...

Page 1: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

CS 241 Section Week #7(10/22/09)

Page 2: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Topics This Section

Midterm StatisticsMP5 ForwardClassical Synchronization ProblemsProblems

Page 3: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Midterm Statistics

Avg: 18.6Median: 19Max: 25Min: 10Standard Deviation: 3.3

Page 4: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

MP5 Overview

Page 5: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

MP5 Overview

This is your first long MP. You have two weeks to complete it.

You are to create a “deadlock resilient semaphore” library. You should implement six functions.

You need to implement:– Deadlock prevention

• enforce a global ordering on all locks• Locks should be acquired in descending order

– Deadlock avoidance• No cycle exist in a wait-for graph• You need to implement the cycle detection algorithm

Page 6: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

MP5 Overview

– Deadlock detection• Periodically incur the cycle detection algorithm• once a deadlock is detected, you need only send a

SIGINT signal. The library does NOT need to worry about how SIGINT is handled.

Since we only allow one instance of each resource, you do not need to implement the Banker’s algorithm for deadlock prevention. You may use a resource allocation graph instead.

The given test cases are far from complete. You should derive your own test cases.

Page 7: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Classical Synchronization Problems

Page 8: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Example 1:Producer-Consumer Problem

Producers insert items Consumers remove itemsShared bounded buffer

e.g. a circular buffer with an insert and a removal pointer.

Page 9: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 10: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 11: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 12: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 13: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 14: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 15: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 16: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

BUFFER FULL: Producer must be blocked!

Page 17: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtrremovePtr

Page 18: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 19: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 20: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 21: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 22: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 23: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtr

removePtr

Page 24: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer

insertPtrremovePtr

BUFFER EMPTY: Consumer must be blocked!

Page 25: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

ChallengeNeed to prevent:

Buffer OverflowProducer writing when there is no storage

Buffer UnderflowConsumer reading nonexistent data

Race conditionTwo processes editing the list at the same time

Page 26: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Synchronization variables

Create these variables to prevent these problems:

items semaphoreCounts how many items are in the bufferCannot drop below 0

slots semaphoreCounts how may slots are available in the bufferCannot drop below 0

list mutexMakes buffer access mutually exclusive

Page 27: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Producer-Consumer Exampleds7-problem1.c shows an example implementation for

one producer and one consumer, but without synchronization code.

• Running it shows– Buffer underflows

• Nonsense data is consumed– Buffer overflows

• Unconsumed data is overwritten

• Think: When should the consumer block? When should the producer block?

Page 28: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Example 2:Dining Philosophers

Page 29: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Dining Philosopher Challenge

{ Think | Eat }N Philosophers circular table with N chopsticksTo eat the Philosopher must first pickup two chopsticksith Philosopher needs ith & i+1st chopstickOnly put down chopstick when Philosopher has finished

eatingDevise a solution which satisfies mutual exclusion but

avoids starvation and deadlock

Page 30: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

The simple implementationwhile(true) {

think()lock(chopstick[i])lock(chopstick[(i+1) % N])eat()unlock(chopstick[(i+1) % N])unlock(chopstick[i])

}

Does this work?

Page 31: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Deadlocked!When every philosopher has picked up his left chopstick,

and no philosopher has yet picked up his right chopstick, no philosopher can continue.

Each philosopher waits for his right neighbor to put a chopstick down, which he will never do.

This is a deadlock.

Page 32: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Formal Requirements for Deadlock

Mutual exclusion Exclusive use of chopsticks

Hold and wait conditionHold 1 chopstick, wait for next

No preemption conditionCannot force another to undo their hold

Circular wait conditionEach waits for next neighbor to put down chopstick

The simple implementations satisfies all of these.

Page 33: CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.

Problems for Week 7 (contd)2) ds7-problem2.c contains dining

philosophers code. Alter the program to prevent deadlock. There are

multiple ways to do this.

Think: What are the conditions of deadlock? Can any of them be removed?