CIS 720 Mutual Exclusion 2.

Slides:



Advertisements
Similar presentations
Module 6: Process Synchronization
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Mutual Exclusion – SW & HW By Oded Regev. Outline: Short review on the Bakery algorithm Short review on the Bakery algorithm Black & White Algorithm Black.
Ch 7 B.
1 Synchronization 2: semaphores and more… 1 Operating Systems, 2011, Danny Hendler & Amnon Meisels.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
CIS 720 Mutual Exclusion. Critical Section problem Process i do (true) entry protocol; critical section; exit protocol; non-critical section od.
© 2004, D. J. Foreman 1 The Dining Philosophers Problem.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
Process Synchronization
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
© 2004, D. J. Foreman 1 High Level Synchronization and Inter-Process Communication.
28/10/1999POS-A1 The Synchronization Problem Synchronization problems occur because –multiple processes or threads want to share data; –the executions.
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.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
1 Semaphores n Semaphores  Issues addressed:  1. Problems with busy-waiting  2. Definition of a semaphore: init(s), P(s) and V(s) - invariant  3. Examples.
CIS 720 Lecture 5. Techniques to avoid interference Disjoint variables –If the write set of each process is disjoint from the read and write set of other.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Critical Sections with lots of Threads. Refresher: Deker’s Algorithm Assumes two threads, numbered 0 and 1 CSEnter(int i) { int J = i^1; inside[i] = true;
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
PARALLEL PROGRAM CHALLENGES
Background on the need for Synchronization
Process Synchronization
Concurrent Processes.
Semaphores and Condition Variables
Chapter 5: Process Synchronization
Inter-Process Communication and Synchronization
Critical Section and Critical Resources
Critical Section and Critical Resources
Midterm review: closed book multiple choice chapters 1 to 9
Threading And Parallel Programming Constructs
Process Synchronization
Lecture 22 Syed Mansoor Sarwar
CIS 720 Mutual Exclusion 2.
Lecture 2 Part 2 Process Synchronization
Critical section problem
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Chapter 6: Synchronization Tools
CIS 720 Lecture 5.
, Part II Process Synchronization
Process Synchronization
CIS 720 Lecture 6.
CIS 720 Lecture 4.
CIS 720 Lecture 4.
CIS 720 Lecture 5.
CIS 720 Lecture 4.
Process Synchronization
Monitors and Inter-Process Communication
CIS 720 Lecture 4.
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

CIS 720 Mutual Exclusion 2

Tie Breaker Algorithm in1 = false; in2 = false; last = 1 co CS1: CS2: do true  do true  last = 1; in1 = true; last = 2; in2 = true <await (!in2 \/ last == 2)>; <await (!in1 \/ last == 1)>; critical section critical section in1 = false; in2 = false; non-critical section non-critical section od od oc

Barrier synchronization Worker[i]: do true  code for task i wait for all tasks to complete od

Barrier synchronization Worker[i]: do true  code for task i <count = count + 1> < await( count == n) > od

Barrier synchronization Worker[i]: do true  code for task i <count = count + 1> < await( count == n) > od

Barrier synchronization co worker[i]: Coordinator do true  do true  code for task i for (i = 1 to n) arrive[i] = 1 await(arrive[i]= 1); await(continue ==1) continue = 1 od od oc

Barrier synchronization co worker[i]: Coordinator do true  do true  code for task I; for (i = 1 to n) arrive[i] = 1 { await(arrive[i]= 1); await(continue[i]==1) arrive[i] = 0; } continue[i] = 0; for (i = 1 to n) continue[i] = 1 od od oc Flag rule: A process that waits for the synchronization flags should reset it.

Invariant based approach Identify synchronization regions in your program Synchronization region: segment of code or control point at which a thread must wait for another thread or signal another thread.

Mutual exclusion CS1 CS2 do (true) do (true)  ****start of region **** start of region critical section critical section **** end of region **** end of region non-critical section non-critical section od

Mutual exclusion CS1 CS2 Associate in and out counter with each region do (true) do (true)  ****start of region **** start of region in1 = in1 + 1 in2 = in2 + 1 critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region

Mutual exclusion CS1 CS2 Associate in and out counter with each region do (true) do (true)  ****start of region **** start of region in1 = in1 + 1 in2 = in2 + 1 critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region Write an invariant using in and out counters I = (in1=out1) \/ (in2=out2)

Mutual exclusion CS1 CS2 do (true) do (true)  ****start of region **** start of region <await(in2=out2) in1 = in1 + 1> <await(in1=out1) in2 = in2 + 1> critical section critical section **** end of region **** end of region out1 = out1+1; out2 = out2+1; non-critical section non-critical section od Associate in and out counter with each region Write an invariant using in and out counters I = (in1=out1) \/ (in2=out2) Derive guards for the increment assertions

Barrier Synchronization P1 P2 do (true) do (true)  task for p1 task for p2 ****start of region **** start of region **** end of region **** end of region od

Barrier CS1 CS2 Associate counters with each region do (true) do (true)  task for p1 task for p2 ****start of region **** start of region arrive1 = arrive1 + 1 arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Associate counters with each region

Barrier CS1 CS2 Associate counters with each region do (true) do (true)  task for p1 task for p2 ****start of region **** start of region arrive1 = arrive1 + 1 arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Associate counters with each region Write an invariant using the counters (depart[1] <= arrive[2]) /\ (depart[2] <= arrive[1])

Semaphores Two operations: P(s), V(s) Semaphore invariant: nP = number of P operations invoked so far nV = number of V operations invoked so far init = initial value of s nP <= nV + init

Binary semaphore: 0 <= s <= 1 Let s = nV + init – nP Invariant: s >= 0 P(s): nP = nP + 1 <await s > 0  s = s - 1> V(s): nV = nV + 1 < s = s + 1> Binary semaphore: 0 <= s <= 1

Mutual exclusion CSi do (true) in[i] = 1 critical section in[i] = 0 non-critical section od I = ( 0 <= (in[1] + in[2] + …. + in[n]) <= 1 ) Let mutex = 1 - (in[1] + in[2] + …. + in[n]) I = ( 0 <= mutex <= 1 )

Mutual exclusion CSi do (true) critical section non-critical section od I = ( 0 <= (in[1] + in[2] + …. + in[n]) <= 1 ) Let mutex = 1 - (in[1] + in[2] + …. + in[n]) I = ( 0 <= mutex <= 1 )

Barrier CS1 CS2 do (true) do (true)  task for p1 task for p2 ****start of region **** start of region arrive1 = arrive1 + 1 arrive2 = arrive2 + 1 **** end of region **** end of region depart1 = depart1+1; depart2 = depart2+1; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)

Barrier CS1 CS2 Barrier1 = (arrive1 – depart2) do (true) do (true)  task for p1 task for p2 barrier1++ barrier2++ barrier2--; barrier1--; od Barrier1 = (arrive1 – depart2) Barrier2 = (arrive2 – depart1)