1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.

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)
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.
Chapter 6: Process Synchronization
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Lecture 5: Concurrency: Mutual Exclusion and Synchronization.
Chapter 3 The Critical Section Problem
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Dining-Philosophers Problem Shared data fork[5]: semaphore; initialized to 1.
Classical Problems of Concurrency
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
Process Synchronization
Chapter 2.3 : Interprocess Communication
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Synchronization Solutions
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.
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,
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
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.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 6 Semaphores.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
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.
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 6: Process Synchronization.
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.
Interprocess Communication Race Conditions
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Synchronization Tools
Presentation transcript:

1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess communication Deadlocks Deadlocks Threads Threads

2 Producer - Consumer Problem Buffer is shared (ie., it is a shared variable) Buffer is shared (ie., it is a shared variable) Producer Process Consumer Process Produce Put in bufferConsume Get from buffer BUFFER

3 Progress in time….. Both processes are started at the same time and consumer uses some old value initially Both processes are started at the same time and consumer uses some old value initially 3 instead of 2! Producer Consumer 12c2 p1p4p3p24321 t Buffer c1

4 A Race Condition Because of the timing and which process starts first Because of the timing and which process starts first There is a chance that different executions may end up with different results There is a chance that different executions may end up with different results

5 Critical Sections Critical Section Critical Section A section of code in which the process accesses and modifies shared variables A section of code in which the process accesses and modifies shared variables Mutual Exclusion Mutual Exclusion A method of preventing for ensuring that one (or a specified number) of processes are in a critical section A method of preventing for ensuring that one (or a specified number) of processes are in a critical section

6 Why Processes Need to Communicate? To synchronize their executions To synchronize their executions To exchange data and information To exchange data and information

7 Rules to Form Critical Sections 1. No two processes may be simultaneously inside their CS (mutual exclusion) 2.No assumptions are made about relative process speeds or number of CPUs 3.A process outside a CS should not block other processes 4.No process should wait forever before entering its CS

8 Mutual Exclusion Problem : Starvation Also known as Indefinite Postponement Also known as Indefinite Postponement Definition Definition Indefinitely delaying the scheduling of a process in favour of other processes Indefinitely delaying the scheduling of a process in favour of other processes Cause Cause Usually a bias in a systems scheduling policies (a bad scheduling algorithm) Usually a bias in a systems scheduling policies (a bad scheduling algorithm) Solution Solution Implement some form of aging Implement some form of aging

9 Another Problem : Deadlocks Two (or more) processes are blocked waiting for an event that will never occur Two (or more) processes are blocked waiting for an event that will never occur Generally, A waits for B to do something and B is waiting for A Generally, A waits for B to do something and B is waiting for A Both are not doing anything so both events never occur Both are not doing anything so both events never occur

10 How to Implement Mutual Exclusion Three possibilities Three possibilities Application: programmer builds some method into the program Application: programmer builds some method into the program Hardware:special h/w instructions provided to implement ME Hardware:special h/w instructions provided to implement ME OS:provides some services that can be used by the programmer OS:provides some services that can be used by the programmer All schemes rely on some code for All schemes rely on some code for enter_critical_section, and enter_critical_section, and exit_critical_section exit_critical_section These "functions" enclose the critical section These "functions" enclose the critical section

11 Application Mutual Exclusion Application Mutual Exclusion is Application Mutual Exclusion is implemented by the programmer implemented by the programmer hard to get correct, and hard to get correct, and very inefficient very inefficient All rely on some form of busy waiting (process tests a condition, say a flag, and loops while the condition remains the same) All rely on some form of busy waiting (process tests a condition, say a flag, and loops while the condition remains the same)

12 Example Producer Producerproduce If lock = 1 loop until lock = 0 lock=1 put in buffer lock=0 Consumer Consumer If lock = 1 loop until lock = 0 lock=1 get from buffer lock=0Consume Note: initially lock = 0

13 Dekker’s Algorithm Program Dekker; Var turn: integer; wantp, wantq:boolean; Procedure p; Beginrepeat wantp := true; while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; CS; turn := 2; wantp:= false; NCS; until false; until false;End; Procedure q; Beginrepeat wantq2 := true; while wantp do if turn = 1 then begin wantq:= false; repeat until turn = 2; wantq:=false end; CS; turn := 1; wantq := false; NCS; until false; until false;End; Begin (* main program *) turn:= 1; wantp:= false; wantq:= false; cobegin p; q coend End.

