ES Unit-5 Answers
ES Unit-5 Answers
Topic – Semaphore
A semaphore is a synchronization tool used in operating systems to manage
concurrent processes or threads. It helps avoid race conditions, deadlocks, and
ensures proper resource sharing.
1. Semaphore is a variable used to control access to a shared resource.
2. It is used in multi-threaded or multi-process environments.
3. Semaphores help in synchronization and mutual exclusion.
4. It prevents problems like two processes accessing the same data at the same
time.
5. There are two main types of semaphores:
Types of Semaphores
1. Binary Semaphore (Mutex-like) : Only two values: 0 (locked) and 1 (unlocked).
Used for mutual exclusion .Only one process can access the resource at a time
2. Counting Semaphore : Can take values 0 or more
ii) Used for counting the number of available resources
iii) Allows multiple processes to access multiple instances of a resource
Semaphores Work
wait() / P() → Decreases the semaphore value. If value < 0, the process waits.
signal() / V() → Increases the semaphore value. If any process is waiting, it gets
unblocked.
Applications of Semaphores
1.Mutual Exclusion : Ensures only one process accesses a critical section at a
time
E.g., Writing to a shared file.
2. Producer-Consumer Problem: Semaphore used to synchronize producer
(adding items) and consumer (removing items) in a buffer.
3. Reader-Writer Problem: Semaphore controls whether multiple readers can
read simultaneously, but only one writer can write.
4. Dining Philosophers Problem: Prevents deadlock when philosophers
(processes) try to pick up shared chopsticks (resources).
5. Thread Synchronization: Ensures threads execute in a specific order or
complete one task before another begins.
6. Operating System Scheduling: Semaphores are used in process scheduling,
interrupt handling, and resource management.
Topic
(i) Mailbox and Message for Inter-Process Communication (IPC)
* Mailbox:
* Think of a mailbox as a designated place.
* Processes can send messages to a mailbox.
* Other processes can read messages from that mailbox.
* Message:
* The actual data or information being sent.
* Can be simple (like a number) or complex (like a structured data).
* Messages are "posted" to a mailbox.
* How it works:
1. A process prepares a message.
2. It sends the message to a specific mailbox.
3. Another process reads the message from that mailbox.
4. The message is then processed.
(ii) Pipe and Queue for Multitasking
* Pipe:
* A one-way communication channel.
* Data flows in a specific direction (like a pipe).
* One process writes data into the pipe.
* Another process reads data from the pipe.
* Queue:
* A data structure where items are added at one end and removed from the
other (FIFO - First In, First Out).
* Processes can add messages to the queue.
* Other processes can read messages from the queue.
* Queues can handle multiple senders and receivers.
* How it works:
1. A process puts data into a pipe or queue.
2. Another process retrieves data from the pipe or queue.
3. The data is then processed by the receiving process.
(iii) Semaphores:
* A synchronization mechanism.
* Used to control access to shared resources.
* Can be thought of as a "lock" or a "counter".
* Two main operations:
* Wait (or P or acquire): Decrements the semaphore value. If the value is
negative, the process blocks.
* Signal (or V or release): Increments the semaphore value. If any processes
are blocked, one is released.
* Used to prevent race conditions and ensure data integrity.
* (ii) Mailbox: (See the explanation above).
* (iii) Pipes: (See the explanation above).