0% found this document useful (0 votes)
12 views9 pages

CPS 303 Assignment 300L

The document discusses semaphores and monitors as mechanisms for managing access to shared resources in concurrent programming. It explains the operations, types, advantages, and disadvantages of semaphores, along with classical synchronization problems they can solve. Additionally, it describes monitors, their characteristics, advantages, and disadvantages, and concludes with a comparison between the two synchronization tools.

Uploaded by

frw8k4ny6w
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)
12 views9 pages

CPS 303 Assignment 300L

The document discusses semaphores and monitors as mechanisms for managing access to shared resources in concurrent programming. It explains the operations, types, advantages, and disadvantages of semaphores, along with classical synchronization problems they can solve. Additionally, it describes monitors, their characteristics, advantages, and disadvantages, and concludes with a comparison between the two synchronization tools.

Uploaded by

frw8k4ny6w
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
You are on page 1/ 9

GROUP THREE

TOPIC-TITLE: SEMAPHORES AND MONITORS

COURSE CODE: CPS 303

LECTURER IN-CHARGE: MRS.ADEBIYI-SODUNKE A.A

GROUP MEMBERS
NAMES MATRIC NUMBER

AHMED KHADIJAH ABIDEMI S122202023

ABBA MUSTAPHA AMODU S122202024

ABOGUNRIN ZAINAB OMOWUNMI S122202025

KOLADE ABDULLATEEF S122202027

ILESANMI LATEEF ABIODUN S122202028

TIJANI RODIYAT S122202029

BALOGUN AISHA ADESEWA S122202030

AKEREDOLU JAMIAT YEWANDE S122202031

MUSTAPHA JAMIU OLANREWAJU S122202032

FADEYI MUADH OLALEKAN S122202033

OYEWOLE ABDULQAHAR S123202045


SEMAPHORES AND MONITORS
INTRODUCTION
Semaphores and monitors are both mechanisms used in programming for managing access to
shared resources in concurrent systems, such as multi-threaded programs, to prevent conflicts like
race conditions and ensure data consistency.

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

Semaphores are of two Types:

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.

CLASSICAL SYNCHRONIZATION PROBLEMS USING SEMAPHORES CONCEPT


These are the classical synchronization problem using the concept of semaphores.
1. TRAFFIC LIGHT CONTROL

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.

2. BANK TRANSACTION PROCESSING

➢ Multiple Transactions: Processes that need to access shared resources (accounts)

➢ Account Balance: Shared resource represented by a semaphore

➢ 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.

3. PRINT QUEUE MANAGEMENT

➢ Multiple Print Jobs: Processes that need to access shared resources (printers)

➢ Printer: Shared resource represented by a semaphore

➢ 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.

➢ Print Job Execution: The printer processes the print job.

➢ Release Semaphore: Now the semaphore is released after the job is done.

4. RAILWAY TRACK MANAGEMENT

➢ Multiple Trains: Processes that need to access shared resources (tracks)

➢ Track: Shared resource represented by a semaphore

➢ Semaphore Value: 1 (only one train can access the track at a time)

Process

➢ Acquire Semaphore: A train acquires the semaphore before entering a track.

➢ Travel Across Track: train Travels across the track.

➢ 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

➢ Mutual exclusion is automatic in monitors.

➢ Monitors are less difficult to implement than semaphores.

➢ 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

➢ Monitors must be implemented into the programming language.

➢ Less flexible compared to semaphores; cannot handle multiple resources as easily.


➢ If not designed carefully, can still lead to deadlocks, especially in nested monitor calls.
DIFFERENCES BETWEEN SEMAPHORE AND MONITOR

FEATURE SEMAPHORE MONITOR

ABSTRACTION LEVEL Lower-level Higher-level

RESOURCE CONTROL Better for multiple resource Typically controls a single resource

ERROR PRONE More prone to programmer error Reduced error risk due to
encapsulation

OWNERSHIP No concept of ownership Only the thread holding the lock


can signal other threads

OPERATIONS Wait(P) and Signal(V) Wait and signal via condition


operations. variables in methods
CONCLUSION

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

"Monitors in Process Synchronization - GeeksforGeeks." 24 Mar. 2023,


https://www.geeksforgeeks.org/monitors-in-process-synchronization/.

"Monitor vs Semaphore - GeeksforGeeks." 20 May. 2024, https://www.geeksforgeeks.org/monitor-vs-


semaphore/.

"Lecture 6: Semaphores and Monitors - University of California, San Diego."


https://cseweb.ucsd.edu/classes/fa05/cse120/lectures/120-l6.pdf.

"Monitor vs. Semaphore - What's the Difference? | This vs. That." https://thisvsthat.io/monitor-vs-
semaphore.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy