Classical Problems of Concurrency

Slides:



Advertisements
Similar presentations
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Advertisements

CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Stuff  Exam timetable now available  Class back in CS auditorium as of Wed, June 4 th  Assignment 2, Question 4 clarification.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Classic Synchronization Problems
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
Operating Systems Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive at.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Process Synchronization
CIS 415 Process Synchronization Background The Critical-Section Problem Software Solutions Semaphores Hardware Support Classical Problems of Synchronization.
1 Chapter 7: Process Synchronization 2 Contents Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Process Synchronization Topics: 1.Background 2.The critical-section problem 3.Semaphores 4.Critical Regions 5.Monitors Topics: 1.Background 2.The critical-section.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
6: Process Synchonization II 1 PROCESS SYNCHRONIZATION II THE BOUNDED BUFFER ( PRODUCER / CONSUMER ) PROBLEM: This is the same producer / consumer problem.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating System I Process Synchronization. Too Much Pizza 3:00 3:05 3:10 3:15 3:20 3:25 3:30 Person A Look in fridge. Pizza! Leave for store. Arrive.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 23 Classic Synchronization.
© 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.
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 and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
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.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Silberschatz and Galvin  Operating Systems Overview D. Manivannan Department of Computer Science University of Kentucky.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
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.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Classic problems of Synchronization : Producers/Consumers problem: The producers/ consumers problem may be stated as follows: Given a set of cooperating.
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization: Semaphores
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Classical Synchronization Problems
Chapter 6-7: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Synchronization Hank Levy 1.
Chapter 7: Synchronization Examples
Presentation transcript:

Classical Problems of Concurrency Operating Systems Classical Problems of Concurrency A. Frank - P. Weisberg

Introduction to Concurrency Classical Problems of Concurrency Critical Regions Monitors Inter-Process Communication (IPC) Communications in Client-Server Systems A. Frank - P. Weisberg

Classical Problems of Concurrency There are many of them – let’s briefly see three famous problems: P/C Bounded-Buffer Readers and Writers Dining-Philosophers A. Frank - P. Weisberg

Reminder: P/C problem with race condition A. Frank - P. Weisberg

P/C Bounded-Buffer Problem We need 3 semaphores: A semaphore mutex (initialized to 1) to have mutual exclusion on buffer access. A semaphore full (initialized to 0) to synchronize producer and consumer on the number of consumable items. A semaphore empty (initialized to n) to synchronize producer and consumer on the number of empty spaces. A. Frank - P. Weisberg

Bounded-Buffer – Semaphores Shared data semaphore full, empty, mutex; Initially: full = 0, empty = n, mutex = 1 A. Frank - P. Weisberg

Bounded-Buffer – Producer Process do { … produce an item in nextp wait(empty); wait(mutex); add nextp to buffer signal(mutex); signal(full); } while (TRUE); A. Frank - P. Weisberg

Bounded-Buffer – Consumer Process do { wait(full) wait(mutex); … remove an item from buffer to nextc signal(mutex); signal(empty); consume the item in nextc } while (TRUE); A. Frank - P. Weisberg

Notes on P/C Bounded-Buffer Solution Remarks (from consumer point of view): Putting signal(empty) inside the CS of the consumer (instead of outside) has no effect since the producer must always wait for both semaphores before proceeding. The consumer must perform wait(full) before wait(mutex), otherwise deadlock occurs if consumer enters CS while the buffer is empty. Conclusion: using semaphores is a difficult art ...  A. Frank - P. Weisberg

Full P/C Bounded-Buffer Solution A. Frank - P. Weisberg

Readers-Writers Problem A data set/repository is shared among a number of concurrent processes: Readers – only read the data set; they do not perform any updates. Writers – can both read and write. Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. A. Frank - P. Weisberg

Readers-Writers Dynamics Any number of reader activities and writer activities are running. At any time, a reader activity may wish to read data. At any time, a writer activity may want to modify the data. Any number of readers may access the data simultaneously. During the time a writer is writing, no other reader or writer may access the shared data. A. Frank - P. Weisberg

