CPS 303 Assignment 300L
CPS 303 Assignment 300L
GROUP MEMBERS
NAMES MATRIC NUMBER
SEMAPHORES
WHAT ARE SEMAPHORES?
Semaphores are just normal variables used to coordinate the activities of multiple processes in a
computer system. They are used to enforce mutual exclusion, avoid race conditions, and implement
synchronization between processes.
HOW IT WORKS:
The process of using Semaphores provides two operations: wait (P) and signal (V).
• Wait (P operation): Decreases the counter. If the counter is positive, the thread proceeds.
If it’s zero or negative, the thread waits.
• Signal (V operation): Increases the counter and wakes up waiting threads if any.
TYPES OF SEMAPHORES
1. BINARY SEMAPHORE: This is also known as a mutex lock. It can have only two values
0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section
problems with multiple processes.
2. COUNTING SEMAPHORE: Its value can range over an unrestricted domain. It is used
to control access to a resource that has multiple instances.
ADVANTAGES OF SEMAPHORES
➢ Easy to understand for single-resource management.
➢ Supports coordination between multiple processes
➢ Provides a flexible and robust way to manage shared resources.
➢ To implement critical sections in a program.
➢ To avoid race conditions.
DISADVANTAGES OF SEMAPHORES
➢ It can lead to performance degradation due to overhead associated with wait and signal
operations.
➢ Can result in DEADLOCK if used incorrectly.
➢ It can cause performance issues in a program if not used properly.
➢ It can be difficult to debug and maintain.
➢ It can be prone to race conditions and other synchronization problems if not used
correctly.
➢ It can be vulnerable to certain types of attacks, such as denial of service attacks.
Description: Traffic lights at an intersection can be managed using semaphores to control the
flow of traffic and ensure safe crossing.
Example: Traffic Lights: Represented by semaphores that control the green, yellow, and red
lights for different directions.
Semaphores Used: Each light direction is controlled by a semaphore that manages the timing
and transitions between light states.
Implementation
Light Controller: Uses semaphores to cycle through green, yellow, and red lights. The
controller ensures that only one direction has the green light at a time and manages transitions to
avoid conflicts.
➢ Semaphore value: 1 (only one transaction can access and modify an account at a time is
crucial for maintaining data integrity).
Process
➢ Acquire Semaphore: A transaction must need to acquire the semaphore first before
modifying an account balance.
➢ The account is : Then operations like debiting or crediting the account are performed
by the transaction.
➢ Release Semaphore: After Completing the transactions the semaphore is released to
allow other transactions to proceed.
➢ Multiple Print Jobs: Processes that need to access shared resources (printers)
➢ Semaphore Value: 1 (only one print job can access the printer at a time)
Process
➢ Acquire Semaphore: First, acquire a semaphore for the printer to begin the print job.
➢ Release Semaphore: Now the semaphore is released after the job is done.
➢ Semaphore Value: 1 (only one train can access the track at a time)
Process
➢ Release Semaphore: The semaphores are released after the train passes the track.
MONITORS
A monitor is a synchronization construct that enables multiple processes or threads to coordinate
actions and ensures that they are not interfering with each other or producing unexpected results. it
ensures that only one thread is executed at a critical code section.
Monitors serve as guards for critical code sections, ensuring that no two processes can enter them
simultaneously.
SYNTAX OF A MONITOR:
CHARACTERISTICS OF MONITORS
➢ Encapsulation: Monitors bundle together the data (shared resource) and the methods
(functions) that operate on it. This encapsulation restricts direct access to the shared
resource, allowing only controlled access through monitor methods.
➢ Automatic Mutual Exclusion: Only one thread can execute a monitor method at a time.
This ensures that multiple threads don’t access the shared resource simultaneously,
preventing race conditions.
➢ Condition Variables: Monitors use condition variables to manage thread synchronization.
Threads can wait (pause execution) until certain conditions are met, and other threads can
signal (notify) waiting threads when the condition is true.
➢ High-Level Abstraction: Monitors provide a structured, easy-to-understand way to handle
synchronization, making them less error-prone than lower-level mechanisms like
semaphores.
➢ Built-In Locking: Monitors have a built-in locking mechanism, so threads don’t need to
manually acquire and release locks, simplifying the code.
ADVANTAGES OF MONITORS
➢ Monitors may overcome the timing errors that occur when semaphores are used.
➢ Monitors are a collection of procedures and condition variables that are combined in a
special type of module.
DISADVANTAGES OF MONITORS
RESOURCE CONTROL Better for multiple resource Typically controls a single resource
ERROR PRONE More prone to programmer error Reduced error risk due to
encapsulation
Both semaphores and monitors are types process synchronization tools in operating systems,
however they are quite different from each other, as descried in the above table. The most
significant difference that you should note here is that semaphores provide more flexibility but
require careful management, while monitors offer a safer, higher-level interface with built-in
mutual exclusion.
REFERENCES
"Monitor vs. Semaphore - What's the Difference? | This vs. That." https://thisvsthat.io/monitor-vs-
semaphore.