Download presentation
Presentation is loading. Please wait.
1
CIS 5512 - Operating Systems Synchronization
Professor Qiang Zeng
2
CIS 5512 – Operating Systems
Previous class… IPC for passing data Pipe, FIFO, Message Queue, Shared Memory Concepts Race condition, critical section, and mutual exclusion Synchronization primitives What (are those sync primitives) How (to implement them) Pipes, FIFO (named pipes), message queues, and shared memory CIS 5512 – Operating Systems
3
Compare the following four IPCs for data passing
Previous class… Compare the following four IPCs for data passing IPC method Features Pipes Can 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 queues Unlike Pipes and FIFOs, they support message boundary and message types Shared memory Data passing doesn’t go through kernel, so it is usually the most efficient one CIS 5512 – Operating Systems
4
CIS 5512 – Operating Systems
Previous class… Can you give an example of race condition? How to resolve such race condition bugs? Example 1: counter++; Example 2: if (account >= 100) account -=100; Solution: identify critical sections in the program, and enforce proper synchronization, e.g., mutual exclusion CIS 5512 – Operating Systems
5
Big picture of synchronization primitives
Software solutions (Dekker’, Bakery, etc.) Busy-waiting Hardware-assisted solutions (based on atomic read-modify-write instructions) Semaphore: it contains an internal counter indicating the number of resources. Binary Semaphore is a special semaphore, whose counter value can only be 0 or 1; it can be used as a mutex Sync. Primitives Blocking Monitor: language-assisted synchronization; data + procedures + lock Condition variable: guarded by a lock to wait on some event CIS 5512 – Operating Systems
6
Previous class: Semaphore – P() and V()
// Atomic and mutual exclusion down(S) { // or, P() if(S.cnt>0) S.cnt--; else put the current process in queue; block the current process; } // Atomic and mutual exclusion up(S) { // or, V() if(any process is in S’s wait queue) remove a process from the queue; resume it; else S.cnt++; } enter_region() { while(test_and_set(&lock) == 1) ; } leave_region() { lock = 0;} test_and_set(int* p){ int t = *p; *p = 1; return t; } CIS 5512 – Operating Systems
7
CIS 5512 – Operating Systems
Previous class… What is a Monitor? Monitor: a language-assisted construct that encapsulates shared data, procedures, and synchronization. A monitor has a mutex lock and a queue. A process has to acquire the lock of before invoking any of the procedures; if the lock is not available, the process sleeps at the queue Compiler automatically inserts lock/unlock operations upon entry/exit of each monitor procedure CIS 5512 – Operating Systems
8
What is a Condition Variable? How to use it?
Previous class… What is a Condition Variable? How to use it? A CV allows a process that has acquired a lock to relinquish the lock (so that other processes have chance to progress) and sleeps; the process is waken up when it is signaled by another process APIs: wait(CV), signal(CV), broadcast(CV) CIS 5512 – Operating Systems
9
How to use a Condition Variable
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t c = PTHREAD_COND_INITIALIZER; Strange_withdraw(x) { … pthread_mutex_lock(&m); while(account < x) // “if” will not work pthread_cond_wait(&c, &m); account -= x; pthread_mutex_unlock(&m); } Deposit(y){ pthread_mutex_lock(&m); account += y; pthread_cond_signal(&c); pthread_mutex_unlock(&m); } Always hold the lock while performing CV operations Always put the wait operation in a loop (Mesa type) In order to check whether the condition is true Note the the signal(c) only hints the conditions changes; it does not imply that the condition is definite true Hoare vs Mesa type monitor The former: In Hoare's original 1974 monitor definition, the signaler yields the monitor to the released thread. More specifically, if thread A signals a condition variable CV on which there are threads waiting, one of the waiting threads, say B, will be released immediately. Before B can run, A is suspended and its monitor lock is taken away by the monitor. Then, the monitor lock is given to the released thread B so that when it runs it is the only thread executing in the monitor. Sometime later, when the monitor becomes free, the signaling thread A will have a chance to run. The Mesa Type Monitors CIS 5512 – Operating Systems
10
“A Condition Variable has to be used with a lock.” Is this true?
Previous class… “A Condition Variable has to be used with a lock.” Is this true? Yes. A CV is used either with an implicit lock (e.g., the lock of a Monitor) or an explicit lock. CIS 5512 – Operating Systems
11
Outline Restroom problem Bar problem Enforcing execution order
Single-slot producer-consumer problem Multi-slot producer-consumer problem
12
Using Semaphores: The restroom problem and the bar problem
13
Binary Semaphore for the restroom problem: mutual exclusion
Given a single-spot restroom, write the function for using the restroom 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 enforcing MUTual EXclusion S = 1; // shared among customers (processes) use_restroom() { down(S); // try to enter the restroom; = lock() Use the restroom //critical section up(S); // leave the restroom; = unlock() }
14
Semaphore for the bar problem
Capacity = 100 Many customers try to enter the bar concurrently Please write code to make sure customers<=100 Caution: a Mutex will not work well; why? S = 100; // shared among processes // each process does the following down(S); // try to enter the bar Have fun; up(S); // leave the bar CIS 5512 – Operating Systems
15
Using Semaphores: Enforcing orders
16
Semaphore for enforcing order
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); // Process 1 down(S); B;
17
Semaphore for enforcing order
Two processes: p0 and p1 How to enforce the order: p0.A1 -> p1.B1 -> p1.B2 -> p0.A2 Hint: use two semaphores S1 = 0; // shared S2 = 0; // shared // Process 0 A1; up(S1); down(S2); A2; // Process 1 down(S1); B1; B2; up(S2); CIS 5512 – Operating Systems
18
Using Semaphores: Single-slot Producer-Consumer problem
19
Single-slot Producer-Consumer problem
One slot; multi Producers keep producing items, while multi Consumers keep consumes items Hint: removeItem() cannot proceed if there is no item in the slot fillSlot() cannot proceed if the slot still has an 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); } CIS 5512 – Operating Systems
20
Using Semaphores: The Producer-Consumer Problem
Acknowledgement: some slides courtesy of Dr. Brighten Godfrey
21
Producer-consumer problem
Chefs cook items and put them on a conveyer belt Waiters pick items off the belt
22
Producer-consumer problem
Now imagine many chefs! ...and many waiters!
23
Producer-consumer problem
A potential mess!
24
Producer-consumer problem
Chef (Producer) Waiter (Consumer) inserts items removes items Shared resource: bounded buffer Efficient implementation: circular fixed-size buffer
25
Shared buffer Chef (Producer) Waiter (Consumer)
26
Shared buffer What does the chef do with a new pizza?
Chef (Producer) Waiter (Consumer) insertPtr removePtr What does the chef do with a new pizza? Where does the waiter take a pizza from?
27
Shared buffer Insert pizza Chef (Producer) Waiter (Consumer) insertPtr
removePtr Insert pizza
28
Shared buffer Insert pizza Chef (Producer) Waiter (Consumer) insertPtr
removePtr Insert pizza
29
Shared buffer insertPtr removePtr Insert pizza Chef (Producer)
Waiter (Consumer) insertPtr removePtr Insert pizza
30
Shared buffer insertPtr removePtr Remove pizza removePtr
Chef (Producer) Waiter (Consumer) insertPtr removePtr Remove pizza removePtr
31
Shared buffer insertPtr Insert pizza removePtr Chef (Producer)
Waiter (Consumer) insertPtr Insert pizza removePtr
32
Shared buffer insertPtr Insert pizza removePtr Chef (Producer)
Waiter (Consumer) insertPtr Insert pizza removePtr
33
Shared buffer BUFFER FULL: Producer must wait! Insert pizza insertPtr
Chef (Producer) Waiter (Consumer) BUFFER FULL: Producer must wait! Insert pizza insertPtr removePtr
34
Shared buffer Remove pizza insertPtr removePtr Chef (Producer)
Waiter (Consumer) Remove pizza insertPtr removePtr
35
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
36
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
37
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
38
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
39
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
40
Shared buffer removePtr Remove pizza insertPtr Chef (Producer)
Waiter (Consumer) removePtr Remove pizza insertPtr
41
Shared buffer Buffer empty: Consumer must be blocked! Remove pizza
Chef (Producer) Waiter (Consumer) Buffer empty: Consumer must be blocked! removePtr Remove pizza insertPtr
42
Designing a solution Wait for empty slot Insert item
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?
43
Designing a solution Wait for empty slot Insert item
Chef (Producer) Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available Mutex (shared buffer) What synchronization do we need?
44
Designing a solution Wait for empty slot Insert item
Chef (Producer) Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available Semaphore (# empty slots) What synchronization do we need?
45
Designing a solution Wait for empty slot Insert item
Chef (Producer) Waiter (Consumer) Wait for empty slot Insert item Signal item arrival Wait for item arrival Remove item Signal empty slot available Semaphore (# filled slots) What synchronization do we need?
46
Producer-Consumer Code
buffer[pi] = data; pi = (pi + 1) % N; result = buffer[ci]; ci = (ci +1) % N;
47
Single-slot Producer-Consumer problem
One slot; multi Producers keep producing items, while multi Consumers keep consumes items Hint: Consumer.removeItem() has to occur after Producer.fillSlot() Once the slot is filled, Producer.fillSlot() has to occur after Consumer.removeItem() 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); } CIS 5512 – Operating Systems
48
Producer-Consumer Code
Counting semaphore – check and decrement the number of free slots Counting semaphore – check and decrement the number of available items sem_wait(&slots); mutex_lock(&mutex); buffer[pi] = data; pi = (pi + 1) % N; mutex_unlock(&mutex); sem_post(&items); sem_wait(&items); mutex_lock(&mutex); result = buffer[ci]; ci = (ci +1) % N; mutex_unlock(&mutex); sem_post(&slots); Block if there are no items to take Block if there are no free slots One mutex can be split into two mutexes? Done – increment the number of available items Done – increment the number of free slots
49
Implementation based on Monitor and CV
monitor ProducerConsumer condition cv_full, cv_cempty; int count; procedure produce(); { while (count == N) wait(cv_full); // if buffer is full, block put_item(widget); // put item in buffer count = count + 1; // increment count of full slots if (count == 1) signal(cv_empty); // if buffer was empty, wake consumer } procedure consume(); while (count == 0) wait(cv_empty); // if buffer is empty, block remove_item(widget); // remove item from buffer count = count - 1; // decrement count of full slots if (count == N-1) signal(cv_full); // if buffer was full, wake producer CIS 5512 – Operating Systems
50
Summary Restroom problem Bar problem Enforcing execution order
Single-slot producer-consumer problem Multi-slot producer-consumer problem
51
CIS 5512 – Operating Systems
References Monitor type: Hoare vs Mesa (Mesa is the choice of most OSes) CIS 5512 – Operating Systems
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.