1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.

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

Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
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.
Mutual Exclusion By Shiran Mizrahi. Critical Section class Counter { private int value = 1; //counter starts at one public Counter(int c) { //constructor.
1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2007 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld.
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.
Multiprocessor Synchronization Algorithms ( ) Lecturer: Danny Hendler The Mutual Exclusion problem.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Operating Systems, 122 Practical Session 5, Synchronization 1.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
1 Chapter 3 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
Multiprocess Synchronization Algorithms ( )
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
1 Operating Systems, 112 Practical Session 5, Synchronization 1.
Enforcing Mutual Exclusion Message Passing. Peterson’s Algorithm for Processes P0 and P1 void P0() { while( true ) { flag[ 0 ] = false; /* remainder */
1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation.
1 Tuesday, June 20, 2006 "The box said that I needed to have Windows 98 or better... so I installed Linux." - LinuxNewbie.org.
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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Chapter 2.3 : Interprocess Communication
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.
The Critical Section Problem
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
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.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
3.1. Concurrency, Critical Sections, Semaphores
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Mutual Exclusion Using Atomic Registers Lecturer: Netanel Dahan Instructor: Prof. Yehuda Afek B.Sc. Seminar on Distributed Computation Tel-Aviv University.
1 Chapter 2 Synchronization Algorithms and Concurrent Programming Gadi Taubenfeld © 2014 Synchronization Algorithms and Concurrent Programming Synchronization.
Process Synchronization Tanenbaum Ch 2.3, 2.5 Silberschatz Ch 6.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally INTERPROCESS COMMUNICATION AND SYNCHRONIZATION SYNCHRONIZATION.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Gokul Kishan CS8 1 Inter-Process Communication (IPC)
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Chapter 5: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 5: Process Synchronization
Module 7a: Classic Synchronization
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Multiprocessor Synchronization Algorithms ( )
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Chapter 6: Synchronization Tools
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Course Syllabus 1. Introduction - History; Views; Concepts; Structure
Process/Thread Synchronization (Part 2)
Syllabus 1. Introduction - History; Views; Concepts; Structure
Presentation transcript:

1 Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation of Processes 3. Process Scheduling – Paradigms; Unix; Modeling 4. Inter-Process Communication (IPC) 4. Process Synchronization - Synchronization primitives and their equivalence; Deadlocks 5. Memory Management - Virtual memory; Page replacement algorithms; Segmentation 6. File Systems - Implementation; Directory and space management; Unix file system; Distributed file systems (NFS) 7. Security – General policies and mechanisms; protection models; authentication 8. Distributed issues – Synchronization; Mutual exclusion Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama

Inter-Process Communication (IPC)  File: A record stored on a file that can be accessed by name the communicating processes.  Signal: A short system message sent from one process to another, not usually used to store information but instead give commands.  Message Exchange: An anonymous data stream that stores and retrieves information in packets.  Shared memory: Memory region shared by the communicating processes, which are allowed to read and change the content of the shared memory. Processes need to communicate to exchange data and/or synchronize their work. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 2

Inter-Process Communication (IPC)  Message Exchange Mechanism enables processes to communicate without the need to share variables.  It should support at least two primitives:  send(destination, message)  receive(source, message) Process AProcess B Message Passing Module Message Passing Module Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 3

Message Format  Header  Message Type  Source & Destination ID  Message Properties  Control Information  Body  The message data Header Body Message Content Message consists of Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 4

Messages and Pipes Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 5

Message Exchange Mechanism  A general method used for IPC:  Inside the same system/computer  In a networked/distributed system.  Message passing could be  Blocking(synchronous):  The sender blocks until the message is received  The receiver blocks until a message is available  Non-Blocking(asynchronous):  The sender sends the message and continue  The receiver receives a valid message or null Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 6

Message Passing Synchronization  The sender  Sends message to multiple destination  Usually expects acknowledgement  The Receiver  Need the received information to proceed  It could be blocked indefinitely if the sender failed or the message was not received. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 7

Send-Received Synchronization The three practical combination:  Blocking send, blocking receive  Non-blocking send, Non-blocking receive  Non-blocking send, Blocking receive Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 8

Queue of messages  Order  Usually FIFO  Capacity  Zero: Sender must wait for receiver  Bounder (predefined length): send wait if the queue is full  Unbounded: sender never waits Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 9

Direct verse Indirect  Direct  Processes identifier is used for source and destination.  Indirect  Messages are sent to shared mailbox  The receiver picked up the messages from the mailbox  The message are queued within the mailbox Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 10

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 11 Mutual exclusion: motivation Race Conditions: Example: Spooler directory with slots; index variables; two processes attempt concurrent access. In points to the next empty slot, Out points to entry holding name of file to print next.... abc.docf.pspaper.ps Process A Process B 4567 Out = 4 In=7

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 12 Code using reads/writes only Add_file_to_print_spool (char *name) 1.Local_in := In 2.StoreName(Spool_dir[local_in], name) 3.In := (local_in+1) mod DIR_LEN A scenario proving the above code wrong Process A performs line 1 Process A is preempted by Process B Process B performs Add_file_to_print_spool to completion Process A is re-scheduled, completes lines 2-3. Process B's file is never printed.

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 13 The mutual exclusion problem (Dijkstra, 1965) We need to devise a protocol that guarantees mutually exclusive access by processes to a shared resource (such as a file, printer, etc.) or, more generally, a critical section of code How can we avoid such race conditions?

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 14 The problem model  Multiple processes  Processes can apply read, write, or stronger operations to shared memory  Completely asynchronous  We assume processes do not fail-stop

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 15 Mutual exclusion: formal definition loop forever Remainder code Entry code Critical section (CS) Exit code end loop Remainder code Entry code Exit code CS

Slide taken from a presentation by Gadi Taubenfeld, IDC An incorrect naïve solution ( means “waiting for this fork”) 16 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama

 The solution o A philosopher first gets o only then it tries to take the 2 forks. Dining philosophers: textbook solution Slide taken from a presentation by Gadi Taubenfeld, IDC 17

Dining philosophers: textbook solution code #define N 5 #defineTHINKING0 #defineHUNGRY1 #defineEATING2 intstate[N]; semaphore mutex = 1; semaphore s[N]; // per each philosopher int left(int i) { return (i-1) % N;} int right(in it) { return (i+1) % N;} void philosopher(int i) { while (TRUE) { think(); pick_sticks(i); eat(); put_sticks(i); } Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 18

Textbook solution code: starvation is possible Eat Block Starvation! Slide taken from a presentation by Gadi Taubenfeld, IDC 19 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama

Textbook solution disadvantages  An inefficient solution o reduces to mutual exclusion o not enough concurrency o Starvation possible Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 20

The LR Solution  If the philosopher acquires one fork and the other fork is not immediately available, she holds the acquired fork until the other fork is free.  Two types of philosophers: o L -- The philosopher first obtains its left fork and then its right fork. o R -- The philosopher first obtains its right fork and then its left fork.  The LR solution: the philosophers are assigned acquisition strategies as follows: philosopher i is R-type if i is even, L-type if i is odd. R L R L R L Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 21 Slide taken from a presentation by Gadi Taubenfeld, IDC

Theorem: The LR solution is starvation-free Assumption: “the fork is fair”. ( means “first fork taken”) R L L R R L Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 22 Slide taken from a presentation by Gadi Taubenfeld, IDC

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 23 Mutex Requirements  Mutual exclusion: No two processes are at the critical section (CS) at the same time  Deadlock-freedom: If processes are trying to enter their critical section, then some process eventually enters the critical section  Starvation-freedom: If a process is trying to enter its critical section, then this process must eventually enter its critical section

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 24 Mutual exclusion using critical regions

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 25 Brute force solution: disabling interrupts Disable interrupts Do your stuff Enable interrupts Problems  User processes are not allowed to disable interrupts  Disabling interrupts must be done for a very short period of time  Does not solve the problem in a multi-processor system

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 26 2-process Mutual Exclusion with a lock variable Program for both processes 1.await lock=0 2.lock:=1 3.CS 4.lock:=0 initially: lock=0 Does the algorithm satisfy mutex? Does it satisfy deadlock-freedom? Does it satisfy starvation-freedom? No Yes No

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 27 Mutual Exclusion: strict alternation Does the algorithm satisfy mutex? Does it satisfy deadlock-freedom? Does it satisfy starvation-freedom? Yes No Program for process 0 1.await turn=0 2.CS 3.turn:=1 Program for process 1 1.await turn=1 2.CS 3.turn:=0 initially: turn=0

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 28 Mutual Exclusion: flag array Does the algorithm satisfy mutex? Does it satisfy deadlock-freedom? Does it satisfy starvation-freedom? Yes No Program for process 0 1.flags[0]:=true 2.await flags[1]=false 3.CS 4.flags[0]:=false Program for process 1 1.flags[1]:=true 2.await flags[0]=false 3.CS 4.flags[1]:=false bool flags[2] initially {false, false}

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 29 Peterson’s 2-process algorithm (Peterson, 1981) initially: b[0]:=false, b[1]:=false, value of turn immaterial Program for process 0 1.b[0]:=true 2.turn:=0 3.await (b[1]=false or turn=1) 4.CS 5.b[0]:=false Program for process 1 1.b[1]:=true 2.turn:=1 3.await (b[0]=false or turn=0) 4.CS 5.b[1]:=false

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 30 Schema for Peterson’s 2-process algorithm Indicate participation b[i]:=true Barrier turn:=i Is there contention? b[1-i]=true? yes no Critical Section Exit code b[i]:=false First to cross barrier? turn=1-i? no, maybe yes Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 31 Peterson’s 2-process algorithm satisfies both mutual-exclusion and starvation-freedom

Observations  Shared register : turn (2-valued) read & write by both processes  Two boolean registers : b[0], b[1] process 0 can write b[0], process 1 can write b[1] both can read b[0] & b[1] Can the algorithm be modified to use only single-writer registers ? 32 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama

33 Kessels’ 2-process algorithm (1982) encode turn=0: as turn[0] = turn[1] turn=1: as turn[0] ≠ turn[1] initially: b[0]:=false, b[1]:=false, value of turn[i] immaterial Program for process 0 1.b[0]:=true 2.local[0]:=turn[1] 3.turn[0]:=local[0] 4.await (b[1]=false or local[0] ≠ turn[1]) 5.CS 6.b[0]:=false Program for process 1 1.b[1]:=true 2.local[1]:=1 – turn[0] 3.turn[1]:=local[1] 4.await (b[0]=false or local[1] = turn[0]) 5.CS 6.b[1]:=false Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama

34 Mutual exclusion for n processes: A tournament tree Level 0 Level 1 Level 2 Processes A tree-node is identified by: [level, node#] Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 35 N-process mutual exclusion: a tournament tree Program for process i 1.node:=i 2.For level = 0 to log n-1 do 3. id:=node mod 2 4. node:=  node/2  5. b[level,2node+id]:=true 6. turn[level,node]:=id 7. await (b[level,2node+1-id]=false or turn[level,node]=1-id) 8.od 9.CS 10.for level=log n –1 downto 0 do 11. node:=  i/2 level  12. b[level,node]:=false 13.od Variables Per node: b[level, 2node], b[level, 2node+1], turn[level,node] Per process (local): level, node, id.

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 36 Fairness: First in First Out (FIFO) entry code exit code critical section remainder  Mutual Exclusion  Deadlock-freedom  Starvation-freedom doorway waiting FIFO: if process p is waiting and process q has not yet started the doorway, then q will not enter the CS before p Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 37 time Lamport’s Bakery Algorithm doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 38 Implementation 1 code of process i, i  {1,..., n} number[i] := 1 + max {number[j] | (1  j  n)} for j := 1 to n (<> i) { await (number[j] = 0)  (number[j] > number[i]) } critical section number[i] := n numberinteger Answer: No, it can deadlock! Does this implementation work? Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 39 time Implementation 1: deadlock doorway 12345n CS exit waiting entry remainder deadlock Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 40 number[i] := 1 + max {number[j] | (1  j  n)} for j := 1 to n (<> i) { await (number[j] = 0)  (number[j],j) < number[i],i) // lexicographical order } critical section number[i] := n numberinteger Answer: It does not satisfy mutual exclusion! Implementation 2 code of process i, i  {1,..., n} Does this implementation work? Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 41 time Implementation 2: no mutual exclusion doorway 12345n CS exit waiting entry remainder Synchronization Algorithms and Concurrent Programming, Gadi Taubenfeld © 2006

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 42 The Bakery Algorithm code of process i, i  {1,..., n} 1: choosing[i] := true 2: number[i] := 1 + max {number[j] | (1  j  n)} 3: choosing[i] := false 4: for j := 1 to n do 5: await choosing[j] = false 6: await (number[j] = 0)  (number[j],j)  (number[i],i) 7: od 8: critical section 9: number[i] := n choosingbits false numberinteger false Doorway Waiting Bakery

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 43 Lamport’s bakery algorithm satisfies mutual-exclusion, starvation-freedom, and FIFO

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 44 Test-and-set(w) do atomically prev:=w w:=1 return prev The test-and-set instruction Program for process i 1.await test&set(v) = 0 2.Critical Section 3.v:=0 initially: v:=0 Mutual exclusion? Yes Deadlock-freedom? No Starvation-freedom? Yes

Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels & Jihad El-Sama 45 Starvation-free mutex using test-and-set program for process i 1.interested[i]:=true 2.await ( (test-and-set(lock) = 0) || (interested[i]=false) ) 3.CS 4.interested[i]:=false 5.j:=(i+1) % n 6.while (j != i && !interested[j]) j:=++j % n 7.if (j=i) 8. lock:=0 9.else 10. interested[j]:=false boolean lock initially 0, interested[n] initially false