CS330 - Operating Systems
Critical Section, Semaphores, Classical Problems
Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.
Processes can execute concurrently and may be interrupted at any time, partially completing execution. Concurrent access to shared data may result in race conditions.
A situation where several processes access and manipulate the same data concurrently, and the outcome depends on the particular order in which access takes place.
Suppose counter = 5
Producer executes: counter++
Consumer executes: counter--
Result could be: 4, 5, or 6!
The Critical Section (CS) is the part of process code that manipulates shared data or resources. Execution of the CS should be mutually exclusive.
All three requirements must be satisfied for a correct solution to the critical section problem!
Assumes that load and store instructions are atomic. Uses two shared variables:
int turn; - Indicates whose turn it is to enter critical sectionboolean flag[2]; - Indicates if a process is ready to enter critical sectionMany systems provide hardware support for critical section code. Modern machines provide special atomic hardware instructions.
Previous solutions are complicated for application programmers. OS designers build software tools to solve critical section problem. Simplest is mutex lock.
This implementation requires busy waiting. While a process is in its critical section, any other process trying to enter must loop continuously (spin). This wastes CPU cycles.
A synchronization tool that provides more sophisticated ways for processes to synchronize their activities.
Semaphore S is an integer variable accessed only through two atomic operations:
S--;
if (S < 0)
block();
S++;
if (S <= 0)
wakeup();
| Type | Range | Usage |
|---|---|---|
| Binary Semaphore | 0 or 1 | Mutual exclusion (mutex) |
| Counting Semaphore | Unrestricted domain | Resource allocation with multiple instances |
Semaphores Used:
mutex - initialized to 1 (mutual exclusion)full - initialized to 0 (number of full buffers)empty - initialized to n (number of empty buffers)A data set is shared among concurrent processes:
Problem: Allow multiple readers to read simultaneously. Only one writer can access shared data at a time.
Shared Data:
rw_mutex - initialized to 1 (common to readers/writers)mutex - initialized to 1 (protects read_count)read_count - initialized to 0 (number of readers)[Insert diagram: 5 philosophers around table with rice bowl and chopsticks]
Problem Description:
Shared Data
↓
Operations/Procedures
↓
Initialization Code
↓
Entry Queue
A high-level abstraction that provides a convenient and effective mechanism for process synchronization. Only one process may be active within the monitor at a time.
Two or more processes waiting indefinitely for an event that can be caused only by one of the waiting processes.
P0 waits for Q (held by P1), P1 waits for S (held by P0) → DEADLOCK!
Indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.
A high-priority process is indirectly preempted by a lower-priority process that holds a lock needed by the high-priority process.
Solution: Priority-inheritance protocol
Q1. A situation where several processes access the same data concurrently and the outcome depends on the order of access is called:
Q2. What is the correct order of operations for protecting a critical section using a binary semaphore?
Q3. Mutual exclusion can be provided by:
Q4. Which of these does NOT satisfy the critical section requirements?
Q5. The bounded buffer problem is also known as:
Q6. In the dining philosophers problem, deadlock occurs when:
Q7. Given two concurrent programs that use semaphores S1 (init=1) and S2 (init=0):
What will be the output pattern?
P1 will be blocked initially on wait(S2). P2 executes wait(S1), prints "B", then signals S2. P1 can now proceed, prints "A", signals S1. Pattern: BABA...
Q8. A shared variable mutex is initialized to 1. Each process executes wait(mutex) before entering CS and signal(mutex) after. What happens if a process replaces signal(mutex) with wait(mutex)?
Deadlock will occur. After the first process enters and exits its CS, it will execute wait(mutex) again, making mutex = -1. No process can enter CS again.
Q9. What are the three requirements for a solution to the critical section problem?
| Term | Definition |
|---|---|
| Race Condition | Outcome depends on order of process execution |
| Critical Section | Code segment that accesses shared data |
| Mutual Exclusion | Only one process in CS at a time |
| Semaphore | Integer variable with atomic wait/signal operations |
| Mutex | Binary semaphore for mutual exclusion |
| Spinlock | Lock that uses busy waiting |
| Deadlock | Processes waiting indefinitely for each other |
| Starvation | Process never gets resources |
| Monitor | High-level synchronization construct |
| Atomic Operation | Operation that completes without interruption |
| Busy Waiting | Continuously testing a condition |
| Priority Inversion | Low priority process blocks high priority |
[Insert diagram showing concurrent access to shared counter]
[Insert diagram from Chapter_6.pdf showing 5 philosophers with chopsticks]
[Insert monitor schematic diagram]
[Insert bounded buffer visualization]
Note: Please provide the actual diagrams from your Chapter_6.pdf slides to replace these placeholders.