IPC and Classical Synchronization Problems

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

Operating Systems Mehdi Naghavi Winter 1385.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
©2009 Operačné systémy Procesy. 3.2 ©2009 Operačné systémy Process in Memory.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Cooperating Processes Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Classic Synchronization Problems
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Classical Problems of Concurrency
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.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Dining Philosophers and the Deadlock Concept. Announcements Homework 2 and Project 2 Design Doc due Today –Make sure to look at the lecture schedule to.
1/26/2007CSCI 315 Operating Systems Design1 Processes Notice: The slides for this lecture have been largely based on those accompanying the textbook Operating.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
Interprocess Communication. Process Concepts Last class.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6(b): Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Inter-Process Communication Mechanisms CSE331 Operating Systems Design.
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems.
Silberschatz, Galvin and Gagne  Operating System Concepts Cooperating Processes Independent process cannot affect or be affected by the execution.
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
3.1 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Interprocess Communication Processes within a system may be.
Processes. Chapter 3: Processes Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication.
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 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 3 Process Scheduling Bernard Chen Spring 2007.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 10 Processes II Read.
PREPARED BY : MAZHAR JAVED AWAN BSCS-III Process Communication & Threads 4 December 2015.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 4: Processes Process Concept Process Scheduling Operations on Processes Cooperating.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Chapter 7 - Interprocess Communication Patterns
Processes. Process Concept Process Scheduling Operations on Processes Interprocess Communication Communication in Client-Server Systems.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
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 ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Gokul Kishan CS8 1 Inter-Process Communication (IPC)
CSC 360, Instructor: Kui Wu IPC. CSC 360, Instructor: Kui Wu Agenda 1.The need to communicate 2.Shared memory 3.Message passing.
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
Interprocess Communication Race Conditions
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.
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Message Passing, Scheduler
Chapter 5: Process Synchronization
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
Chapter 7: Synchronization Examples
Chapter 6 Synchronization Principles
Process Synchronization
Chapter 7: Synchronization Examples
CIS 720 Lecture 6.
Presentation transcript:

IPC and Classical Synchronization Problems

How do processes communicate? IPC (Inter Process Communication) Mechanism to exchange data Why? Share information Computational speedup Modularity Convenience Why not use threads? Functionality, manufacturers, …

IPC: Message Passing OS provided mechanism Address space is NOT shared Process use 2 operations: send(message) receive(message) Size of message: Fixed: simple implementation but difficult to use Variable: commonly used

Naming Direct communication Hard coding required! Explicitly name the end-point Link is between exactly two processes Between any two processes there is only one link Symmetric: Name the endpoints Send(P, msg) and Receive(Q, msg) Asymmetric: Receive does not name endpoint Send(P, msg) and Receive(id, msg) Hard coding required!

Naming Indirect communication: Mailbox or ports Who owns the mailbox? Messages sent and removed from a mailbox Link can be between more than 2 processes More than 2 processes can share a link Who owns the mailbox? Process: Different from shared memory Owner is the receiver Other processes can only send OS: System calls to create, communicate and delete mailboxes Ownership of mailbox can be passed using system calls

IPC: Shared Memory Memory is shared among processes Shared memory is part of a process’s address space The part to be shared is decided by process and not OS Example: POSIX

Comparing these schemes Message passing: Easier to implement No conflicts to consider, good for small data transfers Example: MPI Shared memory Faster for local transfers Cheriton’84 to make message passing fast Distributed Shared Memory vs Message Passing Distributed computation? Multiprocessor architectures?

Synchronization Problems Producer-Consumer Problem Readers-Writers Problem Dining-Philosophers Problem Sleeping Barber Problem

Producer-Consumer Problem Unbounded buffer Producer process writes data to buffer Writes to In and moves rightwards Consumer process reads data from buffer Reads from Out and moves rightwards Should not try to consume if there is no data Out In Need an infinite buffer

