CY2003 Computer Systems Lecture 04 Interprocess 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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Operating Systems Part III: Process Management (Process Synchronization)
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
G53OPS Operating Systems
Chapter 2 Processes and Threads
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Operating System Concepts and Techniques Lecture 12 Interprocess communication-1 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
Mutual Exclusion.
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
Chapter 3 The Critical Section Problem
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Avishai Wool lecture Priority Scheduling Idea: Jobs are assigned priorities. Always, the job with the highest priority runs. Note: All scheduling.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Concurrency 1 CS502 Spring 2006 Thought experiment static int y = 0; int main(int argc, char **argv) { extern int y; y = y + 1; return 0; }
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
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.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
The Critical Section Problem
Concurrency, Mutual Exclusion and Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION OPERATING SYSTEMS.
Games Development 2 Concurrent Programming CO3301 Week 9.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Synchronicity Introduction to Operating Systems: Module 5.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Background on the need for Synchronization
G.Anuradha Reference: William Stallings
Module 7a: Classic Synchronization
Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Presentation transcript:

CY2003 Computer Systems Lecture 04 Interprocess Communication

© JMU, 2004CY2003-Week 042 Overview Interprocess synchronisation –race conditions –critical sections –deadlocks Mutual exclusion with busy waiting –Disabling interrupts –Lock variables –Strict alternation –Peterson’s algorithm

Interprocess Synchronisation

© JMU, 2004CY2003-Week 044 Basic Principles In the earlier discussion of processes, we have glossed over the significance and ramifications of the co-existence of processes within a system Several possible relationships between processes –fully independent processes users working on separate applications within one system –students compiling and running C programs in lab sessions –independent but related processes users contributing to one application –data entry clerks adding orders to sales control system –concurrent programming applications applications constructed as a set of co-operating processes

© JMU, 2004CY2003-Week 045 Interprocess Communication In a real system processes need to communicate with each other in order to co-operate There are several reasons for co-operating –information sharing the processes are interested in the same information –computational speedup a particular task may run faster if it can be broken down into multiple subtasks which run in parallel –convenience an individual user may choose to work on many tasks at once

© JMU, 2004CY2003-Week 046 Synchronisation Problems As soon as we allow multiple processes to share resources and communicate, there are a number of potential process synchronisation problems that may occur –race conditions two or more processes competing for a shared resource the result varies according to the order of process execution –deadlocks a situation where two or more processes are each waiting for resources held by others –starvation a process never gets access to a required resource

© JMU, 2004CY2003-Week 047 A Race fred.doc bill.doc jack.doc out = 4in = 7 process A process B process A in = 7 process A process B in = 7 B.doc in = 8 process A process B A.doc in = 8 The operating system is now in a consistent state, but B.doc will never get printed This is called a race condition - the result will depend on a race between the two processes

© JMU, 2004CY2003-Week 048 Critical Sections How do we avoid race conditions? –the problem is caused by two processes have free (simultaneous) access to a shared resource the data item ‘in’ in the example just seen We need to find some way to prohibit more than one process from accessing such a resource –problems only occur during shared resource access The part of the process which accesses a shared resource is termed a critical section –only one process must be allowed to be in a critical section at one time: this is mutual exclusion

© JMU, 2004CY2003-Week 049 Example of Critical Sections critical region Process A critical region Process B 1. Process A starts to run Process A 2. Neither process is in a critical section, so A is allowed to enter its 3. Process B starts to run Process AProcess B 4. Process B reaches its critical section, but is not allowed to enter - the process blocks 5. Process A is reactivated and leaves critical section Process BProcess A 6. Process B is reactivated and is now allowed to enter its critical section Process AProcess B

© JMU, 2004CY2003-Week 0410 Flawed Attempt at Mutual Exclusion Let’s try an ‘obvious’ solution to achieve mutual exclusion, by setting a ‘ critical_region ’ flag while ( critical_region == BUSY ) do_nothing; critical_region= BUSY; critical_region= FREE; Unfortunately, this doesn’t solve the problem! –suppose process A is interrupted just after it has checked that the critical_region flag is FREE, but before it has run the next line to set it to BUSY –process B then runs and checks the critical_region flag it finds it FREE, and it too enters the critical code region

