1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

1 Implementations: User-level Kernel-level User-level threads package each u.process defines its own thread policies! flexible mgt, scheduling etc…kernel.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Synchronization Principles Gordon College Stephen Brinton.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
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.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Chapter 6: Process 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. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
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.
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.
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.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
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.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock and Starvation
Interprocess Communication Race Conditions
Chapter 6: Process Synchronization
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Process Synchronization
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization – Part II
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Critical section problem
Chapter 7: Synchronization Examples
CSE 153 Design of Operating Systems Winter 2019
Synchronization CSE 2431: Introduction to Operating Systems
Presentation transcript:

1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation (busy waiting); Peterson’s solution; TSL instruction ( TSL reg, LOCK and MOVE LOCK, 0 ) –Sleep & Wakeup Producer-Consumer Problem (bounded buffer)

2 ME Solutions: Today Memory-sharing –All previous primitives/mechanisms are memory-sharing –Semaphores as mutex and as synchronization primitive –Monitors Message-passing solutions Barriers Classical ME / concurrency problems –Access problems(Readers/Writers Problem) –Synchronization problems(Dining Philosophers Problem) –Scheduling(Sleeping Barber Problem)

3 Semaphores? Do SW mechanisms exist for synchronization than HW TSL? Semaphores (S): Integer Variables [System Calls] –Accessible only through 2 standard atomic operations (i.e., operation must execute indivisibly) of: wait() signal()  Wait(S) { ; sleep while S 0 ; // no-op S--; }  Signal(S) { ; wakeup S++; } S can be an integer resource counter; If S is binary, it is called a “mutex” wait(1)  progress & decrement wait(0)  block signal(0)  increment and unblock

4 Binary Semaphores with TSL: Mutexes

5 ME using Semaphores do { wait (mutex);mutex initialized to 1 // critical section signal (mutex); // non critical section } while (TRUE);

6 Ordering (Sync.) with Semaphores Consider 2 concurrent processes P1/P2 with statements S1/S2 Would like S2 to be executed only after S1 completed Let P1 and P2 share a common semaphore “sync” set to 0 –[in P1] S1; Statement S1 executes in Process P1 signal(sync); –[in P2] wait(sync); S2; Statement S2 executes in Process P2 [sync = 0  P2 executes S2 only after P1 has invoked signal(sync); which is only after S1 has been executed]

7 Semaphores The producer-consumer problem using semaphores mutual exclusion  synchronization  | CS  |

8 Semaphore Constructs in Java Implemented by java.util.concurrent.Semaphore class The class uses special language constructs that use the wait() and signal() system calls public Semaphore available = new Semaphore(100); available.acquire(); //available-- ; uses wait() syscall; available.release();//available++; uses signal() syscall; …and other available methods, as acquire(int n); release (int n); acquireUninterrupted() etc. So are higher level sync abstractions useful?

9 Semaphore Problems? Semaphore  effective SW level synchronization System calls (simple!) But, timing errors are still possible through misuse of Wait/Signal  –process interchanges order of wait() and signal() ops on the semaphore wait(mutex) CS signal(mutex)  signal(mutex) … CS … wait(mutex) several processes may end up executing their CS concurrently –Suppose a user replaces signal(mutex) with wait(mutex) wait(mutex) … CS … wait(mutex) deadlock!!! –Suppose the process omits wait(mutex) or signal(mutex) or both ME violated or deadlock

10 Monitors: Language Constructs not System Calls // shared variable declarations

11 Monitors Outline of producer-consumer problem with monitors - only one monitor procedure active at one time - buffer has N slots

12 Monitors in Java Solution to producer-consumer problem in Java

13 Monitors in Java Solution to producer-consumer problem in Java

14 Problems? TSL: lowest level (HW), but busy-waits, priority inversion problem Let’s block instead of busy-waiting… Semaphores: low-level (kernel), depend too much on programmer’s skills Monitors: need language support (C/Pascal?) All: memory sharing solutions, work only on the same machine but not if the processes sit in different machines (LAN etc.) Let’s look at message passing solutions (send/receive)

15 Producer-Consumer with Message Passing Ques: what happens if the producer (or the consumer) is much faster at processing messages than the consumer (or producer)?

16 Barriers (primitives) for Synchronization Use of a barrier (~ AND operation) a)processes approaching a barrier b)all processes blocked at barrier, waiting for C c)last process (C) arrives, all are let through

17 Synchronization Implementations Solaris –adaptive mutex, semaphores, RW locks, threads blocked by locks etc Windows XP –interrupt masking, busy-waiting spin locks (for short code segments), mutex, semaphores, monitors, msg. passing Linux –pre v2.6 (non-preemptible); post v2.6 pre-emptible: interrupts –semaphores, spin-locks (for short CS’s in kernel only)

18 Classical ME/Concurrency Problems Access problems (Readers/Writers Problem) Synch. problems (Dining Philosophers Problem) Scheduling (Sleeping Barber Problem)

19 Readers-Writers Problem A data set is shared among a number of concurrent processes –Readers – only read the database; they do not perform any updates –Writers – can both read and write. Problem – allow multiple readers to queue to read at the same time. Only one single writer can access the shared data at a time. Shared Data –Database –Integer readcount initialized to 0 (# of processes currently reading object) –Semaphore mutex initialized to 1; controls the access to readcount –Semaphore db initialized to 1; controls access to database;

20 Writer/Reader Processes: Structure while (true) { wait (db) ; …writing performed… signal (db) ; } while (true) { wait (mutex) ; // ME for readcount readcount ++ ; if (readcount == 1) wait (db) ; signal (mutex); …reading performed… wait (mutex) ; readcount - - ; if (readcount == 0) signal (db) ; signal (mutex) ; } initialize: db = 1, mutex = 1, readcount = 0 1 reader queued on “db”; N-1 readers queued on “mutex”

21 The Readers and Writers Problem

22 Dining Philosophers Philosophers eat/think Eating needs 2 chopsticks (forks) Pick one instrument at a time Solutions (with semaphores?)…

23 Dining Philosophers – Obvious Solution wait signal Deadlocks? – all pick the left fork at the same time  add a check if the fork is available  Livelock! wait signal

24 Dining-Philosophers Problem Philosopher i: while (true) { …think… wait ( chopstick[i] ); wait ( chopstick[ (i + 1) % N] ) signal (…) ? random backoff? …eat… signal (chopstick[i] ); signal (chopstick[ (i + 1) % N] ); } Needs: No deadlock No starvation for anyone Maximum parallelism Deterministic!

25 Dining Philosophers – No deadlocks - Max Parallelism Solution

26 Continuation… Deadlock free + max. parallelism (2 eat) !!!! // acquire forks

27 Bounded-Buffer Problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N.

28 Producer/Consumer Process while (true) { …produce an item… wait (empty); wait (mutex); …add the item to the buffer… signal (mutex); signal (full); } while (true) { wait (full); wait (mutex); …remove an item from buffer… signal (mutex); signal (empty); …consume the removed item… } initialize: mutex = 1, full = 0, empty = N

29 The Sleeping Barber Problem 1 Barber 1 Barber Chair N Customer Chairs

30 The Sleeping Barber Problem