+ All Categories
Home > Documents > CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman [email protected].

CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman [email protected].

Date post: 20-Dec-2015
Category:
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman [email protected]
Transcript
Page 1: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

CS444/CS544Operating Systems

Synchronization

2/21/2007

Prof. Searleman

[email protected]

Page 2: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

CS444/CS544 Spring 2007 Synchronization w/ semaphores/locks

uses problems

Deadlock Monitors

Reading assignment: Chapter 6

HW#5 posted: due Monday, 2/26/07

Page 3: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Recap: Uses of Semaphores Mutual exclusion

Access to shared resource (critical section) Binary semaphore, initalized to 1 “hold”

Managing N copies of a resource Counting semaphore, initialized to N “enter”

Anything else? Another type of synchronization is to express

ordering/scheduling constraints “Don’t allow x to proceed until after y”

Page 4: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Semaphores for expressing ordering

Initialize semaphore value to 0 semaphore synch = 0;

Code: Pi Pj

A wait(synch);

signal(synch); B

Execute B in Pj only after A executed in

Pi regardless of when Pi & Pj run Note: If signal executes first, wait will find it is an

signaled state (history!)

Page 5: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Consider the following code:

/* N processes, P1 … PN */

semaphore mutex = 1;

Pi: signal(mutex);

c.s.

wait(mutex);

What happens? Answer: It depends, but no guarantee of mutual exclusion

Page 6: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

How about this?

semaphore S1 = 1; /* protects object O1 */semaphore S2 = 1; /* protects object O2 */

P0: wait(S1); P1: wait(S2); wait(S2); wait(S1); /* use O1 & O2 */ /* use O1 & O2 */

signal(S1); signal(S2); signal(S2); signal(S1);

What happens? Answer: It depends, but possible deadlock

Page 7: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Deadlock

Deadlock exists in a set of processes/threads when all processes/threads in the set are waiting for an event that can only be caused by another process in the set (which is also waiting!).

cf. Chapter 7

Page 8: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Preview: Conditions for Deadlock

Deadlock can exist if and only if the following

four conditions are met:1. Mutual Exclusion – some resource must be held

exclusively

2. Hold and Wait – some process must be holding at least one resource and waiting for another

3. No preemption – resources cannot be preempted

4. Circular wait – there must exist a set of processes (p1,p2, …pn) such that p1 is waiting for p2, p2 is waiting for p3, … pn is waiting for p1

Page 9: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Preview: Handling Deadlock

There are a number of ways to handle deadlock.One possibility: Deadlock Prevention that is, guarantee that deadlock cannot occur no

matter whatHow? guarantee that at least one of the four necessary

& sufficient conditions for deadlock cannot occur:(1) Mutual Exclusion (2) Hold and Wait(3) No Preemption (4) Circular Wait

can’t do much about this

we’ll look at these 2 for now (more on this topic later)

Page 10: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Preventing Hold and Wait

Do not allow processes to hold a resource when requesting others No partial allocation Window’s WaitForMultipleObjects

Make processes ask for all resources they need at the beginning Disadvantage: May not need all resources the whole time Can release them early but must hold until used

Make processes release any held resources before requesting more Hard to program!

Page 11: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Preventing Circular wait

Impose an ordering on the possible resources and require that processes request them in a specific order Number the resources Always request them in some order, say increasing

order Why does this work? How does this apply to the locking lab? (see

example done in class)

Page 12: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Recap: Problems with Locks and Semaphores

There is no syntactic connection between the semaphore (or lock or event) and the shared data/resources it is protecting Thus the “meaning” of the semaphore is defined

by the programmer’s use of it Poor software design

Semaphores basically global variables accessed by all threads

Easy for programmers to make mistakes Also no separation between use for mutual exclusion

and use for resource management and use for expressing ordering/scheduling constraints

Page 13: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Programming Language Support

Add programming language support for synchronization Declare a section of code to require mutually

exclusive access (like Java’s synchronized) Associate the shared data itself with the locking

automatically Monitor = programming language support to

enforce synchronization Mutual exclusion code added by the compiler!

Page 14: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Monitors

A monitor is a software module that encapsulates: Shared data structures Procedures that operated on them Synchronization required of processes that invoke

these procedures Like a public/private data interface prevents

access to private data members; Monitors prevent unsynchronized access to shared data structures

Page 15: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Example: bankAccountMonitor bankAccount{

int balance;

int readBalance( ){return balance};void upateBalance(int newBalance){

balance = newBalance;} int withdraw (int amount) {

balance = balance – amount;return balance;

}int deposit (int amount){

balance = balance + amount;return balance;

}}

Locking addedby the compiler!

Page 16: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Monitor

S

balance

readBalance

updateBalance

withdraw

deposit

Shared data

ProceduresWaiting queue

One threadIn Monitor

Page 17: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Waiting Inside a Monitor

What if you need to wait for an event within one of the procedures of a monitor?

Monitors as we have seen to this point enforce mutual exclusion – what about the

Introduce another synchronization object, the condition variable

Within the monitor declare a condition variable:

condition x;

Page 18: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Wait and signal

Condition variables, like semaphores, have the two operations have the two operations, wait and signal. The operation x.wait() means that the process

invoking this operation is suspended until another process invokes x.signal();

The operation wait allows another process to enter the monitor (or no one could ever call signal!)

The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect

Page 19: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Monitor With Condition Variables

S

balance

readBalance

updateBalance

withdraw

deposit

Waiting queue

One threadRunning in Monitor

Condition Variables and their associatedwait queues

Page 20: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Semaphores vs Condition Variables

I’d like to be able to say that condition variables are just like semaphores but …

With condition variables, if no process is suspended then the signal operation has no effect

With semaphores, signal increments the value regardless of whether any process is waiting

Semaphores have “history” (they remember signals) while condition variables have no history

Page 21: CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman jets@clarkson.edu.

Condition Variable Alone?

Could you use a condition variable concept outside of monitors?

Yes, basically a semaphore without history Couldn’t do locking with it because no mutual

exclusion on its own Couldn’t do resource management (counting

semaphore) because no value/history Can use it for ordering/scheduling constraints

(more on this later)


Recommended