CS444/CS544 Operating Systems Synchronization 3/02/2006 Prof. Searleman

Slides:



Advertisements
Similar presentations
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Advertisements

Operating Systems Mehdi Naghavi Winter 1385.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 6: Process Synchronization
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
CS444/CS544 Operating Systems Classic Synchronization Problems 2/28/2007 Prof. Searleman
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Concurrency: Deadlock & Starvation
Deadlock Prevention CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS444/CS544 Operating Systems Synchronization 2/19/2007 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Monitors CSCI 444/544 Operating Systems Fall 2008.
CS444/CS544 Operating Systems Deadlock 3/7/2007 Prof. Searleman
1 Concurrency: Deadlock and Starvation Chapter 6.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
1 Deadlock Solutions CS 241 March 28, 2012 University of Illinois.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
CS444/CS544 Operating Systems Deadlock 3/07/2006 Prof. Searleman
Synchronization II: CPE Operating Systems
Programming with POSIX* Threads Intel Software College.
Chapter 7 – Deadlock (Pgs 283 – 306). Overview  When a set of processes is prevented from completing because each is preventing the other from accessing.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Deadlock CS Introduction to Operating Systems.
Deadlock and Starvation
Synchronization Deadlocks and prevention
Chapter 5: Process Synchronization – Part 3
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 6-7: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 5: Process Synchronization (Con’t)
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 25 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
Andy Wang Operating Systems COP 4610 / CGS 5765
Lecture 14: Pthreads Mutex and Condition Variables
More IPC B.Ramamurthy 4/15/2019.
Lecture 26 Syed Mansoor Sarwar
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CS444/CS544 Operating Systems Synchronization 3/02/2006 Prof. Searleman

Outline Classic Synchronization Problems NOTE: HW#6 posted, due 3/22 HW#6 Lab#2 posted, due Thurs, 3/9 Read: Chapter 7

Generalize to Messaging Synchronization based on data transfer (atomic) across a channel In general, messages can be used to express ordering/scheduling constraints Wait for message before do X Send message = signal Direct extension to distributed systems

Window’s Events & UNIX Signals Window’s Events Synchronization objects used somewhat like semaphores when they are used for ordering/scheduling constraints One process/thread can wait for an event to be signaled by another process/thread Recall: UNIX signals Kill = send signal; Signal = catch signal Many system defined but also signals left to user definition Can be used for synchronization Signal handler sets a flag Main thread polls on the value of the flag Busy wait though

Window’s Events  Create/destroy HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpsa, // security privileges (default = NULL) BOOL bManualReset, // TRUE if event must be reset manually BOOL bInitialState, // TRUE to create event in signaled state LPTSTR lpszEventName ); // name of event (may be NULL) BOOL CloseHandle( hObject );  Wait DWORD WaitForSingleObject( HANDLE hObject, // object to wait for DWORD dwMilliseconds );  Signal (all threads that wait on it receive) BOOL SetEvent( HANDLE hEvent ); //signal on BOOL ResetEvent( HANDLE hEvent ); //signal off

Pthread’s Condition Variables  Create/destroy int pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *attr); int pthread_cond_destroy (pthread_cond_t *cond);  Wait int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mut);  Timed Wait int pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mut, const struct timespec *abstime);  Signal int pthread_cond_signal (pthread_cond_t *cond);  Broadcast int pthread_cond_broadcast (pthread_cond_t *cond);

Pseudo-Monitors Monitor = a lock (implied/added by compiler) for mutual exclusion PLUS zero or more condition variables to express ordering constraints What if we wanted to have monitor without programming language support? Declare locks and then associate condition variables with a lock If wait on the condition variable, then release the lock

Example: Pseudo-monitors pthread_mutex_t monitorLock; pthread_cond_t conditionVar; void pseudoMonitorProc(void) { pthread_mutex_lock(&mutexLock); ….. pthread_cond_wait(&conditionVar, &monitorLock); …. pthread_mutex_unlock(&mutexLock); }

Software Synchronization Primitives Summary Locks Simple semantics, often close to HW primitives Can be inefficient if implemented as a spin lock Semaphores Internal queue – more efficient if integrated with scheduler Monitors Language constructs that automate the locking Easy to program with where supported and where model fits the task Once add in condition variables much of complexity is back Events/Messages Simple model of synchronization via data sent over a channel

Conclusion? Synchronization primitives all boil down to representing shared state (possibly large) with a small amount of shared state All need to be built on top of HW support Once have one kind, can usually get to other kinds Which one you use is a matter of programmatic simplicity (matching primitive to the problem) and taste

ValueQueue“acquire” op “release” op “broadcast” or “release all” op Lock 0/1?Lock Block till value = 0; set value = 1 Unlock Value = 0 No Semaphore INTYWait, down, P va,lue— If value < 0 Add self to queue Signal, up, V Value++ If value <=0 Wake up one No? While (getValue()<0){ Signal } Condition variable N/AYWait Put self on queue Signal If process on queue, wake up one Easy to Support A signal all Monitor N/AYCall proc in monitorReturn from proc in monitor No

Classical Synchronization Problems Bounded-Buffer Problem (also called Producer-Consumer) one-way communication with limited resources Dining-Philosophers Problem shared resources Readers and Writers Problem shared database

Dining-Philosophers Problem semaphore_t chopstick[NUM_PHILOSOPHERS]; void init(){ for (i=0; i< NUM_PHILOSOPHERS; i++) chopstick[i].value = 1; }

