Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Semaphores and Monitors: High-level Synchronization Constructs.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Day 14 Concurrency. Software approaches Programs must be written such that they ensure mutual exclusion. Petersons and Dekkers (Appendix B)
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
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. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
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.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
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, Locks and Monitors By Samah Ibrahim And Dena Missak.
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 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Synchronizing Threads with Semaphores
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 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Semaphores Reference –text: Tanenbaum ch
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Web Server Architecture Client Main Thread for(j=0;j
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
Chien-Chung Shen CIS/UD
Synchronization Semaphores
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
Semaphores Reference text: Tanenbaum ch
Process Synchronization: Semaphores
PARALLEL PROGRAM CHALLENGES
Process Synchronization
Chapter 5: Process Synchronization – Part II
Operating Systems CMPSC 473
Semaphores and Bounded Buffer
Semaphores Questions answered in this lecture:
Threading And Parallel Programming Constructs
Process Synchronization
Synchronization and Semaphores
Synchronization Hank Levy 1.
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
Thread Synchronization including Mutual Exclusion
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Process Synchronization
Semaphores Reference text: Tanenbaum ch
“The Little Book on Semaphores” Allen B. Downey
Process Synchronization
Presentation transcript:

Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for scheduling constraints? Examples: Join and Producer/Consumer UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department CS 537 Introduction to Operating Systems Andrea C. Arpaci-Dusseau Remzi H. Arpaci-Dusseau

Motivation for Semaphores Locks only provide mutual exclusion Ensure only one thread is in critical section at a time May want more: Place ordering on scheduling of threads Example: Producer/Consumer –Producer: Creates a resource (data) –Consumer: Uses a resource (data) Example - ps | grep “gcc” | wc Don’t want producers and consumers to operate in lock step –Place a fixed-size buffer between producers and consumers –Synchronize accesses to buffer –Producer waits if buffer full; consumer waits if buffer empty

Semaphores Semaphores: Introduced by Dijkstra in 1960s Semaphores have two purposes Mutex: Ensure threads don’t access critical section at same time Scheduling constraints: Ensure threads execute in specific order

Semaphore Operations Allocate and Initialize Semaphore contains a non-negative integer value User cannot read or write value directly after initialization –Sem_t sem; –Int sem_init(&sem, is_shared, init_value); Wait or Test P() for “test” in Dutch (proberen) Waits until value of sem is > 0, then decrements sem value Int sem_wait(&sem); Signal or Increment or Post V() for “increment” in Dutch (verhogen) Increments value of semaphore Int sem_post(&sem);

Semaphore Implementation typedef struct { int value; queue tlist; } semaphore; sem_wait (semaphore *S) { // Must be executed atomically S->value--; if (S->value < 0) { add this process to S->tlist; block(); } sem_signal (semaphore *S) {// Must be executed atomically S->value++; if (S->value <= 0) { remove thread t from S->tlist; wakeup(t); }

Semaphore Example What happens if sem is initialized to 2? Scenario: Three processes call sem_wait(&sem) Observations Sem value is negative --> Number of waiters on queue Sem value is positive --> Number of threads that can be in c.s. at same time

Mutual Exclusion with Semaphores Previous example with locks: Void deposit (int amount) { mutex_lock(&mylock); balance += amount; mutex_unlock(&mylocak); } Example with semaphores: Void deposit(int amount) { sem_wait(&sem); balance += amount; sem_signal(&sem); } To what value should sem be initialized???

Binary Semaphores Binary semaphore is sufficient for mutex Binary semaphore has boolean value (not integer) bsem_wait(): Waits until value is 1, then sets to 0 bsem_signal(): Sets value to 1, waking one waiting process General semaphore is also called counting semaphore

Scheduling Constraints with Semaphores General case: One thread waits for another to reach some point Example: Implement thread_join() Parent thread calls thread_join(), which must wait for child thread to call exit(); Shared sem between parent and child (created when child thread is created) To what value is sem initialized??? Parent thread Thread_join() { sem_wait(&sem); } Child thread exit() { sem_signal(&sem); }

Producer/Consumer: Single Buffer Simplest case: Single producer thread, single consumer thread Single shared buffer between producer and consumer Requirements Consumer must wait for producer to fill buffer Producer must wait for consumer to empty buffer (if filled) Requires 2 semaphores emptyBuffer: Initialize to ??? fullBuffer: Initialize to ??? Producer While (1) { sem_wait(&emptyBuffer); Fill(&buffer); sem_signal(&fullBuffer); } Consumer While (1) { sem_wait(&fullBuffer); Use(&buffer); sem_signal(&emptyBuffer); }

Producer/Consumer: Circular Buffer Next case: Single producer thread, single consumer thread Shared buffer with N elements between producer and consumer Requirements Consumer must wait for producer to fill buffer Producer must wait for consumer to empty buffer (if filled) Requires 2 semaphores emptyBuffer: Initialize to ??? fullBuffer: Initialize to ??? Producer i = 0; While (1) { sem_wait(&emptyBuffer); Fill(&buffer[i]); i = (i+1)%N; sem_signal(&fullBuffer); } Consumer j = 0; While (1) { sem_wait(&fullBuffer); Use(&buffer[j]); j = (j+1)%N; sem_signal(&emptyBuffer); }

Producer/Consumer: Multiple Threads Final case: Multiple producer threads, multiple consumer threads Shared buffer with N elements between producer and consumer Requirements Consumer must wait for producer to fill buffer Producer must wait for consumer to empty buffer (if filled) Each consumer must grab unique filled element Each producer must grab unique empty element Why will previous code not work??? Producer While (1) { sem_wait(&emptyBuffer); myi = findempty(&buffer); Fill(&buffer[myi]); sem_signal(&fullBuffer); } Consumer While (1) { sem_wait(&fullBuffer); myj = findfull(&buffer); Use(&buffer[myj]); sem_signal(&emptyBuffer); } Are myi and myj private or shared? Where is mutual exclusion needed???

Producer/Consumer: Multiple Threads Consider three possible locations for mutual exclusion; Which work??? Which is best??? Producer #1 sem_wait(&mutex); sem_wait(&emptyBuffer); myi = findempty(&buffer); Fill(&buffer[myi]); sem_signal(&fullBuffer); sem_signal(&mutex); Consumer #1 sem_wait(&mutex); sem_wait(&fullBuffer); myj = findfull(&buffer); Use(&buffer[myj]); sem_signal(&emptyBuffer); sem_signal(&mutex); Consumer #2 sem_wait(&fullBuffer); sem_wait(&mutex); myj = findfull(&buffer); Use(&buffer[myj]); sem_signal(&mutex); sem_signal(&emptyBuffer); Consumer #3 sem_wait(&fullBuffer); sem_wait(&mutex); myj = findfull(&buffer); sem_signal(&mutex); Use(&buffer[myj]); sem_signal(&emptyBuffer); Producer #2 sem_wait(&emptyBuffer); sem_wait(&mutex); myi = findempty(&buffer); Fill(&buffer[myi]); sem_signal(&mutex); sem_signal(&fullBuffer); Producer #3 sem_wait(&emptyBuffer); sem_wait(&mutex); myi = findempty(&buffer); sem_signal(&mutex); Fill(&buffer[myi]); sem_signal(&fullBuffer);