Semaphores Reference text: Tanenbaum ch. 2.3.5 - 2.3.7.

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.
Ch 7 B.
Prepared By Sarath S Menon S6 CSE.  Imagine a scenario in which there exists two Distinct processes both operating on a single shared data area.  One.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Interprocess Communication
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
Classic Synchronization Problems
Enforcing Mutual Exclusion, Semaphores. Four different approaches Hardware support Disable interrupts Special instructions Software-defined approaches.
1 Threads CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5.
Concurrency, Race Conditions, Mutual Exclusion, Semaphores, Monitors, Deadlocks Chapters 2 and 6 Tanenbaum’s Modern OS.
Interprocess Communication
Chapter 2: Processes Topics –Processes –Threads –Process Scheduling –Inter Process Communication (IPC) Reference: Operating Systems Design and Implementation.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
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.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
CS Introduction to Operating Systems
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
CY2003 Computer Systems Lecture 06 Interprocess Communication Monitors.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
Lecture 15 Semaphore & Bugs. Concurrency Threads Locks Condition Variables Fixing atomicity violations and order violations.
2.3 interprocess communcation (IPC) (especially via shared memory & controlling access to it)
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Semaphores Reference –text: Tanenbaum ch
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
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.
CS 537 – Introduction to Operating Systems
Interprocess Communication Race Conditions
CS703 - Advanced Operating Systems
Instructor: Junfeng Yang
Semaphores Reference text: Tanenbaum ch
CS703 – Advanced Operating Systems
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Background on the need for Synchronization
CIS Operating Systems Synchronization
Chapter 5: Process Synchronization – Part II
Interprocess Communication (3)
Lecture 13: Producer-Consumer and Semaphores
Process Synchronization
Synchronization and Semaphores
Concurrency: Mutual Exclusion and Process Synchronization
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
Lecture 13: Producer-Consumer and Semaphores
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CMPE 135: Object-Oriented Analysis and Design April 30 Class Meeting
Lecture 12 CV and Semaphores
CS 144 Advanced C++ Programming May 7 Class Meeting
Process Synchronization
Lab #9 Semaphores Operating System Lab.
Presentation transcript:

Semaphores Reference text: Tanenbaum ch. 2.3.5 - 2.3.7

Sleep and Wakeup MUTEX with busy waiting wastes precious CPU time Also suffers priority inversion problem Low level process in critical region and high level process in busy waiting Low level process never gets scheduled to get out of critical region sleep - system call causes the caller to block wakeup - system call wakes up the caller

Priority Inversion Problem High Direct blocking Using R1 Med Low Using R1 Using R1 time 1 2 3 4 5 6 7

Producer-Consumer Problem Two processes share a common, fixed-size buffer Producer puts info in the buffer. Consumer takes it out.

Problems with Sleep and Wakeup Race condition can occur with count: # of items in buffer Buffer is empty and consumer just reads count At that instant, scheduler decides to stop consumer temporarily and run producer Producer inserts an item in the buffer. With count =1, it tries to wake consumer up The wake up signal is lost since consumer is not asleep When consumer next runs, it sees count = 0 and falls asleep Eventually, the producer will fill up the buffer and fall asleep Both processes will sleep forever

Semaphores Need a facility to remember the pending wakeup and applies it to a sleep A new variable type,semaphore, is used to count the number of wakeups for future use 2 operations: down(P) : Check count. If count is positive, decrement it and return. If count is 0, put this process to sleep. Checking the value, changing it and going to sleep is done in one atomic action up(V): increments count and wakes up a waiter. Incrementing and waking up the process is done in one atomic action semaphores are used in both user and kernel modes

Semaphore Example int sem = createsem(2); down(sem); /*sem count goes from 2->1 */ down(sem); /*sem count goes from 1->0 */ down(sem); /*this thread waits forever */

Create a MUTEX from a Semaphore Use a binary semaphore(0 or 1) /* initial count of 1 means one process is allowed to run in the critical code */ int mutex = createsem(1); void enter_region(int mutex) { down(mutex); } void leave_region(int mutex) up(mutex);

Semaphore APIs POSIX-compliant prototypes are defined in /sys/semaphore.h sem_init(…) - create semaphore with init count sem_wait(…)- same as down sem_post(…)- same as up sem_destroy(…)- remove semaphore … Non-POSIX compliant prototypes are defined in /sys/synch.h: sema_init(…) etc. Use “man” for find more details on APIs

Solving Producer-Consumer Problem Data pipeline similar to MacDonald’s burger pipeline Don’t want to spin- waste CPU cycles For a bounded data buffer(i.e. buffer with fixed number of slots): Consumer: use a semaphore to keep track of number of objects in the data buffer. Buffer empty, consumer waits. Producer: use a semaphore to keep track of the number of spaces in the buffer. Buffer full, producer waits.

Producer-consumer program /* needs various headers, including <semaphore.h>; need to define N */ sem_t mutex, empty, full; /* struct type sem_t is defined in header */ int main() { if (sem_init(&mutex, /*process*-local*/ 0, /*init count*/ 1) < 0){ perror("Failed to create semaphore 1"); return 1; } if (sem_init(&empty, /*process-local*/ 0, /*init count*/ N) < 0){ perror("Failed to create semaphore 2"); return 1; if (sem_init(&full, /*process-local*/ 0, /*init count*/ 0) < 0){ perror("Failed to create semaphore 3"); return 1; thread_create(producer, ...); thread_create(consumer, ...); getchar(); /* block main thread */ /* here, could bring down threads and destroy semaphores, but exit will do it for us */ return 0;

Producer-consumer program(cont’d) /* Note: each system call should have its return value tested as show above for sem_init */ /* Producer will run first and produce N items. During this time, consumer will start and consume some */ void producer() { int item; while (TRUE) { item = produce_item(); sem_wait(&empty); /* claim empty spot */ sem_wait(&mutex); insert_item(item); /* insert, protected by mutex */ sem_post(&mutex); sem_post(&full); /* signal filled-in spot */ }

Producer-consumer program(cont’d) void consumer() { int item; while (TRUE) { sem_wait(&full); /* claim spot in buffer */ sem_wait(&mutex); item = remove_item(); /* remove, protected by mutex */ sem_post(&mutex); sem_post(&empty); /* signal empty spot */ consume_item(item); }

Sharing Semaphores Among Threads For multithreading, no problem since all threads have access to the semaphore What happens if threads operate on a different process? Need to have shared data structures store in kernel; access them using system calls OS provides ways for sharing data structures; e.g. a shared file

Monitors Thinks of this as a guard attached to a piece of code, one thread inside and others waiting their turns outside A language construct (procedures, variables and data structure) to set up mutex protection for its enclosed code and helps with necessary blocking Processes can call procedures within a monitor, but they cannot directly access the monitor’s internal data from outside Only 1 process can be active in a monitor at any instant

Monitor Example

Blocking Provided by Monitor Use conditioned variables(e.g. full, empty) and their operations (e.g. wait and signal) When the monitor cannot continue, it does a wait on some conditioned variable(e.g. full). This causes the calling process to block. Another process can then enter the monitor. The other process can wake up the blocked one by doing a signal on the conditioned variable (e.g. full) Need a rule to tell what happens after a signal to prevent 2 processes active in the monitor at the same time