CIS 720 Lecture 6.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Producer-Consumer One common situation: Producers and consumers communicate via a buffer.
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
CIS 720 Mutual Exclusion. Critical Section problem Process i do (true) entry protocol; critical section; exit protocol; non-critical section od.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Synchronization Coordinating the Activity of Mostly Independent Entities.
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 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
6/16/2015 Chapter Eight Process Synchronisation. Index Objectives Concurrent processes and Asynchronous concurrent processes Process synchronisation Mutual.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
Language Support for Concurrency. 2 Announcements CS 415 project 1 due today! CS 414 homework 2 available due next week Midterm will be first week in.
IPC and Classical Synchronization Problems
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.
© 2004, D. J. Foreman 1 Mutual Exclusion © 2004, D. J. Foreman 2  Mutual exclusion ■ Critical sections ■ Primitives  Implementing it  Dekker's alg.
Classical Synchronization Problems. Announcements CS 414 grades and solutions available in CMS soon. –Average 74.3 –High of 95. –Score out of 100 pts.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6(b): Synchronization.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Lecture 6: Synchronization (II) – Semaphores and Monitors
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
Synchronizing Threads with Semaphores
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Thread Synchronization including Mutual Exclusion In Java synchronized keyword Monitor or Lock with conditions Semaphore.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
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.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
CSE 451: Operating Systems Spring 2006 Module 8 Semaphores and Monitors John Zahorjan Allen Center 534.
Process Synchronization
Concurrent Processes.
What I Teach (Do what I say, not what I do)
Inter-Process Communication and Synchronization
Critical Section and Critical Resources
The Critical-Section Problem (Two-Process Solution)
Critical Section and Critical Resources
Semaphores and Bounded Buffer
Process Synchronization
Lecture 22 Syed Mansoor Sarwar
CIS 720 Mutual Exclusion 2.
Invariant Based Methodology
CSE 451: Operating Systems Winter Module 8 Semaphores and Monitors
Thread Synchronization including Mutual Exclusion
CSE 451: Operating Systems Autumn Module 8 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
CSE 153 Design of Operating Systems Winter 2019
CIS 720 Lecture 5.
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
, Part II Process Synchronization
Process Synchronization
CIS 720 Lecture 5.
Process Synchronization
Steve’s Concurrency Slides
CIS 720 Mutual Exclusion 2.
Presentation transcript:

CIS 720 Lecture 6

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 in1 = 0; out1 = 0 in2 = 0; out2 = 0 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 od Associate in and out counter with each region Write an invariant using in and out counters I : (in1=out1) \/ (in2=out2) { in2 = 0 /\ out2 = 0 } { in2 = out2 } { in2 = out2 + 1} { I } { in1 = 0 /\ out1 = 0 } { in1 = out1 } { I } { in1 = out1 + 1} { I } { in1 = out1 } { I } { in1 + 1= out1 \/ in2 = out2 } in1 = in1 + 1 { 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 arrive1 = 0; depart1 = 0 arrive2 = 0; depart2 = 0 do (true) do (true)  task for p1 task for p2 <await depart2 < arrive1  <await depart1 < arrive2  arrive1 = arrive1 + 1 > arrive2 = arrive2 + 1 > <await depart1 < arrive2  <await depart2 < arrive1  depart1 = depart1+1; > depart2 = depart2+1 > od od Associate counters with each region Write an invariant using the counters (depart1 <= arrive2 /\ (depart2<= arrive1) { depart1 <= arrive2 /\ depart2 <= arrive1 + 1} arrive1 = arrive1 + 1 { depart1 <= arrive2 /\ depart2 <= arrive1 }

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 nV + init – np >= 0 S = nv + init – np S >= 0

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) in[i] = 1 mutex++ P(mutex) critical section in[i] = 0 mutex-- V(mutex) 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)

Producer/consumer problem do true  do true  produce pdata buf = pdata cdata = buf; consume cdata od od oc

Producer/consumer problem in1 = out1 = in2 = out2 = 0; co Producer: Consumer: do true  {in1=out1} do true  {in2=out2} produce pdata in1 = in1 + 1 in2 = in2 + 1 buf = pdata {in1 = out1+1} cdata = buf {in2 = out2+1} out1 = out1 + 1 out2 = out2 + 1 {in1=out1} consume cdata {in2 = out2} od od oc Invariant: in2 <= out1 /\ in1 <= out2 + 1

Producer/consumer problem do true  do true  produce pdata <await(in1 <=out2)  in1 = in1 + 1> <await(in2 < out1) in2 = in2 + 1> buf = pdata cdata = buf <out1 = out1 + 1> <out2 = out2 + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) Empty = out2 + 1 – in1 Full = out1 – in2

Producer/consumer problem do true  do true  produce pdata <await(empty > 0)  empty= empty -1> <await(full > 0)  full = full -1 > buf = pdata cdata = buf <full = full + 1> <empty = empty + 1> consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2

Producer/consumer problem do true  do true  produce pdata P(empty) P(full) buf = pdata cdata = buf V(full) V(empty) consume cdata od od oc Invariant: (in1 <= out2 + 1) /\ (in2 <= out1) empty = out2 + 1 in1 full = out1 – in2

Producer/consumer problem in1 = out1 = in2 = out2 = 0; co Producer: Consumer1: Consumer2: do true  do true  do true  produce pdata in1 = in1 + 1 in2 = in2 + 1 in3 = in3 + 1 buf = pdata cdata = buf cdata = buf out1 = out1 + 1 out2 = out2 + 1 out3 = out3 + 1 consume cdata consume cdata od od od oc Invariant: