CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.

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.
Operating Systems Part III: Process Management (Process Synchronization)
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 2 Processes and Threads
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.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread 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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Synchronization April 14, 2000 Instructor Gary Kimura Slides courtesy of Hank Levy.
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.
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.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
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.
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:
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
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.
1 Inter Process Communication & Timers. 2 Time.h (page R:Ch9 pp ) #include time_t time(time_t *calptr); Epoch: 00:00 (midnight), Jan 1, 1970 GMT.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Operating Systems COMP 4850/CISG 5550 Interprocess Communication, Part II Dr. James Money.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Synchronization.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Semaphores Reference –text: Tanenbaum ch
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Section 5 Synchronization primitives (Many slides taken from Winter 2006)
Interprocess Communication Race Conditions
Semaphores Reference text: Tanenbaum ch
CIS Operating Systems Synchronization
CSE451 Basic Synchronization Spring 2001
Ref: William Stallings,Galvin, Naresh Chauhan G.Anuradha
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Synchronization Hank Levy 1.
Critical section problem
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Chapter 6 Synchronization Principles
Synchronization Hank Levy 1.
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
Semaphores Reference text: Tanenbaum ch
Process/Thread Synchronization (Part 2)
Presentation transcript:

CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015

Previous class… IPC –Pipes –FIFO (named pipes) –message queues –shared memory Concepts –Race condition, critical section, mutual exclusion Synchronization primitives CIS 5512 – Operating Systems 2

Compare different IPCs IPC methodFeatures PipesCan only be used among parent and child FIFOs (named pipes) Can be referred to by a string, so doesn’t have the limitation above Message queuesUnlike Pipes and FIFOs, they support message boundary and message types Shared memoryData passing doesn’t go through kernel, so it is usually the most efficient one CIS 5512 – Operating Systems 3

Concepts: race condition, critical section, and mutual exclusion lock(); counter++ //critical section unlock(); What goes here? The code below is run simultaneously in multiple threads/processes Race condition exists unless…

Big picture of synchronization primitives Spin locks (busy-waiting locks) Algorithms that do not rely on special instructions (Dekker’, Peterson’s, Bakery) Algorithms based on atomic read-modify- write instructions (test-and-set, xchg)

Solutions based on atomic read-modify- write instructions CIS 5512 – Operating Systems 6 test_and_set(int* p){ int t = *p; *p = 1; return t; } test_and_set(int* p){ int t = *p; *p = 1; return t; } enter_region() { while(test_and_set(&lock) == 1) ; } leave_region() { lock = 0;} enter_region() { while(test_and_set(&lock) == 1) ; } leave_region() { lock = 0;}

Big picture of synchronization primitives 7 Sync. Primitives (or loosely, “locks”) Spin locks (busy-waiting locks) Semaphore: it contains an internal counter indicating the number of resources; API: down()/up() (or, sem_wait/sem_signal) Binary Semaphore is a special semaphore, whose counter value can only be 0 or 1; sometimes it is used as a mutex Conditional Variables: they are used to ease concurrent programming. You have to use it with a mutex. Monitor encapsulates CV and mutex Blocking locks Algorithms that do not rely on special instructions (Dekker’, Peterson’s, Bakery) Algorithms based on atomic read-modify- write instructions (test-and-set, xchg)

Semaphore – avoids busy-waiting CIS 5512 – Operating Systems 8 // Atomic down(S) { // or, P(), sem_wait() if(S.cnt>0) S.cnt--; else put the current process in queue; block the current process; } // Atomic down(S) { // or, P(), sem_wait() if(S.cnt>0) S.cnt--; else put the current process in queue; block the current process; } // Atomic up(S) { // or, V(), sem_signal() if(any process is in S’s wait queue) remove a process from the queue; resume it; else S.cnt++; } // Atomic up(S) { // or, V(), sem_signal() if(any process is in S’s wait queue) remove a process from the queue; resume it; else S.cnt++; }

Using Mutexs and Semaphores: The restroom problem and the bar problem

Binary Semaphore for the restroom problem: mutual exclusion Customers try to enter a single-spot restroom please write code describing the customers Hint: Binary Semaphore, a special semaphore, whose counter value can only be 0 or 1 Here, Binary Semaphore is used as Mutex, a blocking lock for MUTual EXclusion S = 1; // shared among customers (processes) // each customer (process) does the following down(S); // try to enter the restroom; = lock() Use the restroom //critical section up(S); // leave the restroom; = unlock() S = 1; // shared among customers (processes) // each customer (process) does the following down(S); // try to enter the restroom; = lock() Use the restroom //critical section up(S); // leave the restroom; = unlock()

Semaphore for the bar problem Capacity = 100 Many customers try to enter the bar concurrently Please write code describing the customers Caution: a Mutex will not work well; why? CIS 5512 – Operating Systems 11 S = 100; // shared among processes // each process does the following down(S); // try to enter the bar Have fun; up(S); // leave the bar S = 100; // shared among processes // each process does the following down(S); // try to enter the bar Have fun; up(S); // leave the bar

Using Semaphores: A simple signaling problem

Semaphore for signaling Process 0 and Process 1 How to make sure statement A in Process 0 gets executed before statement B in Process 1 Hint: use a semaphore and initialize it as 0 S = 0; // shared // Process 0 A; up(S); S = 0; // shared // Process 0 A; up(S); // Process 1 down(S); B; // Process 1 down(S); B;

