Synchronization Items Flashcards
What are synchronization problems?
Programing problems that arise when you create multithreaded code or a program that exchanges data with other programs.
What synchronization hazards do you know?
Race condition and deadlock.
What is a shared resource?
A shared resource is a resource that can be accessed by multiple threads or processes simultaneously. Examples include variables, data structures, files, and hardware devices. Access to shared resources must be carefully managed to prevent conflicts and ensure data consistency.
What is a race condition (a.k.a. data race)?
A race condition occurs when two computer program processes, or threads, attempt to access the same resource at the same time and cause problems in the system.
A race condition occurs when the behavior and outcome of a program depend on the relative timing of events, such as the order in which threads or processes execute. This can lead to unpredictable and erroneous results if two or more threads modify shared data concurrently without proper synchronization.
Explain how a race condition can happen.
At the assembly/CPU level, a race condition can happen when two or more threads or processes execute instructions that access and modify the same memory location simultaneously. For example, consider two threads incrementing a shared variable:
Thread 1 reads the variable's value into a register. Thread 2 reads the same variable's value into a register. Thread 1 increments the register's value and writes it back to memory. Thread 2 increments the (old) register's value and writes it back to memory. As a result, the variable is only incremented once, even though both threads attempted to increment it.
Can a race condition happen if only one party modifies the data and others are just reading? Can a race condition happen if everybody is just reading?
A race condition can happen if only one party modifies the data and others are just reading, as readers might get inconsistent or partially updated data. However, if everybody is just reading, a race condition cannot happen because reading does not change the state of the shared resource, so there are no conflicting updates.
What are the three properties of a good solution to the critical section problem?
Mutual Exclusion: Ensures that only one process can be in the critical section at a time.
Progress: If no process is in the critical section, and some processes wish to enter, one of the waiting processes must be allowed to enter.
Bounded Waiting: Guarantees that each process will get a chance to enter the critical section within a bounded number of turns.
What is the critical section problem?
The critical section problem involves ensuring that when multiple threads or processes access shared resources, at most one is executing in the critical section at any given time. A critical section in computer programming is a part of a multi-process program that must not be concurrently executed by more than one process.
Explain the idea of locking shared resources.
Locking shared resources involves using synchronization mechanisms, such as locks, to control access to shared resources. A lock can be acquired by a thread before entering the critical section and released after exiting it, ensuring that only one thread can access the shared resource at a time.
What does it mean to have an atomic function?
An atomic function is an operation that is indivisible, meaning it is executed completely without interruption. Two parts of the definition are:
It is performed as a single, uninterruptible step. No other operations can observe the function in a partially completed state.
What does it mean if the function is hardware implemented?
If a function is hardware implemented, it means there is no program code for it, and no general CPU instructions are written in memory. Instead, the function is implemented with hardware circuitry and called with a single special CPU instruction. This often makes the function faster and ensures atomicity at the hardware level.
What is test_and_set instruction and what does it do?
The test_and_set instruction is an atomic operation used in synchronization to achieve mutual exclusion. It tests the value of a variable and sets it to a new value (usually true) in one indivisible operation. If the variable was previously false, it returns false and sets the variable to true. If it was already true, it returns true.
Simple solution of the critical section problem using test_and_set instruction. What is suboptimal in this solution?
A simple solution using test_and_set involves each process repeatedly calling test_and_set in a loop (busy waiting) until it successfully enters the critical section.
Suboptimal Aspect: It doesn’t have the “bounded waiting” property, meaning it is not starvation-free. A process may wait indefinitely if other processes repeatedly access the critical section.
Solution of the critical section problem using test_and_set instruction with the “bounded waiting” property.
A solution with bounded waiting can be implemented using additional variables to ensure that each process gets a chance to enter the critical section. One common approach is Peterson’s algorithm, which uses two shared variables (flags and turn) to achieve mutual exclusion with bounded waiting.
Here’s a conceptual implementation in pseudocode:
cpp
bool flag[n]; // Array of flags for each process
int turn; // Shared variable indicating whose turn it is
void enter_critical_section(int i) {
flag[i] = true;
turn = i;
while (flag[(i + 1) % n] && turn == i) { /* busy wait */ }
}
void exit_critical_section(int i) {
flag[i] = false;
}
What does busy waiting mean? Why is it inefficient? When is it still good?
Busy Waiting: A synchronization technique where a process repeatedly checks a condition in a loop until it becomes true.
Inefficient: It consumes CPU cycles unnecessarily, as the process does nothing useful while waiting.
Still Good: When expected wait times are short and the overhead of performing a context switch to another process is higher than the cost of busy waiting.