MODULE-3
MODULE-3
OPERTING SYSTEMS
MODULE-III
Abraham Silberschatz, Peter Baer Galvin, Greg Gagne, ' Operating System Concepts' 9th Edition, Wiley
India 2015
Process synchronization and Dead locks
3.1 Process synchronization, Race conditions
3.2 Critical Section problem, Peterson’s solution
3.3 Synchronization hardware, Mutex Locks
3.4 Semaphores
3.5 Monitors
3.6 Synchronization problem examples
3.7 Deadlocks: Necessary conditions, Resource Allocation Graphs
3.8 Deadlock prevention
3.9 Deadlock avoidance
3.10 Banker’s algorithm
3.11 Deadlock detection
3.12 Deadlock recovery
Process Synchronization
Imagine two people trying to withdraw money from a shared bank
account simultaneously using ATMs.
If both withdraw at the same time, the account balance could be
updated incorrectly.
Process synchronization is like a mechanism ensuring that one
person completes their transaction before the other begins.
• Process synchronization ensures that multiple processes can execute
concurrently without interference, maintaining data consistency.
• Mutual Exclusion: Only one process can access a critical section at a time.
• Progress: Processes not in the critical section should not block others from
entering.
• Bounded Waiting: There is a limit on how long a process waits to enter the
critical section.
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
register1 = counter
register1 = register1 + 1
counter = register1
• Decides to add one more candy based on the count they see.
• Both check the initial count (e.g., 5 candies) at the same time.
• Add their candy and assume the new count is 6 (ignoring the other's addition).
• Write the same count (6) on the notepad, even though two candies were added, and the total should
be 7.
• The result? The jar physically has 7 candies, but the notepad incorrectly shows 6, because the
actions of the two people overlapped.
• This illustrates a race condition: the final result depends on the timing of their actions, and without
proper coordination, the outcome is incorrect.
race condition
• A race condition occurs when multiple threads or processes
access and manipulate shared data concurrently, and the
outcome of the execution depends on the order in which these
accesses take place.
• Mutex Locks
• Semaphores
• Monitors
Critical Section Problem
• The Critical Section Problem arises when multiple processes or
threads access shared resources (such as variables, files, or
databases) concurrently, and their execution results depend on
the order of execution.
• Mutual Exclusion: Only one process can access the critical section at
a time.
• If there’s no car on the bridge, any car waiting at either side should be
allowed to cross (Progress).
• If multiple cars are waiting, each car will eventually get a turn without
being indefinitely delayed, assuming drivers take turns (Bounded Waiting).
• If drivers do not follow these rules, cars might collide on the bridge (Race
Condition) or one side might monopolize the bridge, leaving the other side
stuck forever (Starvation).
• This highlights the need for mechanisms to prevent chaos when shared
resources (like the bridge) are accessed by multiple entities (cars).
Solution to the Critical Section Problem
1. Entry Section: This ensures they don’t fight over the toy, and they
take turns fairly.
Set flag[i] = true (indicating intent to enter).
Set turn = j (allowing the other process to go first if it wants).
Wait until flag[j] == false or turn == i.
2. Critical Section:
3. Exit Section:
3.Processor-level Instructions:
•Many modern processors include dedicated instructions (e.g., LOCK
prefix in x86 architectures) to perform atomic operations.
Benefits of Atomic Instructions
• Avoid Race Conditions:
• Prevents multiple threads from simultaneously modifying shared
variables.
• Improved Performance:
• Hardware-level atomic instructions are faster than software locks (e.g.,
mutexes or semaphores).
• Foundation for Synchronization Primitives:
• Atomic instructions are the building blocks for locks, semaphores, and
other synchronization mechanisms.
Limitations of Atomic Instructions
• Limited Scope:
• Atomic instructions typically work on small data types (e.g., integers,
pointers) and not on larger, complex structures.
• Busy Waiting:
• When used improperly (e.g., in spinlocks), they can cause threads to
waste CPU cycles.
• Hardware Dependency:
• Atomic instructions rely on specific hardware support, which may not be
available on all systems
Synchronization Hardware
• Many systems provide hardware support for implementing the critical
section code.
• All solutions are based on idea of locking
• Protecting critical regions via locks
do
{
acquire lock
critical section
release lock
remainder section
} while (TRUE);
synchronization hardware mechanisms
• Test-and-Set instruction
• Compare-and-Swap instruction
Imagine a hotel room with a door that has a mechanical "occupied" or "vacant"
sign.
The Test-and-Set instruction is like checking the sign on the door:
If the sign says "vacant," you flip it to "occupied" and enter the room (critical
section).
If the sign says "occupied," you must wait until it becomes vacant again.
If it matches, the machine dispenses a snack (updates the state). If not, you try
again.
1. Test-and-Set Instruction
The Test-and-Set (TAS) instruction is a hardware
instruction that atomically performs the following:
• If TestAndSet() returns false, it means the lock was free, and the
process can enter the critical section.
• Mutex Lock: There is one key to the bathroom door. Whoever holds the key can enter and use the
bathroom.
• Locking (pthread_mutex_lock): When you take the key, you lock the bathroom door. This ensures
no one else can enter while you're inside.
• Critical Section: Being inside the bathroom is like being in the critical section. You're using a
shared resource (the bathroom), and no one else can use it until you're done.
• Unlocking (pthread_mutex_unlock): When you're done, you return the key, unlocking the
bathroom for the next person.
• Waiting (Blocked Threads): If someone else needs the bathroom while you're inside, they have to
wait outside until you finish and return the key.
• Fairness: The person who has been waiting the longest will get the key next (first-come, first-served)
How This Relates to Threads