Semaphore Solution to Dining Philosophers void philosophersLife(int i){ while (1) { think(); wait(chopstick[i]) grab_chopstick(i); wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]) grab_chopstick(i-1); eat(); putdownChopsticks() signal(chopstick[i]); signal(chopstick[(i-1) % NUM_PHILOSOPHERS ]); } Problem? philosopher 0 gets chopstick 0 philosopher 1 gets chopstick 1 …. philosopher N gets chopstick N Deadlock! Solution?

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!). Dining Philosophers is a perfect example. Each holds one chopstick and will wait forever for the other.

Resource Allocation Graph Deadlock can be described through a resource allocation graph Each node in graph represents a process/thread or a resource An edge from node P to R indicates that process P had requested resource R An edge from node R to node P indicates that process P holds resource R If graph has cycle, deadlock may exist. If graph has no cycle, deadlock cannot exist.

Cycle in Resource Allocation Graph Philosopher 0 Philosopher 1 Philosopher 2 Philosopher 3 Chopstick 2 Chopstick 1 Chopstick 0 Chopstick 3 wait(chopstick[i]) wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]) Cycle: P0, C3, P3, C2, P2, C1, P1, C0, P0 Philosopher 1 has chopstick 1 and wants chopstick 0 Philosopher 2 has chopstick 2 and wants chopstick 1 Philosopher 3 has chopstick 3 and wants chopstick 2 Philosopher 0 has chopstick 0 and wants chopstick 3

Fixing Dining Philosophers Make philosophers grab both chopsticks they need atomically Maybe pass around a token (lock) saying who can grab chopsticks Make a philosopher give up a chopstick Others?

Better Semaphore Solution to Dining Philosophers void philosophersLife(int i){ while (1) { think(); if ( i < ((i-1) % NUM_PHILOSOPHERS ))}{ wait(chopstick[i]); wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]); } else { wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]); wait(chopstick[i]); } eat(); signal(chopstick[i]); signal(chopstick[(i-1) % NUM_PHILOSOPHERS ]); } Why better? philosopher 0 gets chopstick 0 philosopher 1 gets chopstick 1 …. philosopher N waits for chopstick 0 No circular wait! No deadlock!! Always wait for low chopstick first

No Cycle in Resource Allocation Graph if ( i < ((i-1) % NUM_PHILOSOPHERS ))}{ wait(chopstick[i]); wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]); } else { wait(chopstick[(i-1) % NUM_PHILOSOPHERS ]); wait(chopstick[i]); } Philosopher 0 Philosopher 1 Philosopher 2 Philosopher 3 Chopstick 2 Chopstick 1 Chopstick 0 Chopstick 3 No cycle! No deadlock! Philosopher 0 will eat, Then 3, then 2, then 1

Conditions for Deadlock Deadlock can exist if and only if the following four conditions are met; Mutual Exclusion – some resource must be held exclusively Hold and Wait – some process must be holding one resource and waiting for another No preemption – resources cannot be preempted 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 All these held in the Dining Philosopher’s first “solution” we proposed

Deadlock Prevention Four necessary and sufficient conditions for deadlock Mutual Exclusion Hold and Wait No Preemption Circular Wait Preventing mutual exclusion isn’t very helpful. If we could allow resources to be used concurrently then we wouldn’t need the synchronization anyway! Preventing the others?

Preventing Hold and Wait Do not allow processes to hold a resource when requesting others Make philosophers get both chopsticks at once 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!

Preventing No Preemption Preemption (have to love those double negative ) Allow system to take back resources once granted Make some philosopher give back a chopstick Disadvantage: Hard to program System figures out how to take away CPU and memory without breaking programmer’s illusion How do you take away access to an open file or a lock once granted?? Would need API to notify program and then code to deal with the removal of the resource at arbitrary points in the code Checkpoint and Rollback?

Preventing Circular wait Impose an ordering on the possible resources and require that processes request them in a specific order How did we prevent deadlock in dining philosophers? Numbered the chopsticks Made philosophers ask for lowest number chopstick first Disadvantage: Hard to think of all types of resources in system and number them consistently for all cooperating processes I use a resource X and Y, you use resource Y and Z and W, someone else uses W, T, R – which is resource 1? (shared files, databases, chopsticks, locks, events, …) For threads in the same process or closely related processes often isn’t that bad

Deadlock Avoidance Avoidance vs Prevention? Both actually prevent deadlock Deadlock Prevention does so by breaking one of the four necessary conditions Deadlock Avoidance allows processes to make any request they want (not constrained in ways so as to break one of the four conditions) *as long as* they declare their maximum possible resource requests at the outset Deadlock avoidance usually results in higher resource allocation by allowing more combinations of resource requests to proceed than deadlock prevention Still deadlock avoidance can deny resource requests that would not actually lead to deadlock in practice

Dining Philosophers Example monitor diningPhilosophers { enum State{thinking, hungry, eating}; State moods[NUM_PHILOSOPHERS]; conditionVariable self[NUM_PHILOSOPHERS]; void pickup(int i); void putdown(int i) ; void test(int i) ; void init() { for (int i = 0; i < NUM_PHILOSOPHERS; i++) state[i] = thinking; }

Dining Philosophers void pickupChopsticks(int i) { state[i] = hungry; test[i]; if (state[i] != eating) self[i].wait(); } void putdownChopsticks(int i) { state[i] = thinking; // test left and right neighbors test((i+(NUM_PHILOSOPHERS-1)) % NUM_PHILOSOPHERS); test((i+1) % NUM_PHILOSOPHERS); }

Dining Philosophers void test(int i) { if ( (state[(i+NUM_PHILOSOPHERS-1) % NUM_PHILOSOPHERS] != eating) && (state[i] == hungry) && (state[(i+1) % NUM_PHILOSOPHERS] != eating)) { state[i] = eating; self[i].signal(); }

Dining Philosophers void philosophersLife(int i) { while(1){ think(); pickupChopticks(); eat(); putdownChopsicks(); }