Producer-Consumer Problem Bounded buffer: size ‘N’ Producer process writes data to buffer Should not write more than ‘N’ items Consumer process reads data from buffer Should not try to consume if there is no data In Out

Producer-Consumer Problem A number of applications: Compiler’s output consumed by assembler Assembler’s output consumed by loader Web server produces data consumed by client’s web browser Example: pipe ( | ) in Unix > cat file | more > prog | sort … what happens here?

Producer-Consumer Problem First attempt to solve: Shared: int counter; any_t buffer[N]; Init: counter = 0; Producer while (true) { /* produce an item in nextProduced*/ while (counter == N) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % N; counter++; } Consumer while (true) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % N; counter--; /* consume an item in nextConsumed*/ }

Producer-Consumer Problem Shared: Semaphores mutex, empty, full; Init: mutex = 1; /* for mutual exclusion*/ empty = N; /* number empty bufs */ full = 0; /* number full bufs */ Producer do { . . . // produce an item in nextp P(empty); P(mutex); // add nextp to buffer V(mutex); V(full); } while (true); Consumer do { P(full); P(mutex); . . . // remove item to nextc V(mutex); V(empty); // consume item in nextc } while (true);

Readers-Writers Problem Courtois et al 1971 Models access to a database Example: airline reservation

Readers-Writers Problem Many processes share a database Some processes write to the database Only one writer can be active at a time Any number of readers can be active simultaneously This problem is non-preemptive Wait for process in critical section to exit First Readers-Writers Problem: Readers get higher priority, and do not wait for a writer Second Readers-Writers Problem: Writers get higher priority over Readers waiting to read Courtois et al.

First Readers-Writers Shared variables: Semaphore mutex, wrl; integer rcount; Init: mutex = 1, wrl = 1, rcount = 0; Writer do { P(wrl); . . . /*writing is performed*/ V(wrl); }while(TRUE); Reader do { P(mutex); rcount++; if (rcount == 1) P(wrl); V(mutex); . . . /*reading is performed*/ rcount--; if (rcount == 0) V(wrl); }while(TRUE);

Readers-Writers Notes If there is a writer First reader blocks on wrl Other readers block on mutex Once a writer exists, all readers get to go through Which reader gets in first? The last reader to exit signals a writer If no writer, then readers can continue If readers and writers waiting on wrl, and writer exits Who gets to go in first? Why doesn’t a writer need to use mutex?

Dining Philosopher’s Problem Dijkstra Philosophers eat/think Eating needs two forks Pick one fork at a time How to avoid deadlock? Example: multiple processes competing for limited resources

A non-solution # define N 5 Philosopher i (0, 1, .. 4) do { think(); take_fork(i); take_fork((i+1)%N); eat(); /* yummy */ put_fork(i); put_fork((i+1)%N); } while (true);

Will this work? Shared: semaphore fork[5]; Init: fork[i] = 1 for all i=0 .. 4 Philosopher i do { P(fork[i]); P(fork[i+1]); /* eat */ V(fork[i]); V(fork[i+1]); /* think */ } while(true);

Dining Philosophers Solutions Allow only 4 philosophers to sit simultaneously Asymmetric solution Odd philosopher picks left fork followed by right Even philosopher does vice versa Pass a token Allow philosopher to pick fork only if both available

One possible solution Shared: int state[5], semaphore s[5], semaphore mutex; Init: mutex = 1; s[i] = 0 for all i=0 .. 4 take_fork(i) { P(mutex); state[i] = hungry; test(i); V(mutex); P(s[i]); } put_fork(i) { state[i] = thinking; test((i+1)%N); test((i-1+N)%N); Philosopher i do { take_fork(i); /* eat */ put_fork(i); /* think */ } while(true); test(i) { if(state[i] == hungry && state[(i+1)%N] != eating && state[(i-1+N)%N != eating) { state[i] = eating; V(s[i]); }