Operating Systems Concepts
1. Interleaving
Interleaving refers to executing multiple processes by switching between them so quickly
that they appear to run simultaneously (on a single-core system).
2. Overlapping
Overlapping is true parallelism where different processes are executed simultaneously on
different processors or cores.
3. Difficulties in Concurrency
- Race Conditions
- Deadlocks
- Starvation
- Complex Debugging
- Resource Contention
4. Main Control Problems
a. Need for Mutual Exclusion: Ensures only one process enters its critical section at a time.
b. Deadlock: A cycle where each process is waiting for a resource held by another.
c. Starvation: A process waits indefinitely due to continuous preference given to others.
5. CAS (Compare-And-Swap Instruction)
An atomic operation that compares a memory value and swaps it only if the comparison
succeeds.
6. CAS and Mutual Exclusion
CAS enables mutual exclusion by allowing threads to safely update shared data without
locks.
7. Exchange (XCHG) and Race Conditions
XCHG swaps data atomically, but poor design can still lead to race conditions.
8. Mutual Exclusion Using Hardware Support
Advantages:
- Fast and efficient
- Simple atomic operations
Disadvantages:
- Busy waiting wastes CPU time
- Doesn't scale well on multi-core systems.
9. Semaphore in a Critical Section
Semaphores manage access to a critical section using 'wait' and 'signal' operations to ensure
mutual exclusion.
10. Mutual Exclusion Using Semaphore
Binary Semaphore:
wait(mutex);
// critical section
signal(mutex);
11. Producer–Consumer Problem (Short)
Producer generates data and puts it in a buffer, consumer takes from the buffer. Needs
synchronization to avoid conflicts.
12. Message Passing
Processes communicate and synchronize by sending and receiving messages rather than
shared memory.
13. Deadlock - Prevention and Avoidance
Prevention: Break one of the four necessary conditions.
Avoidance: Banker’s Algorithm ensures safe state before allocating resources.
14. Banker’s Algorithm
Allocates resources only if the system remains in a safe state, preventing deadlocks.
Example:
Process | Max | Allocated | Need
P0 | 7 | 2 |5
P1 | 3 | 2 |1
P2 | 9 | 3 |6
If Available = 3, the system chooses P1 first, releases resources, and continues safely.
15. Dining Philosophers Problem (Short)
Five philosophers share five forks. Each must pick two forks to eat, risking deadlock if all
pick the same side fork first. Shows resource allocation and deadlock issues.