0% found this document useful (0 votes)
3 views10 pages

Synchronization

Synchronization is crucial in operating systems to coordinate multiple processes or threads accessing shared resources, preventing conflicts and ensuring system stability. Techniques such as semaphores and mutexes are employed to manage access to these resources, addressing issues like race conditions, deadlocks, and starvation. The critical section problem outlines the need for mutual exclusion, progress, and bounded waiting to effectively manage concurrent resource access.

Uploaded by

Ishtiaq Malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Synchronization

Synchronization is crucial in operating systems to coordinate multiple processes or threads accessing shared resources, preventing conflicts and ensuring system stability. Techniques such as semaphores and mutexes are employed to manage access to these resources, addressing issues like race conditions, deadlocks, and starvation. The critical section problem outlines the need for mutual exclusion, progress, and bounded waiting to effectively manage concurrent resource access.

Uploaded by

Ishtiaq Malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Synchronization

Synchronization is one of the most important concepts in an Operating System. In a multitasking


environment, multiple processes or threads may execute simultaneously and share common
resources such as memory, files, databases, printers, and hardware devices.

When several processes attempt to access the same resource at the same time, conflicts may occur,
leading to incorrect results and system instability. Synchronization provides mechanisms to
coordinate process execution and ensure that shared resources are accessed safely and efficiently.

Definition

Synchronization is the process of coordinating multiple processes or threads so that they execute
in a controlled manner when accessing shared resources.

Why Synchronization is Needed


Modern operating systems support:

• Multitasking
• Multiprocessing
• Multithreading

In these environments, several processes may run concurrently.

Consider a bank account system:

Example

Account Balance = $1000

Two processes execute simultaneously:

Process A

Withdraws $200

Process B

Withdraws $300

Without synchronization:

1. Process A reads balance = $1000


2. Process B reads balance = $1000
3. Process A updates balance = $800
4. Process B updates balance = $700

Final Balance = $700

Correct Balance should be:

1000 − 200 − 300 = $500

This incorrect result occurs because both processes accessed the shared data
simultaneously.

Synchronization prevents such problems.

Shared Resources
A shared resource is any resource that can be accessed by multiple processes or threads.

Examples:

• Shared Memory
• Files
• Printers
• Databases
• Network Connections
• Variables

When multiple processes access shared resources, synchronization becomes necessary.

Synchronization Techniques
Several synchronization techniques are used in operating systems.

Major techniques include:

1. Semaphores
2. Mutexes
3. Monitors
4. Spinlocks
5. Condition Variables

This chapter mainly focuses on:


• Semaphores
• Mutexes

1. Semaphores
A semaphore is an integer variable used to control access to shared resources.
It can only be modified through special operations.

Semaphore Operations
Two atomic operations are used:

Wait (P)

Also called:

• Down()
• Acquire()

Signal (V)

Also called:

• Up()
• Release()

Types of Semaphores
There are two major types.

A. Binary Semaphore
B. Counting Semaphore

2. Mutex
Mutex stands for:
Mutual Exclusion
A mutex is a locking mechanism used to protect shared resources.
Only one thread or process can own the mutex at a time.

How Mutex Works


Before entering a critical section:
lock()

After leaving:

unlock()

Example
lock(mutex);

/* Critical Section */

unlock(mutex);

If another thread attempts to acquire the mutex while it is locked, it must wait.

Mutex Life Cycle


Step 1
Mutex is unlocked.
Available
Step 2
Thread T1 acquires lock.
Locked
Step 3
Thread T2 requests lock.
Waiting
Step 4
T1 releases lock.
Unlocked
Step 5
T2 acquires lock.
Locked

Real-Life Example of Mutex


Consider a printer.

Many users send print requests.

Only one print job can use the printer at a time.

Mutex acts like a key:

• User A gets the key.


• Others wait.
• After finishing, User A returns the key.
• Next user gets access.

Difference Between Semaphore and Mutex


Feature Semaphore Mutex

Definition Signaling mechanism Locking mechanism

Value Integer variable Binary lock

