0% found this document useful (0 votes)
18 views14 pages

Process Synchronization in OS Explained

Process synchronization in operating systems prevents simultaneous access to shared resources, ensuring data consistency and proper sequencing. Key mechanisms include hardware synchronization, Peterson's algorithm, semaphores, and mutex locks, which help avoid race conditions and maintain system stability. Classical problems like the Producer-Consumer and Readers-Writers illustrate the challenges of synchronization, including deadlocks and starvation.

Uploaded by

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

Process Synchronization in OS Explained

Process synchronization in operating systems prevents simultaneous access to shared resources, ensuring data consistency and proper sequencing. Key mechanisms include hardware synchronization, Peterson's algorithm, semaphores, and mutex locks, which help avoid race conditions and maintain system stability. Classical problems like the Producer-Consumer and Readers-Writers illustrate the challenges of synchronization, including deadlocks and starvation.

Uploaded by

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

Process Synchronization in

Operating Systems
Concepts, Problems, and
Implementation
Mrunalini Moganti
Faculty, Andhra University
Introduction
• Process Synchronization ensures that two or
more processes do not access shared
resources simultaneously.
• It prevents race conditions, ensures data
consistency, and maintains proper sequencing.
• Example: Two users withdrawing from the
same bank account.
Key Concepts
• • Critical Section – Section of code accessing
shared data.
• • Race Condition – Occurs when output
depends on sequence/timing of threads.
• • Mutual Exclusion – Only one process in CS at
a time.
• • Progress – If no process is in CS, one waiting
can enter.
• • Bounded Waiting – Limit waiting time for
Approaches to Synchronization
• 1. Hardware Synchronization
• 2. Software (Peterson’s Algorithm)
• 3. Semaphores
• 4. Mutex Locks
• 5. Monitors
Hardware Synchronization –
Concept
• Hardware synchronization uses atomic CPU
instructions to ensure mutual exclusion.
• These operations are non-interruptible.
• Common atomic instructions:
• • Test-and-Set (TAS)
• • Compare-and-Swap (CAS)
• • Exchange (XCHG)
• They help implement spinlocks and mutexes.
Hardware Synchronization – Test
and Set
• int TestAndSet(int *lock) {
• int old = *lock;
• *lock = 1;
• return old;
• }

• while(TestAndSet(&lock)); // Wait
• Critical Section
Hardware Synchronization –
Compare and Swap
• bool CompareAndSwap(int *value, int
expected, int new) {
• if (*value == expected) {
• *value = new;
• return true;
• }
• return false;
• }
Hardware Synchronization –
Spinlocks and Limitations
• • Spinlocks keep CPU busy in waiting loop.
• • Suitable for multiprocessor systems where
wait time is short.
• • Issues:
• - Wastes CPU time.
• - May cause starvation if not properly
managed.
Peterson’s Algorithm
• Peterson’s Algorithm ensures mutual exclusion
for two processes.
• Shared variables:
• int flag[2]; int turn;

• Process Pi:
• flag[i] = 1;
• turn = j;
Mutex and Semaphores
• • Mutex: Binary lock allowing one thread
access.
• • Semaphore: Integer variable controlling
resource count.

• Semaphore Operations:
• wait(S): while(S <= 0); S--;
• signal(S): S++;
Classical Problems
• 1. Producer-Consumer Problem
• 2. Readers-Writers Problem
• 3. Dining Philosophers Problem
• Used to demonstrate synchronization
concepts and issues such as deadlock and
starvation.
Producer-Consumer Example
(Semaphore)
• semaphore full = 0, empty = n, mutex = 1;
• Producer:
• wait(empty);
• wait(mutex);
• // produce item
• signal(mutex);
• signal(full);
• Consumer:
Readers-Writers Problem
• • Multiple readers can read simultaneously.
• • Writers need exclusive access.

• Semaphore mutex = 1, wrt = 1;


• int readcount = 0;

• Reader:
• wait(mutex);
Summary
• • Synchronization ensures consistent and safe
access to shared resources.
• • Mechanisms include hardware-level atomic
instructions, Peterson’s algorithm,
semaphores, and monitors.
• • Proper synchronization prevents race
conditions, deadlocks, and starvation.

You might also like