CIS 720 Lecture 2.

Slides:



Advertisements
Similar presentations
Ch 7 B.
Advertisements

CIS 720 Lecture 2. Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
1/25 Concurrency and asynchronous computing How do we deal with evaluation when we have a bunch of processors involved?
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Transaction Processing Lecture ACID 2 phase commit.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
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.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
1 Chapter 5 Concurrency. 2 Concurrency 3 4 Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int *i) { if (*i == 0) { *i.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
CIS 720 Concurrency Control. Locking Atomic statement –Can be used to perform two or more updates atomically Th1: …. ;……. Th2:…………. ;…….
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency, Mutual Exclusion and Synchronization.
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.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Internet Software Development Controlling Threads Paul J Krause.
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.
Synchronizing Threads with Semaphores
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Process Synchronization Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the.
Chapter 15: Transactions Loc Hoang CS 157B. Definition n A transaction is a discrete unit of work that must be completely processed or not processed at.
CIS 720 Correctness of Concurrent Programs. i := 1 max := A[1] do i < n  i = i + 1 if (max < A[i]) max = A[i] od.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Software Systems Verification and Validation Laboratory Assignment 4 Model checking Assignment date: Lab 4 Delivery date: Lab 4, 5.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Process Synchronization
Concurrent Processes.
Classical Synchronization Problems
Chapter 6-7: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Monitors Chapter 7.
CIS 720 Concurrency Control.
ITEC452 Distributed Computing Lecture 5 Program Correctness
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Module 7a: Classic Synchronization
Synchronization Hank Levy 1.
Invariant Based Methodology
Monitors Chapter 7.
Monitors Chapter 7.
Kernel Synchronization II
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CIS 720 Lecture 5.
CIS 720 Lecture 4.
CIS 720 Lecture 4.
CIS 720 Lecture 2.
CIS 720 Lecture 4.
CIS 720 Lecture 4.
CIS 720 Lecture 4.
Presentation transcript:

CIS 720 Lecture 2

Concurrency …shared variable declaration…. …shared variable initialization… co P1 // P2 // P3 // P4 oc Execution of P1 … P4 starts concurrently and the co-oc statement completes when all blocks have completed execution.

Banking System } Deposit(amount, account) { x = db.account; Transaction 1: Deposit $50 in Acc1 Transaction 2: Deposit $70 in Acc1 Possible interleavings T1.x = db.Acc1; T1.x = T1.x + 50 T2.x = db.Acc1; T2.x = T2.x + 70; db.Acc1 = T1.x db.Acc1 = T2.x Deposit(amount, account) { x = db.account; x = x + amount; db.account = x; } Acc1 = 100 T1.x = 150; Acc1 =100 T2.x = 170; Acc1 = 100 Acc1 = 150 Acc1 = 170

Producer/consumer int p = 0; c = 0 co producer: // consumer: while (true) while(true) while (p!= c); while (p==c); p++; c++; buf = new_item item = buf oc

Producer/consumer int p = 0; c = 0 co producer: // consumer: while (true) while(true) while (p!= c); while (p==c); buf = new_item; item = buf; p++ c++ oc

Concurrent blocks in a cobegin statement may interfere with one another since the execution is not atomic Only some of the interleavings may lead to correct answers. Additional synchronization is required to ensure that only those interleavings occur.

Atomic execution Process: a sequence of statements Each statement is a sequence of atomic actions Action action: indivisible action that changes the program state Fine-grained atomic action: an atomic action implemented by the hardware.

Independence of processes Independence of parallel processes: Read set: set of variables read by a block Write set: set of variables updated by a block If the following pairs are disjoint (read set of P, write set of Q) (write set of P, read set of P) (write set of P, write set of Q) then P and Q are independent Independent blocks can execute without interference. They are atomic with respect to each other

Example 2 int x = 0; z = 5; y = 0 co P: x = x + 1 x = x + z // Q: y = z – 1 oc Read x; Add 1; Write x; Read z Read x Add x to z Write x Read x; Add 1; Write x; Read z Read x Add x to z Write x Read x; Add 1; Write x; Read z Read x Add x to z Write x Read z; Sub 1; Write y; Read z; Sub 1; Write y; Read z; Sub 1; Write y; P.read = {x,z}, P.write = {x} Q.read = {z}, Q.write = {y}

Finding max in an array A[0..N-1] Find_max(A,N) { co // int m1 = A[0] int m2 = A[N/2] for (int i=1;i<N/2;i++) for (int j = N/2 + 1; j < N; j++); if (m1 < A[i]) m1 = A[i]; if (m2 < A[j]) m2 = A[j] oc if m1 < m2 then return m2 else return m1 }

Examples (contd) int x = 0; co x = x + 1 // x = x – 1 oc

At-Most-Once Property Critical reference in an expression is a reference to a variable that is changed by another process. x = e satisfies at-most-once property if either e contains at most one critical reference and x is not read by another process, or e contains no critical references (in which case, x can be read by others) If an assignment statement meets this property then its execution will appear to be atomic

Example int x = 0; y = 0 co x = x + 1 // y = y + 1 oc co x = y + 1 // y = y + 1 oc co x = y + 1 // y = x + 1 oc

Example int x = 0; y = 0 co x = x + 1 // y = y + 1 oc co x = y + 1 // y = y + 1 oc co x = y + 1 // y = x + 1 oc Read y; Add 1; Write x; Read y; Add 1; Write y;

Synchronization statements < S > : S is executed atomically < await B  S >: The execution of S is delayed until B is true. No other statement executes between evaluation of B to true and execution of S. Thus, B is guaranteed to be true when S is executed.

Examples < await count > 0  count++> vs while (count <= 0) {}; count++ int x = 0; y = 0; co < x = y + 1> // <y = x + 1> oc

Readers/writer problem Reader: < await(nw == 0)  nr++ > read < nr-- > Writer: < await(nw == 0 && nr == 0)  nw++> write < nw-- >

Producer/consumer Int p = 0; c = 0 co producer: // consumer: while (true) while(true) <await(p== c)  < await (p!=c)  buf = new_item; item = buf; p++ > c++> oc

Producer/consumer int p = 0; c = 0 co producer: // consumer1: // consumer2: while (true) while(true) while(true) <await(p==c)> < await (p!=c)  < await(p!=c) buf = new_item; item1 = buf; item2 = buf p++ c++> c++> oc