Zeng-Qiang Yu Institute for Advanced Study, Tsinghua University, Beijing, China
CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
-
Upload
stuart-brandon-merritt -
Category
Documents
-
view
220 -
download
0
Transcript of CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
![Page 1: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/1.jpg)
CIS 5512 - Operating SystemsSynchronization
Professor Qiang Zeng
Fall 2015
![Page 2: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/2.jpg)
CIS 5512 – Operating Systems 2
Previous class…
• IPC– Pipes– FIFO (named pipes)– message queues– shared memory
• Concepts– Race condition, critical section, mutual exclusion
• Synchronization primitives
![Page 3: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/3.jpg)
CIS 5512 – Operating Systems 3
Compare different IPCs
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
![Page 4: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/4.jpg)
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/processesRace condition exists unless…
![Page 5: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/5.jpg)
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)
![Page 6: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/6.jpg)
CIS 5512 – Operating Systems 6
Solutions based on atomic read-modify-write instructions
test_and_set(int* p){ int t = *p; *p = 1; return t;}
enter_region() { while(test_and_set(&lock) == 1) ;}
leave_region() { lock = 0;}
![Page 7: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/7.jpg)
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)
![Page 8: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/8.jpg)
CIS 5512 – Operating Systems 8
Semaphore – avoids busy-waiting
// Atomicdown(S) { // or, P(), sem_wait() if(S.cnt>0) S.cnt--; else put the current process in queue; block the current process;}
// Atomicup(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++;}
![Page 9: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/9.jpg)
Using Mutexs and Semaphores:The restroom problem and the bar problem
![Page 10: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/10.jpg)
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 followingdown(S); // try to enter the restroom; = lock()Use the restroom //critical sectionup(S); // leave the restroom; = unlock()
![Page 11: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/11.jpg)
CIS 5512 – Operating Systems 11
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?
S = 100; // shared among processes
// each process does the followingdown(S); // try to enter the barHave fun; up(S); // leave the bar
![Page 12: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/12.jpg)
Using Semaphores:A simple signaling problem
![Page 13: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/13.jpg)
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 0A;up(S);
// Process 1down(S);B;
![Page 14: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/14.jpg)
CIS 5512 – Operating Systems 14
Semaphore for signaling
• 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; // sharedS2 = 0; // shared
// Process 0A1;up(S1);down(S2);A2;
// Process 1down(S1);B1;B2;up(S2);
![Page 15: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/15.jpg)
CIS 5512 – Operating Systems 15
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()S_slot = 1; // sharedS_item = 0; // shared
// Producerwhile(true) { down(S_slot); fillSlot(); up(S_item);}
// Consumerwhile(true) { down(S_item); removeItem(); up(S_slot);}
![Page 16: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/16.jpg)
Using Semaphores:The Producer-Consumer
Problem
Acknowledgement: some slides courtesy of Dr. Brighten Godfrey
![Page 17: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/17.jpg)
Producer-consumer problem
• Chefs cook items and put them on a conveyer belt
Waiters pick items off the belt
![Page 18: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/18.jpg)
Producer-consumer problem
• Now imagine many chefs!
...and many waiters!
![Page 19: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/19.jpg)
Producer-consumer problem
• A potential mess!
![Page 20: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/20.jpg)
Producer-consumer problemChef (Producer) Waiter (Consumer)
inserts items removes items
Shared resource:bounded buffer
Efficient implementation:circular fixed-size buffer
![Page 21: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/21.jpg)
Shared bufferChef (Producer) Waiter (Consumer)
![Page 22: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/22.jpg)
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)
![Page 23: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/23.jpg)
Shared buffer
insertPtr
removePtr
Insert pizza
insertPtr
Chef (Producer) Waiter (Consumer)
![Page 24: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/24.jpg)
Shared buffer
insertPtr
removePtr
Insert pizza
Chef (Producer) Waiter (Consumer)
![Page 25: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/25.jpg)
Shared buffer
insertPtr
removePtr
Insert pizza
Chef (Producer) Waiter (Consumer)
![Page 26: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/26.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
removePtr
Chef (Producer) Waiter (Consumer)
![Page 27: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/27.jpg)
Shared buffer
insertPtr
removePtr
Insert pizza
Chef (Producer) Waiter (Consumer)
![Page 28: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/28.jpg)
Shared buffer
insertPtr
removePtr
Insert pizza
Chef (Producer) Waiter (Consumer)
![Page 29: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/29.jpg)
Shared buffer
insertPtr
removePtr
BUFFER FULL: Producer must wait!
Insert pizza
Chef (Producer) Waiter (Consumer)
![Page 30: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/30.jpg)
Shared buffer
insertPtrremovePtrRemove pizza
Chef (Producer) Waiter (Consumer)
![Page 31: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/31.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 32: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/32.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 33: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/33.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 34: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/34.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 35: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/35.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 36: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/36.jpg)
Shared buffer
insertPtr
removePtr
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 37: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/37.jpg)
Shared buffer
insertPtrremovePtr
Buffer empty: Consumer must be blocked!
Remove pizza
Chef (Producer) Waiter (Consumer)
![Page 38: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/38.jpg)
Designing a solutionChef (Producer) Waiter (Consumer)
Wait for empty slotInsert itemSignal item arrival
Wait for item arrivalRemove item
Signal empty slot available
What synchronization do we need?
![Page 39: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/39.jpg)
Designing a solutionChef (Producer) Waiter (Consumer)
Wait for empty slotInsert itemSignal item arrival
Wait for item arrivalRemove item
Signal empty slot available
What synchronization do we need?
Mutex(shared buffer)
![Page 40: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/40.jpg)
Designing a solutionChef (Producer) Waiter (Consumer)
Wait for empty slotInsert itemSignal item arrival
Wait for item arrivalRemove item
Signal empty slot available
What synchronization do we need?
Semaphore(# empty slots)
![Page 41: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/41.jpg)
Designing a solutionChef (Producer) Waiter (Consumer)
Wait for empty slotInsert itemSignal item arrival
Wait for item arrivalRemove item
Signal empty slot available
What synchronization do we need?
Semaphore(# filled slots)
![Page 42: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/42.jpg)
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
![Page 43: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/43.jpg)
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
![Page 44: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/44.jpg)
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
![Page 45: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/45.jpg)
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
![Page 46: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/46.jpg)
Readers-Writers Problem
![Page 47: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/47.jpg)
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
![Page 48: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/48.jpg)
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
![Page 49: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/49.jpg)
(full codeonline)
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
![Page 50: CIS 5512 - Operating Systems Synchronization Professor Qiang Zeng Fall 2015.](https://reader030.fdocuments.us/reader030/viewer/2022033104/5697c0051a28abf838cc5033/html5/thumbnails/50.jpg)
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