© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.

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: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch 7 B.
Chapter 2 Processes and Threads
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Interprocess Communication
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
© 2004, D. J. Foreman 1 The Dining Philosophers Problem.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Process Synchronization
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CS510 Concurrent Systems Introduction to Concurrency.
Experience with Processes and Monitors in Mesa
1 CS 333 Introduction to Operating Systems Class 6 – Monitors and Message Passing Jonathan Walpole Computer Science Portland State University.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION 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.
Slide 9-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 9.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
CY2003 Computer Systems Lecture 04 Interprocess Communication.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Homework-6 Questions : 2,10,15,22.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Process Synchronization
Chapter 5: Process Synchronization
CS510 Operating System Foundations
Chapter 5: Process Synchronization
Module 7a: Classic Synchronization
Critical section problem
Chapter 6: Synchronization Tools
Monitors and Inter-Process Communication
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication

© 2004, D. J. Foreman 2 Basic Concept  Consists of a lock and zero or more condition variables

© 2004, D. J. Foreman 3 The Dining Philosophers Philosopher (int i) { while (TRUE) {// Think (wait for forks) then Eat P (fork[i]); // test left fork P (fork[(i+1) mod 5]); // test right fork eat(); V (fork[(i+1) mod 5]); V (fork[i]); } semaphore fork[5] = (1,1,1,1,1); // create philosopher processes fork (philosopher, 1, 0); fork (philosopher, 1, 1); fork (philosopher, 1, 2); fork (philosopher, 1, 3); fork (philosopher, 1, 4);

© 2004, D. J. Foreman 4 One Solution philosopher (int i) { while (TRUE) {// Think (wait for forks) then Eat j = i % 2; P (fork [(i+j) mod 5]); P (fork [(i+1-j) mod 5]); eat(); V (fork [(i+1-j) mod 5]); V (fork [(i+j) mod 5]); } semaphore fork[5] = (1,1,1,1,1); fork (philosopher, 1, 0); fork (philosopher, 1, 1); fork (philosopher, 1, 2); fork (philosopher, 1, 3); fork (philosopher, 1, 4);

© 2004, D. J. Foreman 5 AND Synchronization  For n resources: R n  Some P i ->R j, maybe >1 in one critsec  order of P operations may cause deadlock  Using semaphores S i  P simultaneous (S 1, …, S n )

© 2004, D. J. Foreman 6 Simultaneous Semaphores P_sim (semaphore S, int N) {L1: if ( (S[0]>=1)&& … &&(S[N-1]>=1) ) { for (i=0; i<N; i++) S[i]--; } else { Enqueue the calling thread in the queue for the first S[i] where S[i]<1; The calling thread is blocked while it is in the queue; // When the thread is removed from the queue Goto L1; // this is an algorithm, not code! } V_sim (semaphore S, int N) {for (i=0; i<N; i++) {S[i]++; Dequeue all threads in the queue for S[i]; All such threads are now ready to run (but may be blocked again in Psimultaneous ); } else {} }

© 2004, D. J. Foreman 7 Dining Philosophers Re-visited Philosopher (int i) {while (TRUE) { // Think & Eat Psim (fork[i], fork [(i+1) mod 5]); eat(); Vsim (fork[i], fork [(i+1) mod 5]); } semaphore fork[5] = (1,1,1,1,1); fork(philosopher, 1, 0); fork(philosopher, 1, 1); fork(philosopher, 1, 2); fork(philosopher, 1, 3); fork(philosopher, 1, 4);

© 2004, D. J. Foreman 8 Mutex solutions-1  First attempt: int turn = 0; // initial value for turn (the semaphore) /* process 0 *//* processes 1 */ {while(turn != 0) no-op;{while(turn != 1) no-op; /* critical section*/ /* critical section*/ turn = 1; turn = 0; }} Strict alternation between two processes via use of shared variable “turn” Mutual exclusion achieved Two problems:  Performance determined by least active (slowest) process  If one process crashes outside of the critical section, the other will wait forever for the CS.

© 2004, D. J. Foreman 9 Mutex solutions-2  Second attempt: /* process 0 *//* processes 1 */ {flag[0] = true;{flag[1] = true; while(flag[1]) no-op;while(flag[0]) no-op; /* critical section*/ /* critical section*/ flag[0] = false;flag[1] = false; }} Crash outside CS will not indefinitely block other process  Mutual exclusion achieved Q: Is Deadlock caused by race to set flag[i]?

© 2004, D. J. Foreman 10 Correct (s/w) solution  See Dekker’s Algorithm: ■ Combines using the “turn” variable and flag[] variable. ■ Avoids mutual courtesy ■ “turn” guarantees mutual exclusion. ■ “flag[]” breaks strict alternation problem – if your flag is set to 1 and other flag is 0, then enter no matter what turn is. ■ In the event that a race causes flag[0] = flag[1] = 1, then there is no deadlock to get to the CS because turn will allow one of the processes to enter. ■ flag[i] is an indication of “intent” to enter.

© 2004, D. J. Foreman 11 Monitors  Look like C++ "classes" ■ encapsulation ■ private mutex (semaphore) variable ■ public interfaces  public interfaces use P, V for protection ■ acts like a "critical section"

© 2004, D. J. Foreman 12 Condition Variables (cv's)  Private to monitor  Access via: ■ wait() – suspends process ■ signal() – lets ONE process resume ■ queue() – TRUE if #waiters on cv > 0  2 approaches ■ Hoare – p1 waiting, p0 signals, p1 starts now ■ Mesa (Hansen) – p1 waiting, p0 signals and continues to run, p1 re-checks when p0 ends fewer context switches

© 2004, D. J. Foreman 13 Comparison  Hoare semantics if (R is held) R.wait(); //proceed  Mesa semantics while (R held) R.wait(); // forces re-try //proceed

© 2004, D. J. Foreman 14 Mesa vs. Hoare  Mesa: signaler keeps lock, waking thread must wait on acquire  Hoare: signaler releases lock, waking thread acquires and runs

© 2004, D. J. Foreman 15 IPC  Pipes ■ anonymous limited to parent-child due to file reference child inherits pipe-end as an open file ■ named pipe is opened with a name names are system-wide managed like files  Message passing ■ send() & receive() ■ synchronous & asynchronous

© 2004, D. J. Foreman 16 Notes on Java  The JVM ■ uses monitors for mutual exclusion ■ provides wait and notify for cooperation

© 2004, D. J. Foreman 17 Condition Variables vs Semaphores CV'sSemaphores Only in monitorsAnywhere BUT monitors Wait always blocksWait blocks if count>0 Signal releases a blocked thread or does nothing Signal releases a blocked thread or increments count Either caller or released thread continues (Hoare vs Mesa) Both continue