Module 3: Semaphores & Mutex Flashcards
Locks and keys
- Lock blocks other threads from accessing a resource
- Only one thread has access to it
- Other threads need to wait
Busy waiting / spinning
- Sync technique
- Thread waits for a resource to become available =>Spins in a loop.
- Repeatedly cheks for a condition to be true
- Wastes CPU cycles = less efficient than semaphores/mutex
Semaphore type
- Counter, but not an int
- Keeps track of how many of a resource are available at a given time
- When thread wants semaphore: counter value decreases–
- Value is negative = threads must wait til 0 or positive
- Thread releases semaphore = value increases++
Structure of Semaphores
- Variable that controls access to common resources by multiple threads
- Avoids critical section problems
- Allows / disallows access to resource (depends on availability of the resource)
- Thread waiting => signaled by other thread releasing the resource
Semaphore Attribute: Value (int)
- int
- accessed thru methods P and V
- identifies how many threads are in CS at same time
- change during execution to see how many threads from blocked queue can enter CS
Semaphore Attribute: Queue
- each semaphore has associated queue of blocked threads
- not accessible for programmer
Semaphore Method: Initialize
- nr of available units of a resource, s
- Initialize(4) => 4 threads can enter CS at the same time
Semaphore Method: P (or Wait, down)
- called by thread that wants to enter CS
- (s–) semaphore value down
- s = 0, thread blocks (enters queue) until s > 0
- s < 0, thread waits (no more resources)
Semaphore Method: V (or Signal, up)
- called by thread to exit CS
- increment value (s++)
- if threads blocked for the semaphore = thread unblocks
P and V in C#
- P = WaitOne
- V = Release
P as a method
- semaphore calls P() atomically
- > 0, decrease by 1
- else, thread blocks self (waiting for resource)
- signals OS that a thread needs a resource. If not available = wait.
semProducer.WaitOne(); called in method Produce
V as a method
- semaphore calls v() atomically
- increment by 1 (no blocking)
- signals Os that a resource is free for other threads
semProducer.Release(); called in method Consume
Creating a Semaphore
Semaphore semProducer; // counts empty slots to write
Semaphore semConsumer; //counts full slots to read & remove
semProducer = new Semaphore(bufferSize, bufferSize) //initial & max units
Semaphore signal history
- no thread is waiting = signal is remembered when P is called next time
- semaphore remembers which thread released the resource
- sema = not owned by a thread. One thread can call P & another V of the same sema.
- sema = object, but doesn’t follow OOP. Any thread can call P and V on another thread.
Use of semaphores
- Sync access to shared resource (esp useful when of the same type)
- For each type fo shared resources, a seperate semaphore can be used
- Ex: one guards writers, one guards readers
Semaphores in CS
Non-critical code
Get Semaphore obj - Wait
Critical code
Release Semaphore obj - Signal
Non-critical code
Several semaphores in a code block
- Several sema can exist in an execution. Associate thread with correct semaphore when calling P/V
- Sema A = empty slots in a bounded buffer
- Sema B = filled slots in a bounded buffer
- Mutexes used to give access to section of code that can’t be executed concurrently by more than one thread