Lecture 10: Synchronization 1 Flashcards
Process/Thread Synchronization
- For processes, threads to communicate, require shared data access
- Shared data access in unpredictable order determined by scheduler can lead to data inconsistencies and undesirable process / thread behaviors. Synchronization prevents that.
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
- Ensuring that only one thread does a particular thing
(e. g.: Go to the store and buy milk) at a time - one thread’s execution excludes the other
Critical Section
- Piece of code that only one thread can execute at any time
(i. e.: Only 1 thread at a time gets into this code)
-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
- Mutual exclusion
no more than 1 process in its critical section at a time - Progress: No thread outside of its critical section should block other threads
- Bounded waiting: Must be a bound on number of times other threads allowed to enter their critical sections after thread has made request to enter its critical section and before request granted
• assume each thread executes at non-zero speed