University of Pennsylvania 9/21/00CSE 3801 Concurrent Process Synchronization (Lock and semaphore) CSE 380 Lecture Note 5 Insup Lee.

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: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Ch 7 B.
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.
Unix IPC and Synchronization. Pipes and FIFOs Pipe: a circular buffer of fixed size written by one process and read by another int pipe(int fildes[2])
1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania Fall 2003 Lecture 2.4: Interprocess Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
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.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process 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.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
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.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
CSE 451: Operating Systems Winter 2012 Semaphores and Monitors Mark Zbikowski Gary Kimura.
Module 2.1: Process Synchronization
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization(cont.) Advanced Operating System Fall 2009.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
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.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock and Starvation
Chapter 6: Process Synchronization
Process Synchronization
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Deadlock and Starvation
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Critical Section and Critical Resources
Critical Section and Critical Resources
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization Hank Levy 1.
CMPT 300: Operating Systems I
Critical section problem
CSE 451: Operating Systems Winter Module 7 Semaphores and Monitors
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Process/Thread Synchronization (Part 2)
Presentation transcript:

University of Pennsylvania 9/21/00CSE 3801 Concurrent Process Synchronization (Lock and semaphore) CSE 380 Lecture Note 5 Insup Lee

University of Pennsylvania 9/21/00CSE 3802 Locks lock (x) performs lock: [ if x = false then x := true else go to lock ] unlock (x) performs [x := false ] E.g., var x : boolean parbegin P1:... lock(x); CS_1; unlock(x) … … Pn:... lock(x); CS_n; unlock(x) … parend

University of Pennsylvania 9/21/00CSE 3803 Properties 1. Starvation is possible. 2. Busy waiting. 3. Different locks may be used for different shared resources. 4. Proper use not enforced. E.g., forget to lock.

University of Pennsylvania 9/21/00CSE 3804 How to implement locks Requires an atomic (uninterruptable at the memory level) operations like test-and-set or swap. atomic function TestAndSet (var x: boolean): boolean; begin TestAndSet := x; x := true; end procedure Lock (var x : boolean); while TestAndSet(x) do skip od; procedure Unlock (var x: boolean); x := false; (1) If not supported by hardware, TestAndSet can be implemented by disabling and unabling interrupts. (2) Lock can also be implemented using atomic swap(x,y).

University of Pennsylvania 9/21/00CSE 3805 Hardware Instructions Examples: (1) VAX 11, (2) B6500 MIPS -- Load-Linked/Store Conditional (LL/SC) Pentium -- Compare and Exchange, Exchange, Fetch and Add SPARC -- Load Store Unsigned Bit (LDSTUB) in v9 PowerPC -- Load Word and Reserve (lwarx) and Store Word Conitional (stwcx)

University of Pennsylvania 9/21/00CSE 3806 (1) VAX 11 BBSSI Branch on Bit Set and Set Interlocked. BBCCI Branch on Bit Clear and Clear Interlocked. op bitpos, var, displacement Operation: teststate = if {BBSSI} then 1 else 0 {set interlock} tmp := bit bit := teststate {release interlock} if tmp = teststate then pc := pc + displacement fi Busy waiting -- set bit and if already set then go to 1$ 1$: BBSSI bit, base, 1$

University of Pennsylvania 9/21/00CSE 3807 (2) B6500 Read with lock operation. SPIN: If RDLK (x) then go to SPIN; RDLK(x) register memory A: addr of x B: 1 x: Swap the contents of B and x in one memory cycle and return B.

University of Pennsylvania 9/21/00CSE 3808 Semaphores P V Dijkstra ‘65 wait signal Per Brinch Hansen The semaphore has a value that is invisible to the users and a queue of processes waiting to acquire the semaphore. type semaphore = record value : integer; L : list of process; end P(S):[ S.value := S.value-1; if S.value < 0 then add this process to S.L; block; end if ] V(S):[ S.value := S.value + 1; if S.value <= 0 then remove a process P from S.L; wakeup(P); end if ]

University of Pennsylvania 9/21/00CSE 3809 Properties of semaphore parbegin S.value = 1 P1:... P(S); CS1; V(S);... P2:... P(S); CS2; V(S); Pn:... P(S); CSn; V(S);... parend Properties No busy waiting May starve unless FCFS (scheduling left to the implementer of semaphores) Can handle multiple users by proper initialization. Example: 3 tape drivers Can implement scheduling on an precedence graph.

University of Pennsylvania 9/21/00CSE More properties and examples e.g. P2 P1 P4 P3 P6 P5 P1: Do Work P2: P(S12) P3: P(13) V(S12) Do Work Do Work V(S13) V(S24) V(S34) V(S35) 5Proper use can't be enforced by compiler. e.g. P(S) V(S) CS CS V(S) P(S) e.g. S1, S2 P1: P(S1) P2: P(S2) P(S2) P(S1) CS CS V(S2) V(S1) V(S1) V(S2)

University of Pennsylvania 9/21/00CSE Classical problems The bounded buffer problem The readers and writers problems The sleeping barber problem The dining philosophers problem

