Chien-Chung Shen CIS/UD

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

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.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
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.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
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.
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.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Lecture 15 Semaphore & Bugs. Concurrency Threads Locks Condition Variables Fixing atomicity violations and order violations.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Synchronization.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Synchronisation Examples
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Semaphores Reference –text: Tanenbaum ch
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
Web Server Architecture Client Main Thread for(j=0;j
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Interprocess Communication Race Conditions
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization – Part II
Critical Section and Critical Resources
Critical Section and Critical Resources
Semaphores Questions answered in this lecture:
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Synchronization and Semaphores
Critical section problem
Chapter 30 Condition Variables
Chien-Chung Shen CIS/UD
Concurrency: Mutual Exclusion and Process Synchronization
Synchronization Primitives – Semaphore and Mutex
Thread Synchronization including Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Lecture 12 CV and Semaphores
CSE 542: Operating Systems
Chien-Chung Shen CIS/UD
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Chien-Chung Shen CIS/UD cshen@udel.edu Chapter 31 Semaphores Chien-Chung Shen CIS/UD cshen@udel.edu

Basic Ideas of Semaphores Invented by Edsger Dijkstra as a synchronization primitive, which can be used as both locks and condition variables Use of semaphores and implementation of semaphores BTW, do you know who Dijkstra is?

Definition An object with one integer value and two operations, sem_wait() and sem_post() Initial value of a semaphore determines its behavior sem_t s; sem_init(&s, 0, X); // 0: shared between threads in the same process // X: initial value int sem_wait(sem_t *s) { // P decrement the value of s by 1 wait if value of s is negative } int sem_post(sem_t *s) { // V increment the value of s by 1 if there are one or more threads waiting, wake one

Features sem_wait() will either return right away (because value of semaphore was 1 or higher when sem_wait() is called), or it will cause the caller to suspend execution waiting for a subsequent post. Multiple calling threads may call into sem_wait(), and thus all be queued waiting to be woken sem_post() does not wait. Rather, it simply increments the value of semaphore and then, if there is any thread waiting, wakes one of them up Value of semaphore, when negative? equal to the number of waiting threads

Semaphore as Lock X = ? termed binary semaphore (X = 1) sem_t m; sem_init(&m, 0, X); // initialize semaphore to X; sem_wait(&m); // critical section here sem_post(&m); X = ? termed binary semaphore (X = 1)

Binary Semaphore as Lock

Semaphore as Condition Variable Used when a thread waits for something to happen, and a different thread makes that something happen and then signaling that it has happened, thus waking the waiting thread. sem_t s; // parent waits for child void child(void *arg) { printf("child\n"); sem_post(&s); // signal here: child is done return NULL; return NULL; } int main(int argc, char *argv[]) { sem_init(&s, 0, X); // what should X be? printf("parent: begin\n");  pthread_t c; Pthread_create(c, NULL, child, NULL); sem_wait(&s); // wait here for child printf("parent: end\n"); return 0; X = ?

Semaphore as Condition Variable Parent waits for child Child runs to the end first X = 1

Producer/Consumer (Bounded Buffer) single producer and single consumer buffer of 1 slot – producer waits for a buffer to become empty in order to put data into it [who can empty the buffer?], and consumer similarly waits for a buffer to become filled before taking data out [who can fill the buffer] Can I put/take data into/out of buffer? If I did, I will let the other party know P cares empty and C cares full C P

Bounded Buffer P/C I What about MAX > 1 and multiple producers and consumers?

Bounded Buffer Multiple P/C So far so good? Deadlock! e.g., consumer runs first…….

Bounded Buffer Multiple P/C Reduce scope of lock

Dining Philosophers by Dijkstra Philosopher(int p) { while (1) { think(); getforks(p); eat(); putforks(p); } void getforks(int p) { sem_wait(forks[left(p)]); sem_wait(forks[right(p)]); } void putforks(int p) { sem_post(forks[left(p)]); sem_post(forks[right(p)]); sem_t forks[5]; int left(int p) { return p; } int right(int p) { return (p+1)%5; Problem? deadlock void getforks(int p) { if (p == 4) { sem_wait(forks[right(p)]); sem_wait(forks[left(p)]); } else { } change how forks are acquired by at least one of the philosophers

Semaphore Usage: Summary Mutual exclusion: binary semaphore as mutex lock Controlled access to a given resource consisting of a finite number of instances: counting semaphore Semaphore is initialized to the number of instances available Synchronization: two concurrent running threads T1 and T2 with statements S1 and S2, respectively Require S2 be executed only after S1 has completed (on one CPU) Semaphore s = 0; T1: S1; signal(s); T2: wait(s); S2; T1 S1 S2 T2

Implementation of Semaphore

Implementation of Semaphore via disabling/enabling interrupts