SlideShare a Scribd company logo
3
Most read
6
Most read
16
Most read
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Operating System
KCS – 401
Semaphores
Dr. Pankaj Kumar
Associate Professor – CSE
SRMGPC Lucknow
Outline of the Lecture
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Semaphore – Introduction
Characteristic of Semaphore
Type of Semaphore
Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
A semaphore is a variable or abstract data type used to control access to a common resource by
multiple processes and avoid critical section problems in a concurrent system such as
a multitasking operating system. A trivial semaphore is a plain variable that is changed (for
example, incremented or decremented, or toggled) depending on programmer-defined conditions.
A semaphore is a variable that indicates the number of resources that are available in a system at
a particular time and this semaphore variable is generally used to achieve the process
synchronization. It is generally denoted by "S".
Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
A semaphore uses two functions i.e. wait() and signal(). Both these functions are used to change
the value of the semaphore but the value can be changed by only one process at a particular time
and no other process should change the value simultaneously.
The wait() function is used to decrement the value of the semaphore variable "S" by one if the
value of the semaphore variable is positive. If the value of the semaphore variable is 0, then no
operation will be performed.
wait(S) {
while (S == 0); //there is ";" sign here S--;
}
The signal() function is used to increment the value of the semaphore variable by one.
signal(S) {
S++;
}
Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Characteristic of Semaphore
• It is a mechanism that can be used to provide synchronization of tasks.
• It is a low-level synchronization mechanism.
• Semaphore will always hold a non-negative integer value.
• Semaphore can be implemented using test operations and interrupts, which should be executed
using file descriptors.
Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Types of Semaphore
The two common kinds
• Counting semaphores
• Binary semaphores
Counting Semaphores:
In Counting semaphores, firstly, the semaphore variable is initialized
with the number of resources available. After that, whenever a
process needs some resource, then the wait() function is called and
the value of the semaphore variable is decreased by one. The process
then uses the resource and after using the resource,
the signal() function is called and the value of the semaphore variable
is increased by one.
So, when the value of the semaphore variable goes to 0 i.e all the
resources are taken by the process and there is no resource left to be
used, then if some other process wants to use resources then that
process has to wait for its turn. In this way, we achieve the process
synchronization.
Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Types of Semaphore
The two common kinds
• Counting semaphores
• Binary semaphores
Binary Semaphores: In Binary semaphores, the value of the
semaphore variable will be 0 or 1. Initially, the value of semaphore
variable is set to 1 and if some process wants to use some resource
then the wait() function is called and the value of the semaphore is
changed to 0 from 1. The process then uses the resource and when it
releases the resource then the signal() function is called and the value
of the semaphore variable is increased to 1.
If at a particular instant of time, the value of the semaphore variable
is 0 and some other process wants to use the same resource then it has
to wait for the release of the resource by the previous process. In this
way, process synchronization can be achieved.
Test and Set Lock
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Test and Set Lock (TSL) is a synchronization mechanism.
It uses a test and set instruction to provide the synchronization among the processes executing
concurrently.
It is an instruction that returns the old value of a memory location and sets the memory location
value to 1 as a single atomic operation.
If one process is currently executing a test-and-set, no other process is allowed to begin another
test-and-set until the first process test-and-set is finished.
Test and Set Lock
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Initially, lock value is set to 0.
● Lock value = 0 means the critical section
is currently vacant and no process is
present inside it.
● Lock value = 1 means the critical section
is currently occupied and a process is
present inside it.
Semaphore Usage
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
do {
wait(mutex):
critical Section
signal(mutex);
Remainder section
}while(1)
P2:
wait(synch);
S2;
Dining-Philosophers Problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Philosophers spend their lives alternating thinking and eating
Don’t interact with their neighbors, occasionally try to pick up 2
chopsticks (one at a time) to eat from bowl
Need both to eat, then release both when done
In the case of 5 philosophers
Shared data
Bowl of rice (data set)
Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
The structure of Philosopher i:
do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
Bounded buffer Problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
The structure of the producer process
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Sleeping Barber problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Problem : The analogy is based upon a hypothetical
barber shop with one barber. There is a barber shop
which has one barber, one barber chair, and n chairs
for waiting for customers if there are any to sit on the
chair.
•If there is no customer, then the barber sleeps in his
own chair.
•When a customer arrives, he has to wake up the
barber.
•If there are many customers and the barber is cutting
a customer’s hair, then the remaining customers
either wait if there are empty chairs in the waiting
room or they leave if no chairs are empty.
Sleeping Barber problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Customer {
while(true) {
sem_wait(accessseats);
if(FreeSeats > 0) {
FreeSeats--; /* sitting down.*/
sem_post(Customers); /* notify the barber. */
sem_post(accesseats); /* release the lock */
sem_wait(Barber); /* wait in the waiting room if barber is
busy. */
// customer is having hair cut
} else {
srm_post(accessSeats); /* release the lock */
// customer leaves } }}
Variables: shared data
Semaphore Customers = 0;
Semaphore Barber = 0; // sleeping
Access Mutex Seats = 1;
int FreeSeats = N;
Sleeping Barber problem
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Barber {
while(true) {
Wait (customers); /* waits for a customer (sleeps). */
wait (mutex); // whenever wait(1) is executed it decrement value 0 ie. mutex to protect the
number of available seats
Numberoffreeseat++ // a chair get free
sem_post(barber) /* bring customer for haircut.*/
sem_post(mutex) /* release the mutex on the chair.*/
/* barber is cutting hair.*/
}
}
Problem in Semaphore
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
1. One of the biggest limitations of semaphore is priority inversion.
2. Deadlock, suppose a process is trying to wake up another process which is not in a sleep state.
Therefore, a deadlock may block indefinitely.
3. The operating system has to keep track of all calls to wait and to signal the semaphore.
several process may active simultaneously
Signal(mutex);
……
Critical section
……..
Wait(mutex);
Deadlock may occur
wait(mutex);
……
Critical section
……..
wait(mutex);
Critical Region
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
One of the main problem in semaphore is that semaphore are not syntactically related to the
shared resources that they guard. In particular there are no declaration that could alert the
compiler that a specific data structure is shared and that its accessing needs to be controlled.
Acritical region protects a shared data structure by making its known to it known to the compiler
which can then generate code that maintains mutual exclusive access to the related data. The
declaration of shared variable has the following format:
V: shared : T;
Where the keyword shared informs the compiler that the variable mutes of user deifned type T, is
shared by several person. Process may acess a guarede variable by means of the region construct
as follows:
Region V when B do S;
Statement “do” is executed as a critical section.
Monitor
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
The monitor is one of the ways to achieve Process
synchronization. The monitor is supported by
programming languages to achieve mutual exclusion
between processes. For example Java Synchronized
methods. Java provides wait() and notify() constructs.
1.It is the collection of condition variables and
procedures combined together in a special kind of
module or a package.
2.The processes running outside the monitor can’t access
the internal variable of the monitor but can call
procedures of the monitor.
3.Only one process at a time can execute code inside
monitors.
Monitor
B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar
Key Differences Between Semaphore and Monitor
1. The basic difference between semaphore and monitor is that the semaphore is an integer
variable S which indicate the number of resources available in the system whereas,
the monitor is the abstract data type which allows only one process to execute in critical
section at a time.
2. The value of semaphore can be modified by wait() and signal() operation only. On the other
hand, a monitor has the shared variables and the procedures only through which shared
variables can be accessed by the processes.
3. In Semaphore when a process wants to access shared resources the process performs wait()
operation and block the resources and when it release the resources it performs signal()
operation. In monitors when a process needs to access shared resources, it has to access them
through procedures in monitor.
4. Monitor type has condition variables which semaphore does not have.

More Related Content

PPT
Operating System Scheduling
PDF
Operating System Lecture Notes
DOCX
Amdahl`s law -Processor performance
PPT
Process management in os
PPT
Linux Commands
PDF
Operators in PHP
DOCX
operating system question bank
PDF
Buenas prácticas de codificación para capas de acceso a datos de aplicaciones...
Operating System Scheduling
Operating System Lecture Notes
Amdahl`s law -Processor performance
Process management in os
Linux Commands
Operators in PHP
operating system question bank
Buenas prácticas de codificación para capas de acceso a datos de aplicaciones...

What's hot (18)

PPTX
Cookie and session
PDF
Deadlock Avoidance - OS
PPTX
Lec 01_Linux System Administration (1).pptx
PPTX
Windows process-scheduling
PDF
Operating Systems - Process Synchronization and Deadlocks
PPTX
System_Planning_And_The_Initial_Investigation
PPTX
Php basics
PPTX
Join ordering in fragment queries
PPT
Linux basic commands
PPTX
Deadlock Prevention
PPT
Cpu Scheduling Galvin
PPTX
System analysis and design
PPTX
04. availability-concepts
PPTX
Concept of thread, multi thread, tcb
PPTX
Os unit 3 , process management
PPT
Linux file system
PPTX
Os unit 2
PPT
Backup And Recovery
Cookie and session
Deadlock Avoidance - OS
Lec 01_Linux System Administration (1).pptx
Windows process-scheduling
Operating Systems - Process Synchronization and Deadlocks
System_Planning_And_The_Initial_Investigation
Php basics
Join ordering in fragment queries
Linux basic commands
Deadlock Prevention
Cpu Scheduling Galvin
System analysis and design
04. availability-concepts
Concept of thread, multi thread, tcb
Os unit 3 , process management
Linux file system
Os unit 2
Backup And Recovery
Ad

Similar to Operating System: Semaphor (20)

PPTX
Operating system 24 mutex locks and semaphores
PPT
Process Synchronization -1.ppt
PPT
Semaphores OS Basics
PPT
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
PPTX
Semaphore
PPTX
Interprocess Communication important topic in iOS .pptx
PPTX
PPT 123456617829291912192891semaphores.pptx
PPTX
Operating system 27 semaphores
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
PPTX
B.Tech. Computer Science Engineering OS Notes Unit 2
PPTX
OS Semaphore.pptx
PPT
14-Semaphores.ppt
PPTX
Classical problems of process synchronization
PPTX
Process cooperation and synchronisation
PPTX
Operating system
PPTX
Semophores and it's types
PPTX
Binary Semaphore
PPTX
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
Semaphore
Operating system 24 mutex locks and semaphores
Process Synchronization -1.ppt
Semaphores OS Basics
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
Semaphore
Interprocess Communication important topic in iOS .pptx
PPT 123456617829291912192891semaphores.pptx
Operating system 27 semaphores
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
B.Tech. Computer Science Engineering OS Notes Unit 2
OS Semaphore.pptx
14-Semaphores.ppt
Classical problems of process synchronization
Process cooperation and synchronisation
Operating system
Semophores and it's types
Binary Semaphore
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Semaphore
Ad

More from Shri Ram Swaroop Memorial College of Engineering & Management (20)

PDF
Operating System: Process and synchronization
PDF
Operating System: interprocess Communication
Operating System: Process and synchronization
Operating System: interprocess Communication

Recently uploaded (20)

PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Classroom Observation Tools for Teachers
PDF
Pre independence Education in Inndia.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Pharma ospi slides which help in ospi learning
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
PPH.pptx obstetrics and gynecology in nursing
Classroom Observation Tools for Teachers
Pre independence Education in Inndia.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
RMMM.pdf make it easy to upload and study
Insiders guide to clinical Medicine.pdf
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
Anesthesia in Laparoscopic Surgery in India
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial disease of the cardiovascular and lymphatic systems
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharma ospi slides which help in ospi learning
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
01-Introduction-to-Information-Management.pdf
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose

Operating System: Semaphor

  • 1. B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Operating System KCS – 401 Semaphores Dr. Pankaj Kumar Associate Professor – CSE SRMGPC Lucknow
  • 2. Outline of the Lecture B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Semaphore – Introduction Characteristic of Semaphore Type of Semaphore
  • 3. Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar A semaphore is a variable or abstract data type used to control access to a common resource by multiple processes and avoid critical section problems in a concurrent system such as a multitasking operating system. A trivial semaphore is a plain variable that is changed (for example, incremented or decremented, or toggled) depending on programmer-defined conditions. A semaphore is a variable that indicates the number of resources that are available in a system at a particular time and this semaphore variable is generally used to achieve the process synchronization. It is generally denoted by "S".
  • 4. Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar A semaphore uses two functions i.e. wait() and signal(). Both these functions are used to change the value of the semaphore but the value can be changed by only one process at a particular time and no other process should change the value simultaneously. The wait() function is used to decrement the value of the semaphore variable "S" by one if the value of the semaphore variable is positive. If the value of the semaphore variable is 0, then no operation will be performed. wait(S) { while (S == 0); //there is ";" sign here S--; } The signal() function is used to increment the value of the semaphore variable by one. signal(S) { S++; }
  • 5. Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Characteristic of Semaphore • It is a mechanism that can be used to provide synchronization of tasks. • It is a low-level synchronization mechanism. • Semaphore will always hold a non-negative integer value. • Semaphore can be implemented using test operations and interrupts, which should be executed using file descriptors.
  • 6. Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Types of Semaphore The two common kinds • Counting semaphores • Binary semaphores Counting Semaphores: In Counting semaphores, firstly, the semaphore variable is initialized with the number of resources available. After that, whenever a process needs some resource, then the wait() function is called and the value of the semaphore variable is decreased by one. The process then uses the resource and after using the resource, the signal() function is called and the value of the semaphore variable is increased by one. So, when the value of the semaphore variable goes to 0 i.e all the resources are taken by the process and there is no resource left to be used, then if some other process wants to use resources then that process has to wait for its turn. In this way, we achieve the process synchronization.
  • 7. Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Types of Semaphore The two common kinds • Counting semaphores • Binary semaphores Binary Semaphores: In Binary semaphores, the value of the semaphore variable will be 0 or 1. Initially, the value of semaphore variable is set to 1 and if some process wants to use some resource then the wait() function is called and the value of the semaphore is changed to 0 from 1. The process then uses the resource and when it releases the resource then the signal() function is called and the value of the semaphore variable is increased to 1. If at a particular instant of time, the value of the semaphore variable is 0 and some other process wants to use the same resource then it has to wait for the release of the resource by the previous process. In this way, process synchronization can be achieved.
  • 8. Test and Set Lock B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Test and Set Lock (TSL) is a synchronization mechanism. It uses a test and set instruction to provide the synchronization among the processes executing concurrently. It is an instruction that returns the old value of a memory location and sets the memory location value to 1 as a single atomic operation. If one process is currently executing a test-and-set, no other process is allowed to begin another test-and-set until the first process test-and-set is finished.
  • 9. Test and Set Lock B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Initially, lock value is set to 0. ● Lock value = 0 means the critical section is currently vacant and no process is present inside it. ● Lock value = 1 means the critical section is currently occupied and a process is present inside it.
  • 10. Semaphore Usage B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Consider P1 and P2 that require S1 to happen before S2 Create a semaphore “synch” initialized to 0 P1: S1; signal(synch); do { wait(mutex): critical Section signal(mutex); Remainder section }while(1) P2: wait(synch); S2;
  • 11. Dining-Philosophers Problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Philosophers spend their lives alternating thinking and eating Don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl Need both to eat, then release both when done In the case of 5 philosophers Shared data Bowl of rice (data set) Semaphore chopstick [5] initialized to 1
  • 12. Dining-Philosophers Problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar The structure of Philosopher i: do { wait (chopstick[i] ); wait (chopStick[ (i + 1) % 5] ); // eat signal (chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think } while (TRUE);
  • 13. Bounded buffer Problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar The structure of the producer process do { ... /* produce an item in next_produced */ ... wait(empty); wait(mutex); ... /* add next produced to the buffer */ ... signal(mutex); signal(full); } while (true);
  • 14. Sleeping Barber problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Problem : The analogy is based upon a hypothetical barber shop with one barber. There is a barber shop which has one barber, one barber chair, and n chairs for waiting for customers if there are any to sit on the chair. •If there is no customer, then the barber sleeps in his own chair. •When a customer arrives, he has to wake up the barber. •If there are many customers and the barber is cutting a customer’s hair, then the remaining customers either wait if there are empty chairs in the waiting room or they leave if no chairs are empty.
  • 15. Sleeping Barber problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Customer { while(true) { sem_wait(accessseats); if(FreeSeats > 0) { FreeSeats--; /* sitting down.*/ sem_post(Customers); /* notify the barber. */ sem_post(accesseats); /* release the lock */ sem_wait(Barber); /* wait in the waiting room if barber is busy. */ // customer is having hair cut } else { srm_post(accessSeats); /* release the lock */ // customer leaves } }} Variables: shared data Semaphore Customers = 0; Semaphore Barber = 0; // sleeping Access Mutex Seats = 1; int FreeSeats = N;
  • 16. Sleeping Barber problem B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Barber { while(true) { Wait (customers); /* waits for a customer (sleeps). */ wait (mutex); // whenever wait(1) is executed it decrement value 0 ie. mutex to protect the number of available seats Numberoffreeseat++ // a chair get free sem_post(barber) /* bring customer for haircut.*/ sem_post(mutex) /* release the mutex on the chair.*/ /* barber is cutting hair.*/ } }
  • 17. Problem in Semaphore B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar 1. One of the biggest limitations of semaphore is priority inversion. 2. Deadlock, suppose a process is trying to wake up another process which is not in a sleep state. Therefore, a deadlock may block indefinitely. 3. The operating system has to keep track of all calls to wait and to signal the semaphore. several process may active simultaneously Signal(mutex); …… Critical section …….. Wait(mutex); Deadlock may occur wait(mutex); …… Critical section …….. wait(mutex);
  • 18. Critical Region B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar One of the main problem in semaphore is that semaphore are not syntactically related to the shared resources that they guard. In particular there are no declaration that could alert the compiler that a specific data structure is shared and that its accessing needs to be controlled. Acritical region protects a shared data structure by making its known to it known to the compiler which can then generate code that maintains mutual exclusive access to the related data. The declaration of shared variable has the following format: V: shared : T; Where the keyword shared informs the compiler that the variable mutes of user deifned type T, is shared by several person. Process may acess a guarede variable by means of the region construct as follows: Region V when B do S; Statement “do” is executed as a critical section.
  • 19. Monitor B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar The monitor is one of the ways to achieve Process synchronization. The monitor is supported by programming languages to achieve mutual exclusion between processes. For example Java Synchronized methods. Java provides wait() and notify() constructs. 1.It is the collection of condition variables and procedures combined together in a special kind of module or a package. 2.The processes running outside the monitor can’t access the internal variable of the monitor but can call procedures of the monitor. 3.Only one process at a time can execute code inside monitors.
  • 20. Monitor B.Tech – CS 2nd Year Operating System (KCS- 401) Dr. Pankaj Kumar Key Differences Between Semaphore and Monitor 1. The basic difference between semaphore and monitor is that the semaphore is an integer variable S which indicate the number of resources available in the system whereas, the monitor is the abstract data type which allows only one process to execute in critical section at a time. 2. The value of semaphore can be modified by wait() and signal() operation only. On the other hand, a monitor has the shared variables and the procedures only through which shared variables can be accessed by the processes. 3. In Semaphore when a process wants to access shared resources the process performs wait() operation and block the resources and when it release the resources it performs signal() operation. In monitors when a process needs to access shared resources, it has to access them through procedures in monitor. 4. Monitor type has condition variables which semaphore does not have.