University of Pennsylvania 9/21/00CSE The Producer-Consumer Problem  bounded buffer (of size n)  one set of processes (producers) write to it  one set of processes (consumers) read from it semaphore: full = 0 /* counting semaphores */ empty = n mutex = 1 /* binary semaphore */ process Producer process Consumer do forever do forever. P(full) /* produce */ P(mutex). /* take from buffer */ P(empty) V(mutex) P(mutex) V(empty) /* add to buffer */. V(mutex) /* consume */ V(full). end end

University of Pennsylvania 9/21/00CSE The Dining Philosopher Problem Five philosopher spend their lives thinking + eating. One simple solution is to represent each chopstick by a semaphore. P before picking it up & V after using it. var chopstick: array[0..4] of semaphores=1 philosopher i repeat P( chopstock[i] ); P( chopstock[i+1 mod 5] );... eat... V( chopstock[i] ); V( chopstock[i+1 mod 5] );... think... forever Is deadlock possible?

University of Pennsylvania 9/21/00CSE Number of possible states o5 philosophers oLocal state (LC) for each philosoper thinking, waiting, eating oGlabal state = (LC 1, LC 2, …, LC5) E.g., (thinking, waiting, waiting, eating, thinking) E.g., (waiting, eating, waiting, eating, waiting) oSo, the number of global states are 3 ** 5 = 243 oActually, it is a lot more than this since waiting can be Waiting for the first fork Waiting for the second fork

University of Pennsylvania 9/21/00CSE Number of possible behaviors Sequence of states Initial state: (thinking,thinking,thinking,thinking,thinking) The number of possible behaviors = 5 x 5 x 5 x … Deadlock state: (waiting,waiting,waiting,waiting, waiting) Given the state transition model of your implementation, show that it is not possible to reach the deadlock state from the initial state.

University of Pennsylvania 9/21/00CSE The Readers and Writers Problem Shared data to be accessed in two modes: reading and writing. –Any number of processes permitted to read at one time –writes must exclude all other operations. Read Write Read Y N conflict Write N N matrix Intuitively: Reader: | Writer: when(no_writers==0) do | when(no_readers==0 no_readers=no_readers+1 | and no_writers==0) do | no_writers = 1 | | | no_readers=no_readers-1 | no_writers = 0. |.. |.

University of Pennsylvania 9/21/00CSE A Solution to the R/W problem Semaphore: mutex = 1 /* mutual excl. for updating readcount */ wrt = 1 /* mutual excl. writer */ int variable: readcount = 0 Reader: P(mutex) readcount = readcount + 1 if readcount == 1 then P(wrt) V(mutex) P(mutex) readcount = readcount – 1 if readcount == 0 then V(wrt) V(mutex) Writer: P(wrt) V(wrt) Notes: wrt also used by first/last reader that enters/exits critical section. Solution gives priority to readers in that writers can be starved by stream of readers.

University of Pennsylvania 9/21/00CSE nd assignment Use semaphores Need to have shared memory between processes to allocate semaphores

University of Pennsylvania 9/21/00CSE Semaphores A semaphore is a non-negative integer count and is generally used to coordinate access to resources System calls: int sema_init(sema_t *sp, unsigned int count, int type, void * arg): Initialize semaphores pointed to by sp to count. type can assign several different types of behavior to a semaphore int sema_destroy(sema_t *sp); destroys any state related to the semaphore pointed to by sp. The semaphore storage space is not released. int sema_wait(sema_t *sp); blocks the calling thread until the semaphore count pointed to by sp is greater than zero, and then it atomically decrements the count.

University of Pennsylvania 9/21/00CSE Semaphores (cont’d) int sema_trywait(sema_t *sp); atomically decrements the semaphore count pointed to by sp, if the count is greater than zero; otherwise, it returns an error. int sema_post(sema_t *sp); atomically increments the semaphore count pointed to by sp. If there are any threads blocked on the semaphore,one will be unblocked.

University of Pennsylvania 9/21/00CSE Example The customer waiting-line in a bank is analogous to the synchronization scheme of a semaphore using sema_wait() and sema_trywait():

University of Pennsylvania 9/21/00CSE Semaphores example #include #define TELLERS 10 sema_t tellers; /* semaphore */ int banking_hours(), deposit_withdrawal; void *customer(), do_business(), skip_banking_today();... sema_init(&tellers, TELLERS, USYNC_THREAD, NULL); /* 10 tellers available */ while(banking_hours()) pthread_create(NULL, NULL, customer, deposit_withdrawal);... void * customer(int deposit_withdrawal) { int this_customer, in_a_hurry = 50; this_customer = rand() % 100;

University of Pennsylvania 9/21/00CSE if (this_customer == in_a_hurry) { if (sema_trywait(&tellers) != 0) if (errno == EAGAIN) { /* no teller available */ skip_banking_today(this_customer); return; } /* else go immediately to available teller and decrement tellers */ } else sema_wait(&tellers); /* wait for next teller, then proceed, and decrement tellers */ do_business(deposit_withdrawal); sema_post(&tellers); /* increment tellers; this_customer's teller is now available */ }