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

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, 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.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
Classic Synchronization Problems
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Classical Problems of Concurrency
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 7: Process Synchronization
CS444/CS544 Operating Systems Deadlock 3/7/2007 Prof. Searleman
CS444/CS544 Operating Systems Classic Synchronization Problems 3/5/2007 Prof. Searleman
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CS444/CS544 Operating Systems Deadlock 3/07/2006 Prof. Searleman
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 7: Process Synchronization Background The Critical-Section Problem.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Synchronisation Examples
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CSE Operating System Principles Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
Chapter 71 Monitor for Reader/Writers  Synchronizes access if all processes follow.  If some don’t go through monitor and database is accessed directly,
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Deadlock and Starvation
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Classical Synchronization Problems
Deadlock and Starvation
Chapter 6-7: Process Synchronization
CSE451 Basic Synchronization Spring 2001
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Module 7a: Classic Synchronization
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
Problem: CFQ (Circular Fifo Queue)
Presentation transcript:

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

Outline Synchronization methods Monitors & Semaphore solutions Producer/Consumer (bounded buffer) Readers/Writers Dining Philosophers NOTE: HW#6 and Lab#2 due this week (by Friday) HW#7 posted, due 3/28 HW#7 Read: Chapter 8 Class on Thursday is HERE (Snell, NOT lab)

Announcements: IBM Workshop on IMS, Saturday, 9-4, ITL Want a Career in IMS? What is IMS you ask? Well if you've ever used an ATM then you've probably used IMS. If you've done Web banking, you've probably used IMS. If you've called FEDEX to track your package, guess what? FEDEX uses IMS. Major fortune 500 companies use it. IMS (Information Management System) is IBM's premiere Hierarchical Database system that runs on a mainframe system (those large corporate computers). Visit the IMS home page at

Announcements: IBM Workshop on IMS, Saturday, 9-4, ITL The first half of the seminar will describe the following: - Architecture of IMS - The different IMS database types - Database recovery facilities. - IMS Transaction management. - Some of the special facilities: MSC, RSR & XRF. The second half of the seminar contains a live demo operating and using an IMS system followed by discussions of the various Job opportunities in IMS: System programming, Database Administration, Application Programming, Testing and Technical support Lunch and refreshments will be provided!!! Please see Prof. Searleman or Prof. Matthews to sign up

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

Setting up a synchronization problem How to use semaphores How to use a monitor How to use condition variables Shown in class

Bounded Buffer Producer/Consumer Finite size buffer (array) in memory shared by multiple processes/threads Producer threads “produce” an item and place it in the buffer Consumer threads remove an item from the buffer and “consume” it Why do we need synchronization? Shared data = the buffer state Which parts of buffer are free? Which filled? What can go wrong? Producer doesn’t stop when no free spaces; Consumer tries to consume an empty space; Consumer tries to consume a space that is only half-filled by the producer; Two producers try to produce into same space; Two consumers try to consume the same space,…

Producer thread Reason(s) to wait? How to implement? Consumer thread Reason(s) to wait? How to implement? CONCEPT: Producers produce items to be stored in the buffer. Consumers remove and consume items which have been stored. Mutual exclusion must be enforced on the buffer itself. Moreover, producers can store only when there is an empty slot, and consumers can remove only when there is a full slot.

Monitor Solution to Producer/Consumer The buffer and its control variables are encapsulated by a monitor. The monitor provides procedures to put an item in the buffer and to take an item out of the buffer. The monitor includes two condition variables: slot_free represents the condition that there is space for an item, and item_present represents the condition that at least one item is present in the buffer. In this example, the buffer is implemented as an array of size MAX treated as a circular (ring) buffer. Variables in and out give the index of the next position for putting in and taking out (if any). Variable count gives the number of items in the buffer.

