Process/Thread Synchronization
Processes
IPC, shared files, …
Threads
Shared memory communication
Race Condition
Outcome of thread execution depends on timing of threads
Synchronization
Use of atomic (i.e.: uninterruptible) operations to ensure correct cooperation amongst threads
Mutual Exclusion
Critical Section
-Typically: Code that reads, writes shared variables, files,…
Lock
-Construct that prevents someone from doing something
e. g.: 1. Lock before entering critical section
2. Unlock when leaving critical section
- wait if locked
Starvation
-Occurs when 1 or more threads never gets access to critical section
Critical Sections Problem
-Ensure that only one thread executes its critical section at a time (avoid race condition)
Producer-Consumer Problem
Producer produces objects into shared buffer
• Consumer consumes objects from shared buffer
• Producer, consumer run as separate threads
• Must ensure:
1. Consumer doesn’t remove from empty buffer
2. Producer doesn’t insert into full buffer (bounded-buffer only)
Critical Sections Problem Goals of Solution