semaphores and condition variables
TRANSCRIPT
![Page 1: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/1.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphores andCondition VariablesModern Operating Systems, by Andrew Tanenbaum
Operating Systems: Three Easy Pieces (a.k.a. the OSTEP book)
1
Chap 2.3 & 6
Chap 30&31
![Page 2: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/2.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore
ā¢ A fundamental synchronization primitive used for:ā¢ Locking critical regions
ā¢ Inter-process synchronization
ā¢ A special integer, "sem", on which only two operations can be performedā¢ A "DOWN(sem)" operation
ā¢ An "UP(sem)" operation
2
![Page 3: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/3.jpg)
Binghamton
University
CS-550
Spring 2020
The DOWN(sem) Operation
ā¢ If (sem>0)ā¦ā¢ Subtract 1 from sem
ā¢ Continue processing (this is a "successful") down operation
ā¢ while (sem==0) ā¦ā¢ Block the caller until sem>0 (someone else did an "UP(sem)")
ā¢ try DOWN(sem) again.. if successful, break ā¢ Might fail because other processes might do a DOWN first
3
![Page 4: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/4.jpg)
Binghamton
University
CS-550
Spring 2020
The UP(sem) Operation
ā¢ If (sem==0) ā¢ add 1 to sem
ā¢ wake up all processes blocked on DOWN(sem)
ā¢ All woken up processes compete to perform DOWN(sem)ā¢ Only one can succeedā¦ the rest are blocked again
ā¢ elseā¢ add 1 to sem
4
![Page 5: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/5.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
5
sem=3
![Page 6: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/6.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
6
sem=2
Down(sem)
![Page 7: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/7.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
7
sem=1
Down(sem)
![Page 8: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/8.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
8
sem=0
Down(sem)
![Page 9: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/9.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
9
sem=0
Down(sem)
B
l
o
c
k
e
d
![Page 10: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/10.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
10
sem=0
B
l
o
c
k
e
d
Down(sem)Down(sem)
![Page 11: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/11.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
11
sem=1
Up(sem)
B
l
o
c
k
e
d
Down(sem)Down(sem)
![Page 12: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/12.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
12
sem=0
B
l
o
c
k
e
d
Down(sem)
![Page 13: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/13.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
13
sem=2
B
l
o
c
k
e
d
Down(sem)
Up(sem)Up(sem)
![Page 14: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/14.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphore Example ā "Chair is taken"
14
sem=1
![Page 15: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/15.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex
ā¢ A binary semaphore ā Can have a value of 0 (blocking) or 1 (open)
ā¢ Used as a lock around critical sections
ā¢ To enter a critical section (lock a resource) use "Down(mutex)"ā¢ If mutex==1 (open), decrement mutex value to 0 and continue
ā¢ If mutex==0 (blocking) blocks until someone else does an "Up(mutex)"
ā¢ To exit a critical section (unlock resource) use "Up(mutex)"ā¢ Increment mutex value to 1
ā¢ Wake up all sleepers on Down(mutex) ā one gets the lock, the others go back to sleep
15
![Page 16: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/16.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex Example ā "Chair is taken"
16
mutex=1
![Page 17: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/17.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex Example ā "Chair is taken"
17
mutex=0
Down(mutex)
![Page 18: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/18.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex Example ā "Chair is taken"
18
mutex=0
Down(mutex)
B
l
o
c
k
e
d
![Page 19: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/19.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex Example ā "Chair is taken"
19
mutex=1
Down(mutex)
B
l
o
c
k
e
d
Up(mutex)
![Page 20: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/20.jpg)
Binghamton
University
CS-550
Spring 2020
Mutex Example ā "Chair is taken"
20
mutex=0
![Page 21: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/21.jpg)
Binghamton
University
CS-550
Spring 2020
Example: Producer/Consumer Problem
ā¢ Producers and Consumers runs concurrentlyā¢ Producers add items to a common buffer
ā¢ Consumers take items from a common buffer
21
Common Buffer
Used slots
Available slots
Producer
Producer
Consumer
Consumer
Consumer
![Page 22: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/22.jpg)
Binghamton
University
CS-550
Spring 2020
Producer/Consumer - Coordination
ā¢ Consumers should sleep when buffer is empty
ā¢ Producers should sleep when buffer is full
ā¢ No two processes should work on the buffer at the same time
22
Producer
Producer
Consumer
Consumer
Consumer
![Page 23: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/23.jpg)
Binghamton
University
CS-550
Spring 2020
Using Semaphores for Prod/Cons
#define N 100 // Number of slots in the common buffer
typedef int semaphore; // Semaphore is a special kind of int
semaphore mutex=1; // Control access to the common buffer
semaphore avail=N; // The number of available slots in buffer
semaphore used=0; // The number of used slots in buffer
23
Note: Two types of semaphores used hereā¦ā¢ A binary semaphore (mutex) to lock the bufferā¢ Regular semaphores to count avail and used slots
![Page 24: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/24.jpg)
Binghamton
University
CS-550
Spring 2020
unblock producers
Critical Section
block when used=0block when avail=0
Example producer/consumer code
void producer(void) {
int item;
while(TRUE) {
item=produce_item();
down(&avail);
down(&mutex);
insert_item(item);
up(&mutex);
up(&used);
}
}
void consumer(void) {
int item;
while(TRUE) {
down(&used);
down(&mutex);
item=remove_item();
up(&mutex);
up(&avail);
consume_item(item);
}
}
24
Critical Sectionunblock consumers
![Page 25: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/25.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphores ā POSIX interface
ā¢ sem_open() ā Optionally creates and/or connects to a named semaphore
ā¢ sem_init() ā Initializes an un-named semaphore in (shared) memory
ā¢ sem_wait() ā blocks while semaphore is held by other processes, then decrements (down)
ā¢ sem_trywait() ā returns an error if semaphore is held by other processes
ā¢ sem_post() ā Increments the count of the semaphore (up)
ā¢ sem_getvalue() ā Returns the current value of the semaphore
ā¢ sem_close() ā Ends the connection to a named semaphore
ā¢ sem_unlink() ā Ends the connection to a named semaphore and removes it when all connections are ended
ā¢ sem_destroy() ā Cleans up an unnamed semaphore
25
![Page 26: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/26.jpg)
Binghamton
University
CS-550
Spring 2020
Semaphores ā System V (older) interface
ā¢ semget(key, nsems, semflg) ā creation (sem value=0)
ā¢ semctl(semid,0,SETVAL,arg) ā Initializationā¢ union semun arg; arg.val=1;
ā¢ Not atomic w/ creation
ā¢ semop(semid, &sops, nsops) - Incr/Decr/Test-and-Setā¢ struct sembuf sops;
ā¢ semctl(semid,0,IPC_RMID, 0) - Deletion
26
![Page 27: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/27.jpg)
Binghamton
University
CS-550
Spring 2020
Monitors with Condition Variables
ā¢ Object oriented view of contention used for thread-safe objects
ā¢ Invented by Per Brinch Hansen and C.A.R. Hoare for Concurrent Pascal
ā¢ Monitor: A collection of critical section functions that operate on shared resources
ā¢ One global lock for all proceduresā¦ only one procedure can be executing at any given time
ā¢ One or more "condition variables", c
ā¢ wait(c) ā Releases the global lock, and puts the calling function to sleep. Does not return until it re-acquires the monitor lock
ā¢ signal(c) ā Wakes all functions waiting on c, who then compete for the monitor lock
27
![Page 28: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/28.jpg)
Binghamton
University
CS-550
Spring 2020
Monitor Function State Graph
28
Executing
AsleepCompeting signal(c)
signal(c)
invoked
finished
![Page 29: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/29.jpg)
Binghamton
University
CS-550
Spring 2020
Example Monitor: Producer/Consumer
procedure producer;
begin
while true do
begin
item=produce_item;
PCmon.insert(item)
end
end;
procedure consumer;
begin
while true do
begin
item=Pcmon.remove;
consume_item(item)
end
end;
monitor Pcmon
condition full, empty;
integer count;
procedure insert(item: integer);
begin
if count = N then wait(full);
insert_item(item);
count:=count+1;
if count=1 then signal(empty)
end;
function remove: integer;
begin
if count=0 then wait(empty)
remove = remove_item;
count := count ā 1;
if count = N-1 then signal(full)
end;
count =: 0;
end monitor;
29
![Page 30: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/30.jpg)
Binghamton
University
CS-550
Spring 2020
Atomic Locking ā Test and Set Lock
ā¢ Hardware building block for locks
ā¢ TSL Instruction: TSL Register,Lockā¢ Lock ā Address in memory with a value of 0 or 1ā¢ Register ā One of the CPU General Purpose Registers
ā¢ The TSL instruction does TWO operations atomically (as one)1. Register := Lock; // Copy value of Lock to Register
2. Lock := 1; // Set the Lock value to 1
ā¢ Atomic: Entire instruction must complete without pre-emption
30
![Page 31: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/31.jpg)
Binghamton
University
CS-550
Spring 2020
Implementing Mutex using TSL
mutex_lock:
TSL REG,MUTEX
CMP REG,#0
JZE ok
CALL thread_yield
JMP mutex_lock
ok: RET
mutex_unlock:
MOVE MUTEX,#0
RET
31
Set mutex to 1 (locked) if it wasn't already
If it wasn't locked, good to go
If it was locked, let another thread work
ā¦and try again when scheduler runs you again
In C Syntax:void lock(boolean *mutex) {
while(test_and_set(lock)==true) {};
}
void unlock(boolean *mutex) {
mutex=0;
}
![Page 32: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/32.jpg)
Binghamton
University
CS-550
Spring 2020
Compare and Set (CAS)
ā¢ Atomic Operation useful for "lock-free" synchronization
bool compare_and_set(target,old,new) {
if (target!=old) return false; // somebody touched target
target=new; // Nobody touched targetā¦ OK to update
return true;
}
32
Note: This is atomic!Ref : https://en.wikipedia.org/wiki/Compare-and-swap
![Page 33: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/33.jpg)
Binghamton
University
CS-550
Spring 2020
Example CAS: race-free adder
int add(int *counter, int increment) {
int old; int new;
do {
old=*counter;
new=old+increment;
} while (compare_and_swap(counter,old,new)==false);
return new;
}
33
Note: These do not need to be atomic!
![Page 34: Semaphores and Condition Variables](https://reader033.vdocument.in/reader033/viewer/2022041506/62524d5ed4e3421c94118c82/html5/thumbnails/34.jpg)
Binghamton
University
CS-550
Spring 2020
Compare and Swap Instruction
ā¢ Alternate x86 Instruction for locking
ā¢ Instruction Format: CMPXCHG NEWVAL,TARGETā¢ NEWVAL ā Any CPU General Purpose Registerā¢ TARGET ā Memory location or Registerā¢ EAX implicit operand ā Contains the "compare to" (old) value of TARGETā¢ EAX result contains the actual old value of TARGETā¢ EFLAGS.ZF ā indicates if exchange was successful
if (%EAX==TARGET) then { EFLAGS.ZF:=1; TARGET:=NEWVAL }
else { EFLAGS.ZF:=0; %EAX:=TARGET; }
34