Structure of a Monitor monitor BB { // shared variables condition slot_free, item_present; anytype buffer[MAX]; int in, out, count; // monitor procedures void put_in_buffer(anytype item); anytype get_from_buffer(void); // initialization code for shared variables in = 0; out = 0; count = 0; } // end monitor BB

Producer & Consumer threads: PRODUCER : repeat { /* produce an item */ item = produce(); /* put it in the buffer */ BB.put_in_buffer(item); } until done; CONSUMER: repeat { /* get item from the buffer */ item = BB. get_from_buffer(); /* consume it */ consume(item); } until done;

void put_in_buffer(anytype item) { /* if no space is available, wait for one */ if (count >= MAX) slot_free.wait(); /* store the item */ buffer[in] = item; in = in + 1 mod n; count = count + 1; /* signal that the item is present */ item_present.signal(); } anytype get_from_buffer(void){ anytype item; /* if no items are present, wait for one */ if (count <= 0) item_present.wait(); /* get the next item */ item = buffer[out]; out = out + 1 mod n; count = count - 1; /* announce that a space is free */ slot_free.signal(); /* return the item */ return(item); }

Semaphore Solution to Bounded-Buffer semaphore_t mutex; semaphore_t full; semaphore_t empty; container_t { BOOL free = TRUE; item_t item; } container_t buffer[FIXED_SIZE]; void initBoundedBuffer{ mutex.value = 1; full.value = 0; empty.value = FIXED_SIZE }

Semaphore Solution to Bounded-Buffer void producer (){ container_t *which; wait(empty); wait(mutex); which = findFreeBuffer(); which->free = FALSE; which->item = produceItem(); signal(mutex); signal(full); } void consumer (){ container_t *which; wait(full); wait(mutex); which = findFullBuffer(); consumeItem(which->item); which->free = TRUE; signal(mutex); signal(empty); } Can we do better? Lock held while produce/consume? Exercise

Readers/writers Shared data area being accessed by multiple processes/threads Reader threads look but don’t touch We can allow multiple readers at a time. Why? Writer threads touch too. If a writer present, no other writers and no readers. Why? Is Producer/Consumer a subset of this? Producers and consumers are both writers Producer = writer type A; Consumer = writer type B; and there are no readers What might be a reader? Report current num full.

Semaphore Solution to Readers/ Writers (Reader Preference) semaphore_t mutex; semaphore_t okToWrite; int numReaders; void init{ mutex.value = 1; okToWrite.value = 1; numReaders = 0; } void writer (){ wait(okToWrite); do writing (could pass in pointer to write function) signal(okToWrite); } void reader (){ wait(mutex); numReaders++; if (numReaders ==1) wait(okToWrite); //not ok to write signal(mutex); do reading (could pass in pointer to read function) wait(mutex); numReaders--; if (numReaders == 0) signal(okToWrite); //ok to write again signal (mutex); } Can we do better? Fairness to writers?

Monitor Solution to Readers/Writers reader thread reason(s) to wait? how to implement? writer thread reason(s) to wait? how to implement? “fairness” don’t want to starve either readers or writers

Reader/Writer Monitor monitor RW { // shared variables condition OKtoread, OKtowrite; int readercount, waitingWriters, waitingReaders; boolean busy; // 4 monitor procedures void startRead(); void endRead(); void startWrite(); void endWrite(); // initialization code for shared variables readercount = 0; busy = false; waitingWriters = waitingReaders = 0; } // end monitor RW

Reader & Writer threads: READER : repeat { RW.startRead(); /* read database */ RW.endRead(); } until done; WRITER: repeat { RW.startWrite(); /* update database */ RW.endWrite(); } until done;

// Monitor procedures for readers: void startRead(){ if ( busy || (waitingWriters > 0) ) { waitingReader++; OKtoRead.wait(); waitingReaders--; } readercount = readercount + 1; OKtoRead.signal(); } void endRead(){ readercount = readercount - 1; if (readercount == 0) OKtoWrite.signal(); }

// Monitor procedures for writers: void startWrite(){ if ( (readercount != 0) || busy ) { waitingWriters++; OKtoWrite.wait(); waitingWriters--; } busy = true; } void endWrite(){ busy = false; if (waitingReaders > 0) OKtoRead.signal(); else OKtoWrite.signal(); }

Semaphore Solution to Readers/ Writers (Fair) semaphore_t readCountMutex, incoming, next; int numReaders; BOOL writeInProgress,readInProgress; void init{ readCountMutex.value = 1; incoming.value = 1; next.value = 1; numReaders = 0; writeInProgress = FALSE; readInProgress = FALSE; }

void writer() { wait(incoming); wait(next); writeInProgress = TRUE; // let someone else move // on, and wait on next signal(incoming); // do writing writeInProgress = FALSE; if (next.value == 0){ signal(next); } void reader (){ wait(incoming); if (!readInProgress) wait(next); wait(readCountMutex); numReaders++; readInProgress = TRUE; signal(readCountMutex); // if next thread on incoming // is writer, will block on next signal(incoming); // do reading wait(readCountMutex); numReaders--; if (numReaders == 0){ readInProgress = FALSE; if (next.value == 0){ signal (next); } signal(readCountMutex); }

Converting a monitor solution to a semaphore solution Basic concept: Each condition c; simulated with semaphore cSem = 0; For mutual exclusion, introduce a new semaphore: semaphore mutex = 1; A wait on a condition variable c: c.wait() becomes: signal(mutex); // release exclusion wait(cSem); // block wait(mutex); // regain exclusion before accessing // shared variables What about a signal on a condition variable?

Dining Philosophers monitor DP { enum State{thinking, hungry, eating}; State moods[NUM_PHIL]; condition self[NUM_PHIL]; void pickup(int i); void putdown(int i); void test(int i); void init() { for (int i = 0; i < NUM_PHIL; i++) state[i] = thinking; } } // end DP

Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); // check if OK to eat if (state[i] != eating) self[i].wait(); } void putdown(int i) { state[i] = thinking; // test left and right neighbors test((i+ (NUM_PHIL-1 )) % NUM_PHIL); test((i+1) % NUM_PHIL); }

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

Philosopher Threads void philosophersLife(int i) { while(1){ think(); DP.pickupChopticks(); eat(); DP.putdownChopsicks(); }

Remember Game is obtaining highest possible degree of concurrency and greatest ease of programming Tension Simple high granularity locks easy to program Simple high granularity locks often means low concurrency Getting more concurrency means Finer granularity locks, more locks More complicated rules for concurrent access

Other Classic Synchronization Problems Sleeping Barber Traffic lights for two lane road through a one lane tunnel