Ownership No ownership Ownership exists

Resource Count Single or multiple resources Single resource

Operations Wait(), Signal() Lock(), Unlock()

Complexity More complex Simpler

Use Case Resource management Mutual exclusion

Applications of Synchronization
Synchronization is used in:

Operating Systems

• Process scheduling
• Resource allocation

Databases

• Transaction management

Banking Systems

• Account updates

File Systems

• Concurrent file access

Web Servers
• Handling multiple requests

Multithreaded Applications

• Shared memory management

Critical Section Problem


Modern operating systems support multitasking, multithreading, and multiprocessing, where
multiple processes or threads execute simultaneously. These processes often share resources such
as memory, files, databases, printers, and network devices.

When multiple processes access shared resources concurrently, several problems may arise,
including:

• Race Conditions
• Deadlocks
• Starvation
• Data Corruption

To prevent these issues, operating systems use synchronization mechanisms. The foundation of
synchronization is the Critical Section Problem.

Structure of a Process
Every process that uses shared resources can be divided into four sections:

• Entry Section

• Critical Section

• Exit Section

• Remainder Section

A. Entry Section
Contains code that requests permission to enter the critical section.
Purpose
• Check resource availability.
• Obtain lock or synchronization object.
B. Critical Section
Contains code that accesses shared resources.
Examples
• Updating bank account balance.
• Writing to a file.
• Accessing shared memory.

C. Exit Section
Contains code that releases resources.
Purpose
• Release locks.
• Signal waiting processes.

D. Remainder Section
Contains all other code that does not require synchronization.

Requirements for Solving the Critical


Section Problem
A synchronization mechanism should satisfy three conditions:

1. Mutual Exclusion
Only one process can enter the critical section at a time.
Example
If Process P1 is using a printer:
• P2 must wait.
• P3 must wait.

2. Progress
If no process is in the critical section and some processes want to enter, one of
them should be selected without unnecessary delay.
Purpose
Prevents indefinite waiting.

3. Bounded Waiting
Every waiting process should eventually get a chance to enter the critical section.
Purpose
Prevents starvation.

Methods for Solving the Critical Section


Problem
Solutions can be categorized into:

Software Solutions

• Peterson's Algorithm
• Dekker's Algorithm

Hardware Solutions

• Test-and-Set
• Compare-and-Swap

Operating System Solutions

• Semaphores
• Mutexes
• Monitors

Synchronization Hardware
Modern processors provide hardware instructions specifically designed for synchronization.

These instructions execute atomically.

Atomic Operation

An operation that executes completely without interruption.

No other process can interfere while it is executing.

Why Hardware Support is Needed


Software-only solutions may:
• Be slow.
• Fail on multiprocessor systems.
• Become complex.

Hardware synchronization provides:

• Speed
• Reliability
• Better performance

1. Test-and-Set Instruction
Test-and-Set is a hardware synchronization instruction used to implement mutual
exclusion.

It checks a memory location and sets its value in one atomic operation.

Example
Imagine a printer shared by multiple users.

Printer Available
lock = FALSE

First process acquires lock.

lock = TRUE

Other processes wait until the lock becomes FALSE again.

Compare-and-Swap (CAS)
Compare-and-Swap is another hardware synchronization instruction.

It compares a memory value with an expected value.

If they match, the memory value is updated.

The entire operation is atomic.

Real-Life Example
Imagine a locker.
Rule:

"If locker number is empty, place your lock."

If someone already locked it, you cannot enter.

This is similar to Compare-and-Swap.

Synchronization Problems
Several problems occur when synchronization is not properly implemented.

The most common are:

1. Race Condition
2. Deadlock
3. Starvation

1. Race Condition
A race condition occurs when multiple processes access shared data simultaneously and
the result depends on execution timing.

2. Deadlock
Deadlock is a situation where two or more processes wait indefinitely for resources held
by each other.

No process can proceed.

3. Starvation
Starvation occurs when a process waits indefinitely because other processes continuously
receive resources.

The process is never allowed to proceed.

You might also like