Semaphore for signaling CIS 5512 – Operating Systems 14 How to make sure event A1 in Process 0 occurs before B1 in Process 1, and B2 in Process 1 occurs before A2 in Process 0? Hint: use two semaphores S1 = 0; // shared S2 = 0; // shared // Process 0 A1; up(S1); down(S2); A2; S1 = 0; // shared S2 = 0; // shared // Process 0 A1; up(S1); down(S2); A2; // Process 1 down(S1); B1; B2; up(S2); // Process 1 down(S1); B1; B2; up(S2);

Single-slot Producer-Consumer problem Hint: –Consumer.removeItem() has to occur after Producer.fillSlot() –Producer.fillSlot(), once the slot is filled, has to occur after Consumer.removeItem() CIS 5512 – Operating Systems 15 S_slot = 1; // shared S_item = 0; // shared // Producer while(true) { down(S_slot); fillSlot(); up(S_item); } S_slot = 1; // shared S_item = 0; // shared // Producer while(true) { down(S_slot); fillSlot(); up(S_item); } // Consumer while(true) { down(S_item); removeItem(); up(S_slot); } // Consumer while(true) { down(S_item); removeItem(); up(S_slot); }

Using Semaphores: The Producer-Consumer Problem Acknowledgement: some slides courtesy of Dr. Brighten Godfrey

Producer-consumer problem Chefs cook items and put them on a conveyer belt Waiters pick items off the belt

Producer-consumer problem Now imagine many chefs!...and many waiters!

Producer-consumer problem A potential mess!

Producer-consumer problem Chef (Producer)Waiter (Consumer) inserts itemsremoves items Shared resource: bounded buffer Efficient implementation: circular fixed-size buffer

Shared buffer Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr What does the chef do with a new pizza? Where does the waiter take a pizza from? Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Insert pizza insertPtr Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Insert pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Insert pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza removePtr Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Insert pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Insert pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr BUFFER FULL: Producer must wait! Insert pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Remove pizza Chef (Producer)Waiter (Consumer)

Shared buffer insertPtr removePtr Buffer empty: Consumer must be blocked! Remove pizza Chef (Producer)Waiter (Consumer)

Designing a solution Chef (Producer)Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available What synchronization do we need?

Designing a solution Chef (Producer)Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available What synchronization do we need? Mutex (shared buffer)

Designing a solution Chef (Producer)Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available What synchronization do we need? Semaphore (# empty slots)

Designing a solution Chef (Producer)Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available What synchronization do we need? Semaphore (# filled slots)

Producer-Consumer Code buffer[ insertPtr ] = data; insertPtr = (insertPtr + 1) % N; result = buffer[removePtr]; removePtr = (removePtr +1) % N; Critical Section: move insert pointer Critical Section: move remove pointer

Producer-Consumer Code sem_wait(&slots); mutex_lock(&mutex); buffer[ insertPtr ] = data; insertPtr = (insertPtr + 1) % N; mutex_unlock(&mutex); sem_post(&items); sem_wait(&items); mutex_lock(&mutex); result = buffer[removePtr]; removePtr = (removePtr +1) % N; mutex_unlock(&mutex); sem_post(&slots); Block if there are no free slots Block if there are no items to take Counting semaphore – check and decrement the number of free slots Counting semaphore – check and decrement the number of available items Done – increment the number of available items Done – increment the number of free slots

Consumer Pseudocode: getItem() sem_wait(&items); pthread_mutex_lock(&mutex); result = buffer[removePtr]; removePtr = (removePtr +1) % N; pthread_mutex_unlock(&mutex); sem_signal(&slots); Error checking/EINTR handling not shown

Producer Pseudocode: putItem(data) sem_wait(&slots); pthread_mutex_lock(&mutex); buffer[ insertPtr ] = data; insertPtr = (insertPtr + 1) % N; pthread_mutex_unlock(&mutex); sem_signal(&items); Error checking/EINTR handling not shown

Readers-Writers Problem

Mutual exclusion problem Problem statement: –Reader threads only read the object –Writer threads modify the object –Writers must have exclusive access to the object –Unlimited number of readers can access the object Occurs frequently in real systems, e.g., –Online airline reservation system –Multithreaded caching Web proxy

void writer(void) { while (1) { sem_wait(&w); /* Critical section */ /* Writing here */ sem_post(&w); } } Writers: int readcnt; /* Initially = 0 */ sem_t mutex, w; /* Both initially = 1 */ Shared: Solution favoring readers

(full code online) void reader(void) { while (1) { sem_wait(&mutex); readcnt++; if (readcnt == 1) /* First reader in */ sem_wait(&w); /* Lock out writers */ sem_post(&mutex); /* Main critical section */ /* Reading would happen here */ sem_wait(&mutex); readcnt--; if (readcnt == 0) /* Last out */ sem_post(&w); /* Let in writers */ sem_post(&mutex); } } Readers: Solution favoring readers

Summary Synchronization: more than just locking a critical section Semaphores useful for counting available resources –sem_wait(): wait for resource only if none available –sem_post(): signal availability of another resource Multiple semaphores / mutexes can work together to solve complex problems