Here’s a simple explanation of the content from your textbook pages about Process Synchronization, Critical
Section, Mutual Exclusion, and Semaphores:
🔄 Process Synchronization
When many processes run at the same time, they may need to access shared resources (like memory or
files).
Synchronization ensures that they don’t interfere with each other.
It avoids problems like data corruption or incorrect outputs.
⚖️Concurrency vs Parallelism
Concurrency = Two or more tasks start and progress at the same time (not necessarily exactly at the same
moment).
Parallelism = Tasks run at the same exact time using different CPUs.
🧩 Critical Section Problem
A critical section is the part of a program where it accesses shared resources.
Only one process should be in the critical section at a time.
The problem: How to make sure only one process enters this section?
🔧 The solution has 3 parts:
1. Entry Section – Code to request entry into the critical section.
2. Critical Section – The code that accesses shared resources.
3. Exit Section – Code to exit the critical section.
🔐 Mutual Exclusion
Means no two processes can access the critical section at the same time.
Can be achieved using:
o Hardware solutions
o Software solutions
o Semaphores (important)
🛑 Race Condition
Happens when two or more processes change shared data at the same time.
It causes unpredictable results.
To avoid it: use proper synchronization.
🧱 Semaphores
Semaphores are variables used to control access to shared resources.
There are 2 types:
1. Binary Semaphore (like a switch – 0 or 1)
2. Counting Semaphore (can be any number)
🧑💻 Operations on Semaphores:
wait() (or P) – Decreases semaphore value, maybe makes the process wait.
signal() (or V) – Increases semaphore value, may allow waiting process to enter.
🔄 Busy Waiting
When a process keeps checking if it can enter the critical section.
This wastes CPU time.
Solutions exist to reduce or eliminate this (like using sem_wait()).
🔧 Semaphore Functions in C (POSIX)
sem_init() → Initializes a semaphore.
sem_wait() → Waits/decrements the semaphore.
sem_post() → Signals/increments the semaphore.
📌 Example:
#include <semaphore.h>
sem_t mySemaphore;
sem_init(&mySemaphore, 0, 1); // Initialize semaphore to 1
sem_wait(&mySemaphore); // Lock (enter critical section)
// critical section code
sem_post(&mySemaphore); // Unlock (exit critical section)
📝 University Questions (Examples)
1. Explain how wait() and signal() work in multiprocessor systems.
2. Give examples of semaphores with proper explanation.
Let me know if you want a summary table, real-life examples, or help with pseudocode!