Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Classic Synchronization Problems
Classical Problems of Concurrency
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Dining Philosophers, Monitors, and Condition Variables
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
1 Concurrency: Deadlock and Starvation Chapter 6.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
1 Processes, Threads, Race Conditions & Deadlocks Operating Systems Review.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
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.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Synchronizing Threads with Semaphores
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Inter Process Communication & Timers. 2 Time.h (page R:Ch9 pp ) #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
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.
Producer-Consumer Problem David Monismith cs550 Operating Systems.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
CIS Operating Systems Synchronization
Process Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Classical Synchronization Problems
HW1 and Synchronization & Queuing
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Critical Section and Critical Resources
Critical Section and Critical Resources
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Computer Science & Engineering Electrical Engineering
Chapter 6 Synchronization Principles
Chapter 7: Synchronization Examples
Operating System 6 CONCURRENCY: MUTUAL EXCLUSION AND SYNCHRONIZATION
Presentation transcript:

Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization

Copyright ©: Nahrstedt, Angrave, Abdelzaher 2 CS241 Administrative This week SMP4 is due today HW1 will be out next (practice for midterm)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 3 This lecture Goals: Introduce classic synchronization problems Topics Producer-Consumer Dining Philosophers

Copyright ©: Nahrstedt, Angrave, Abdelzaher 4 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 5 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 6 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 7 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 8 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 9 Producer-Consumer

Copyright ©: Nahrstedt, Angrave, Abdelzaher 10 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 11 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 12 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 13 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 14 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 15 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 16 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 17 Producer-Consumer insertPtr removePtr BUFFER FULL: Producer must be blocked!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 18 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 19 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 20 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 21 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 22 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 23 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 24 Producer-Consumer insertPtr removePtr

Copyright ©: Nahrstedt, Angrave, Abdelzaher 25 Producer-Consumer insertPtr removePtr BUFFER EMPTY: Consumer must be blocked!

Copyright ©: Nahrstedt, Angrave, Abdelzaher 26 Producer-Consumer Problem Producers insert items Consumers remove items Shared bounded buffer * * Efficient implementation is a circular buffer with an insert and a removal pointer.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 27 Producer-Consumer Problem Producer inserts items. Updates insertion pointer. Consumer executes destructive reads on the buffer. Updates removal pointer Both update information about how full/empty the buffer is. Solution should allow multiple readers/writers

Copyright ©: Nahrstedt, Angrave, Abdelzaher 28 Challenge Prevent buffer overflow Prevent buffer underflow Proper synchronization

Copyright ©: Nahrstedt, Angrave, Abdelzaher 29 Solution Prevent overflow: block producer when full! Counting semaphore to count #free slots 0  block producer Prevent underflow: block consumer when empty!Counting semaphore to count #items in buffer 0  block consumer Mutex to protect accesses to shared buffer & pointers.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 30 Assembling the solution sem_wait(slots), sem_post(slots) sem_wait(items), sem_post(items) mutex_lock(m) mutex_unlock(m) insertptr=(insertptr+1) % N removalptr=(removalptr+1) % N Initialize semaphore slots to size of buffer Initialize semaphore items to zero.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 31 Pseudocode getItem() Error checking/EINTR handling not shown sem_wait(items) mutex_lock(mutex) result=buffer[ removePtr ]; removePtr=(removePtr +1 ) % N mutex_unlock(mutex) sem_post(slots)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 32 Pseudocode putItem(data) Error checking/EINTR handling not shown sem_wait(slots) mutex_lock(mutex) buffer[ insertPtr]= data; insertPtr=(insertPtr + 1 ) % N mutex_unlock(mutex) sem_post(items)

Copyright ©: Nahrstedt, Angrave, Abdelzaher 33 Dining Philosophers

Copyright ©: Nahrstedt, Angrave, Abdelzaher 34 Dining Philosopher Challenge { Think | Eat } N Philosophers circular table with N chopsticks To eat the Philosopher must first pickup two chopsticks i th Philosopher needs i th & i+1 st chopstick Only put down chopstick when Philosopher has finished eating Devise a solution which satisfies mutual exclusion but avoids starvation and deadlock

Copyright ©: Nahrstedt, Angrave, Abdelzaher 35 Seems simple enough …? while(true) { think() lock(chopstick[i]) lock(chopstick[(i+1) % N]) eat() unlock(chopstick[(i+1) % N]) unlock(chopstick[i]) }

Copyright ©: Nahrstedt, Angrave, Abdelzaher 36 What if? Made picking up left and right chopsticks an atomic operation? or, N-1 Philosophers but N chopsticks?

Copyright ©: Nahrstedt, Angrave, Abdelzaher 37 What if? Made picking up left and right chopsticks an atomic operation? or, N-1 Philosophers but N chopsticks? … both changes prevent deadlock.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 38 Formal Requirements for Deadlock Mutual exclusion Hold and wait condition No preemption condition Circular wait condition Original scenario & our proposed ritual had all four of these properties.

Copyright ©: Nahrstedt, Angrave, Abdelzaher 39 Formal Requirements for Deadlock Mutual exclusion Exclusive use of chopsticks Hold and wait condition Hold 1 chopstick, wait for next No preemption condition Cannot force another to undo their hold Circular wait condition Each waits for next neighbor to put down chopstick

Copyright ©: Nahrstedt, Angrave, Abdelzaher 40 Define the Input and Output of the Following time() Input Output ctime() Input Output gettimeofday() Input Output