Readers-Writers with active readers A. Frank - P. Weisberg

Readers-Writers with an active writer A. Frank - P. Weisberg

Should readers wait for waiting writer? A. Frank - P. Weisberg

Readers-Writers problem There are various versions with different readers and writers preferences: The first readers-writers problem, requires that no reader will be kept waiting unless a writer has obtained access to the shared data. The second readers-writers problem, requires that once a writer is ready, no new readers may start reading. In a solution to the first case writers may starve; In a solution to the second case readers may starve. A. Frank - P. Weisberg

First Readers-Writers Solution (1) readcount (initialized to 0) counter keeps track of how many processes are currently reading. mutex semaphore (initialized to 1) provides mutual exclusion for updating readcount. wrt semaphore (initialized to 1) provides mutual exclusion for the writers; it is also used by the first or last reader that enters or exits the CS. A. Frank - P. Weisberg

First Readers-Writers Solution (2) Shared data semaphore mutex, wrt; int readcount; Initially mutex = 1, wrt = 1, readcount = 0 A. Frank - P. Weisberg

First Readers-Writers – Writer Process do { wait(wrt); … writing is performed signal(wrt); } while(TRUE); A. Frank - P. Weisberg

First Readers-Writers – Reader Process do { wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed readcount--; if (readcount == 0) signal(wrt); } while(TRUE); A. Frank - P. Weisberg

Dining Philosophers Problem (1) Philosophers spend their lives alternating between thinking and eating. Five philosophers can be seated around a circular table. There is a shared bowl of rice. In front of each one is a plate. Between each pair of philosophers there is a chopstick, so there are five chopsticks. It takes two chopsticks to take/eat rice, so while n is eating neither n+1 nor n-1 can be eating. A. Frank - P. Weisberg

Dining Philosophers Problem (2) Each one thinks for a while, gets the chopsticks/forks needed, eats, and puts the chopsticks/forks down again, in an endless cycle. Illustrates the difficulty of allocating resources among process without deadlock and starvation. A. Frank - P. Weisberg

Dining Philosophers Problem (3) The challenge is to grant requests for chopsticks while avoiding deadlock and starvation. Deadlock can occur if everyone tries to get their chopsticks at once. Each gets a left chopstick, and is stuck, because each right chopstick is someone else’s left chopstick. A. Frank - P. Weisberg

Dining Philosophers Solution (1) Each philosopher is a process. One semaphore per fork: fork: array[0..4] of semaphores Initialization: fork[i].count := 1 for i := 0..4 Process Pi: repeat think; wait(fork[i]); wait(fork[i+1 mod 5]); eat; signal(fork[i+1 mod 5]); signal(fork[i]); forever Note: deadlock if each philosopher starts by picking up his left fork! A. Frank - P. Weisberg

Dining Philosophers Solution (2) Possible solutions to avoid deadlock: Allow at most four philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up the forks only if both are available (picking must be done in a critical section). Use an asymmetric solution - an odd-numbered philosopher picks up first the left chopstick and then the right chopstick. Even-numbered philosopher picks up first the right chopstick and then the left chopstick. A. Frank - P. Weisberg

Dining Philosophers Solution (4) A solution: admit only 4 philosophers at a time that try to eat. Then 1 philosopher can always eat when the other 3 are holding 1 fork. Hence, we can use another semaphore T that would limit at 4 the number of philosophers “sitting at the table”. Initialize: T.count := 4 Process Pi: repeat think; wait(T); wait(fork[i]); wait(fork[i+1 mod 5]); eat; signal(fork[i+1 mod 5]); signal(fork[i]); signal(T); forever A. Frank - P. Weisberg

Dining Philosophers Solution (5) . . . A. Frank - P. Weisberg

Dining Philosophers Problem (6) A. Frank - P. Weisberg