© JMU, 2004CY2003-Week 0411 Requirements for Solution The actual requirements needed for a correct and efficient solution to the mutual exclusion problem are far from obvious –no two processes may be in their critical section at the same time –no assumptions may be made about speeds or the number of processors –no process running outside its critical section may block other processes –no process should have to wait forever to enter its critical section

© JMU, 2004CY2003-Week 0412 Deadlocks Even if the mutual exclusion problem is solved, there is another related problem: deadlocks In many cases a process needs exclusive access to more than one resource –suppose there are two processes, A and B, and two shared resources, printer and tape A allocates the printer B allocates the tape –then A requests the tape and blocks until B releases it –and then B requests the printer and blocks until A releases it

Mutual exclusion with busy waiting

© JMU, 2004CY2003-Week 0414 Disabling Interrupts –Allow a process to disable interrupts before it enters its critical section and then enable interrupts after it leaves its critical section –CPU will be unable to switch processes –Guarantees that the process can use the shared variable without another process accessing it –But, disabling interrupts, is a major undertaking –At best, the computer will not be able to service interrupts for, maybe, a long time –At worst, the process may never enable interrupts, thus (effectively) crashing the computer –The disadvantages far outweigh the advantages

© JMU, 2004CY2003-Week 0415 Lock variable Another method is to assign a lock variable -For example, set the variable to (say) 1 when a process is in its critical section and reset to zero when a processes exits its critical section But this is flawed as it simply moves the problem from the shared variable to the lock variable

© JMU, 2004CY2003-Week 0416 Strict alternation An integer variable turn keeps track of whose turn to enter the critical region –If this variable is zero, then process 0 enters the critical region –process 1 waits for its turn and Continually tests the variable. While (TRUE) { while (turn !=n) /*wait */; critical_region(); turn = n+1; noncritical_region(); }

© JMU, 2004CY2003-Week 0417 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_section(); turn = 0; noncritical_section(); }

© JMU, 2004CY2003-Week 0418 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_section(); turn = 0; noncritical_section(); } Assume the variable turn is initially set to zero. Process 0 runs. And finding turn is zero, it enters its critical region If process 1 tries to run, it will find that turn is zero and will have to wait When process 0 exits its critical region turn = 1 and process 1 can continue Process 0 is now blocked

© JMU, 2004CY2003-Week 0419 Strict alternation Process 0 While (TRUE) { while (turn != 0); // wait critical_section(); turn = 1; noncritical_section(); } Process 1 While (TRUE) { while (turn != 1); // wait critical_section(); turn = 0; noncritical_section(); } –Process 0 runs, enters its critical section and exits; setting turn to 1. Process 0 is now in its non-critical section. Assume this non-critical procedure takes a long time. –Process 1, which is a much faster process, now runs and once it has left its critical section turn is set to zero. –Process 1 executes its non-critical section very quickly and returns to the top of the procedure. –The situation is now that process 0 is in its non-critical section and process 1 is waiting for turn to be set to zero. In fact, there is no reason why process 1 cannot enter its critical region as process 0 is not in its critical region.

© JMU, 2004CY2003-Week 0420 Peterson’s algorithm Any process that is interested in entering the critical region, sets its interested value to True and sets the turn variable to the process number. –The process calls for the enter_the_region function. –When leaving the critical region, the process calls the leave_the_region function and sets interested to False.

© JMU, 2004CY2003-Week 0421 Peterson’s algorithm int turn; int interested [N]; void enter_the_region(int process) { int other; other = 1 – process; interested [process] = True; turn = process; while (turn == process && interested [other] = = True); } void leave_the_region (int process) { interested [process] = False; }

© JMU, 2004CY2003-Week 0422 Summary Interprocess synchronisation –race conditions –critical sections –deadlocks Mutual exclusion with busy waiting –Disabling interrupts –Lock variable –Strict variable –Peterson’s algorithm