14 Explanation of the Algorithm Procedure p; Beginrepeat wantp := true;(* 1 *) wantp := true;(* 1 *) while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; (* 2 *) while wantq do if turn = 2 then begin wantp:= false; repeat until turn = 1; wantp:=true end; (* 2 *) CS; CS; turn := 2;(* 3 *) turn := 2;(* 3 *) wantp := false;(* 4 *) wantp := false;(* 4 *) NCS; NCS; until false; until false;End; 1. Process makes a request to enter CS 2. If the other process had made a request to enter CS before and if it is its turn then Take back the request to enter CS by setting the “want” variable to false” Take back the request to enter CS by setting the “want” variable to false” Wait for the other process to exit CS and change the turn variable Wait for the other process to exit CS and change the turn variable Make a new request to enter CS and then enter CS Make a new request to enter CS and then enter CS 3. Flip the turn variable so that now the other process can enter CS 4. Set “want” variable to false to indicate that the process is now out of CS

15 Comments Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn Explicit control of transfer by a turn variable. Hence; turn = 1 means P’s turn, turn = 2 Q’s turn If a process is blocked, the other process can still go If a process is blocked, the other process can still go Dekker’s algorithm is correct (prove it!) Dekker’s algorithm is correct (prove it!) It satisfies the mutual exclusion property It satisfies the mutual exclusion property It is free from deadlock and starvation It is free from deadlock and starvation

16 Hardware ME : Test and Set Instruction Perform an indivisible x:=r and r:=1 Perform an indivisible x:=r and r:=1 x is a local variable x is a local variable r is a global register set to 0 initially r is a global register set to 0 initially repeat (test&set(x)) until x = 0; repeat (test&set(x)) until x = 0; r:= 0; r:= 0;

17 Hardware ME : Exchange Instruction Exchange: swap the values of x and r Exchange: swap the values of x and r x is a local variable x is a local variable r is a global register set to 1 initially r is a global register set to 1 initially x:= 0; x:= 0; repeat exchange(r, x) until x = 1; repeat exchange(r, x) until x = 1; exchange(r, x); exchange(r, x); Note: r:= 0 and x:= 1 when the process is in CS

18 Another Hardware ME : Disabling Interrupts On a single CPU only one process is executed On a single CPU only one process is executed Concurrency is achieved by interleaving execution (usually done using interrupts) Concurrency is achieved by interleaving execution (usually done using interrupts) If you disable interrupts then you can be sure only one process will ever execute If you disable interrupts then you can be sure only one process will ever execute One process can lock a system or degrade performance greatly One process can lock a system or degrade performance greatly

19 Hardware ME Characteristics Advantages Advantages can be used by a single or multiple processes (with shared memory) can be used by a single or multiple processes (with shared memory) simple and therefore easy to verify simple and therefore easy to verify can support multiple critical sections can support multiple critical sections Disadvantages Disadvantages is used (very important) busy waiting is used (very important) starvation is possible starvation is possible deadlock is possible (especially with priorities) deadlock is possible (especially with priorities)

20 Mutual Exclusion Through OS Semaphores Semaphores Message passing Message passing

21 Semaphores Major advance incorporated into many modern operating systems (Unix, OS/2) Major advance incorporated into many modern operating systems (Unix, OS/2) A semaphore is A semaphore is a non-negative integer a non-negative integer that has two indivisible, valid operations that has two indivisible, valid operations

22 Semaphore Operations Wait(s) Wait(s) If s > 0 then s:= s - 1 else block this process Signal(s) Signal(s) If there is a blocked process on this semaphore then wake it up else s:= s + 1

23 More on Semaphores The other valid operation is initialisation The other valid operation is initialisation Two types of semaphores Two types of semaphores binary semaphores can only be 0 or 1 binary semaphores can only be 0 or 1 counting semaphores can be any non-negative integer counting semaphores can be any non-negative integer Semaphores are an OS service implemented using one of the methods shown already Semaphores are an OS service implemented using one of the methods shown already usually by disabling interrupts for a very short time usually by disabling interrupts for a very short time

24 Producer - Consumer Problem: Solution by Semaphores Initially semaphore mutex is 1 Initially semaphore mutex is 1 Produce Wait(mutex) Put in buffer Signal(mutex) Wait(mutex) Get from buffer Signal(mutex) Consume CS

25 Another Example Three processes all share a resource on which Three processes all share a resource on which one draws an A one draws an A one draws a B one draws a B one draws a C one draws a C Implement a form of synchronization so that the output appears ABC Implement a form of synchronization so that the output appears ABC think(); draw_A(); think(); draw_B(); think(); draw_C(); Process AProcess BProcess C

26 Semaphore b = 0, c = 0; Semaphore b = 0, c = 0; think(); draw_A(); signal(b); wait(b); think(); draw_B(); signal(c); wait(c); think(); draw_C(); Process AProcess BProcess C

27 Dining Philosophers 5 seating places, 5 plates of spagetti and 5 forks 5 seating places, 5 plates of spagetti and 5 forks A philosophers life cycle A philosophers life cycle Repeat think; eat forever Eating can only be done with 2 forks Eating can only be done with 2 forks Devise a ritual (protocol) that will allow the philosophers to eat. The protocol should satisfy mutual exclusion (no two philosophers try to use the same fork simultaneously), free from deadlock and absense of starvation Devise a ritual (protocol) that will allow the philosophers to eat. The protocol should satisfy mutual exclusion (no two philosophers try to use the same fork simultaneously), free from deadlock and absense of starvation Figure is from Modern OS by Tanenbaum

28 Dining Philosophers Solution – First Attempt Program diningphilosophers; Var i: integer; fork : array[0..4] of semaphore; fork : array[0..4] of semaphore; Procedure philosopher (i : integer); Beginrepeatthink; wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5];(* get right fork *) eat; signal(fork[i]);(* return left fork *) signal(fork[(i+1) mod 5];(* return right fork *) until false; End; Begin (* main *) for i:= 0 to 4 do fork[i]:= 1; (* initially all forks are available *) cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend;End.

29 Comments on First Attempt Mutual exclusion is implemented by a binary semaphore fork Mutual exclusion is implemented by a binary semaphore fork Deadlock is possible if all 5 philosophers take the left forks simultaneously then all would wait for the right fork Deadlock is possible if all 5 philosophers take the left forks simultaneously then all would wait for the right fork How to handle the deadlock and ensure liveliness? How to handle the deadlock and ensure liveliness? Let at the most 4 philosophers to sit and eat. Let at the most 4 philosophers to sit and eat. Two of them can eat, one holds a fork and the other just sits Two of them can eat, one holds a fork and the other just sits One philosopher can eat and the other 3 can hold their left forks One philosopher can eat and the other 3 can hold their left forks

30 Correct Solution Program diningphilosophers; Var fork : array[0..4] of semaphore; i : integer; i : integer; table : semaphore; (* seating limit *) table : semaphore; (* seating limit *) Procedure philosopher (i : integer); Beginrepeatthink;wait(table); wait(fork[i]); (* get left fork *) wait(fork[(i+1) mod 5];(* get right fork *) eat; signal(fork[i]);(* return left fork *) signal(fork[(i+1) mod 5];(* return right fork *) signal(table); until false; End; Begin (* main *) for i:= 0 to 4 do fork[i]:= 1; table:= 4; cobegin philosopher(0); philosopher(1); philosopher(2); philosopher(3); philosopher(4); coend;End.

31 Message Passing Provides synchronization and information exchange Provides synchronization and information exchange Message Operations: Message Operations: send(destination, &message) send(destination, &message) receive (source, &message) receive (source, &message)

32 Producer - Consumer Problem Using Messages #define N 100 /*number of message slots*/ producer( ) {int item; message m; while (TRUE) { while (TRUE) {produce_item(&item);receive(consumer,&m); build_message(&m, item); send(consumer,&m);}}

33 Consumer( ) {int item; message m; for (i=0; i<N; i++) send(producer,&m); for (i=0; i<N; i++) send(producer,&m); while (TRUE) { while (TRUE) {receive(producer,&m);extract_item(&m,&item);send(producer,&m);consume_item(item);}}