SlideShare a Scribd company logo
Process Synchronization:
A co-operation process is one that can affect or be affected by other processes executing in the
system. Co-operating process may either directly share a logical address space or be allotted to the
shared data only through files. This concurrent access is known as Process synchronization.
Critical Section Problem:
Consider a system consisting of n processes (P0, P1, ………Pn -1) each process has a segment of code
which is known as critical section in which the process may be changing common variable, updating
a table, writing a file and so on. The important feature of the system is that when the process is
executing in its critical section no other process is to be allowed to execute in its critical section.
The execution of critical sections by the processes is a mutually exclusive. The critical section
problem is to design a protocol that the process can use to cooperate each process must request
permission to enter its critical section. The section of code implementing this request is the entry
section. The critical section is followed on exit section. The remaining code is the remainder
section. Example:
While (1)
{
Entry Section;
Critical Section;
Exit Section;
Remainder Section;
}
A solution to the critical section problem must satisfy the following three conditions.
1. Mutual Exclusion: If process Pi is executing in its critical section then no any other process
can be executing in their critical section.
2. Progress: If no process is executing in its critical section and some process wish to enter
their critical sections then only those process that are not executing in their remainder
section can enter its critical section next.
3. Bounded waiting: There exists a bound on the number of times that other processes are
allowed to enter their critical sections after a process has made a request.
Semaphores:
For the solution to the critical section problem one synchronization tool is used which is known as
semaphores. A semaphore ‗S‘ is an integer variable which is accessed through two standard
operations such as wait and signal. These operations were originally termed ‗P‘ (for wait means
to test) and ‗V‘ (for single means to increment). The classical definition of wait is
Wait (S)
{
While (S <= 0)
{
Test;
}
S--;
}
The classical definition of the signal is
Signal (S)
{
S++;
}
In case of wait the test condition is executed with interruption and the decrement is executed
without interruption.
Binary Semaphore:
A binary semaphore is a semaphore with an integer value which can range between 0 and 1.
Let ‗S‘ be a counting semaphore. To implement the binary semaphore we need following
the structure of data.
Binary Semaphores S1, S2;
int C;
Initially S1 = 1, S2 = 0 and the value of C is set to the initial value of the counting semaphore ‗S‘.
Then the wait operation of the binary semaphore can be implemented as follows.
Wait (S1)
C--;
if (C < 0)
{
Signal (S1);
Wait (S2);
}
Signal (S1);
The signal operation of the binary semaphore can be implemented as follows:
Wait (S1);
C++;
if (C <=0)
Signal (S2);
Else
Signal (S1);
Classical Problem on Synchronization:
There are various types of problem which are proposed for synchronization scheme such as
Bounded Buffer Problem: This problem was commonly used to illustrate the power of
synchronization primitives. In this scheme we assumed that the pool consists of ‗N‘ buffer
and each capable of holding one item. The ‗mutex‘ semaphore provides mutual exclusion
for access to the buffer pool and is initialized to the value one. The empty and full
semaphores count the number of empty and full buffer respectively. The semaphore empty
is initialized to ‗N‘ and the semaphore full is initialized to zero. This problem is known as
procedure and consumer problem. The code of the producer is producing full buffer and
the code of consumer is producing empty buffer. The structure of producer process is as
follows: do {
produce an item in nextp
. . . . . . . . . . . .
Wait (empty);
Wait (mutex);
. . . . . . . . . . .
add nextp to buffer
. . . . . . . . . . . .
Signal (mutex);
Signal (full);
} While (1);
The structure of consumer process is as follows:
do {
Wait (full);
Wait (mutex);
. . . . . . . . . . .
Remove an item from buffer to nextc
. . . . . . . . . . .
Signal (mutex);
Signal (empty);
. . . . . . . . . . . .
Consume the item in nextc;
. . . . . . . .. . . .. .
} While (1);
Reader Writer Problem: In this type of problem there are two types of process are used
such as Reader process and Writer process. The reader process is responsible for only
reading and the writer process is responsible for writing. This is an important problem of
synchronization which has several variations like
o The simplest one is referred as first reader writer problem which requires that no
reader will be kept waiting unless a writer has obtained permission to use the shared
object. In other words no reader should wait for other reader to finish because a
writer is waiting.
o The second reader writer problem requires that once a writer is ready then the writer
performs its write operation as soon as possible.
The structure of a reader process is as follows:
Wait (mutex);
Read count++;
if (read count == 1)
Wait (wrt);
Signal (mutex);
. . . . . . . . . . .
Reading is performed
. . . . . . . . . . .
Wait (mutex);
Read count --;
if (read count == 0)
Signal (wrt);
Signal (mutex);
The structure of the writer process is as follows:
Wait (wrt);
Writing is performed;
Signal (wrt);
Dining Philosopher Problem: Consider 5 philosophers to spend their lives in thinking &
eating. A philosopher shares common circular table surrounded by 5 chairs each occupies
by one philosopher. In the center of the table there is a bowl of rice and the table is laid
with 6 chopsticks as shown in below figure.
When a philosopher thinks she does not interact with her colleagues. From time to time a
philosopher gets hungry and tries to pickup two chopsticks that are closest to her. A
philosopher may pickup one chopstick or two chopsticks at a time but she cannot pickup a
chopstick that is already in hand of the neighbor. When a hungry philosopher has both her
chopsticks at the same time, she eats without releasing her chopsticks. When she finished
eating, she puts down both of her chopsticks and starts thinking again. This problem is
considered as classic synchronization problem. According to this problem each chopstick
is represented by a semaphore. A philosopher grabs the chopsticks by executing the wait
operation on that semaphore. She releases the chopsticks by executing the signal operation
on the appropriate semaphore. The structure of dining philosopher is as follows: do{
32 | P a g e
Wait ( chopstick [i]);
Wait (chopstick [(i+1)%5]);
. . . . . . . . . . . . .
Eat
. . . . . . . . . . . . .
Signal (chopstick [i]);
Signal (chopstick [(i+1)%5]);
. . . . . . . . . . . . .
Think
. . . . . . . . . . . . .
} While (1);
Critical Region:
According to the critical section problem using semaphore all processes must share a semaphore
variablemutex which is initialized to one. Each process must execute wait (mutex) before
entering the critical section and execute the signal (mutex) after completing the execution but
there are various difficulties may arise with this approach like:
Case 1: Suppose that a process interchanges the order in which the wait and signal operations on
the semaphore mutex are executed, resulting in the following execution: Signal (mutex);
. . . . . . . . . .
Critical Section
. . . . . . . . . . .
Wait (mutex);
In this situation several processes may be executing in their critical sections simultaneously, which is
violating mutual exclusion requirement.
Case 2: Suppose that a process replaces the signal (mutex) with wait (mutex). The execution is as
follows:
Wait (mutex);
. . . . . . . . . . .
Critical Section
. . . . . . . . . . .
Wait (mutex);
33 | P a g e
In this situation a deadlock will occur
Case 3: Suppose that a process omits the wait (mutex) and the signal (mutex). In this case the
mutual exclusion is violated or a deadlock will occur.
To illustrate the various types or error generated by using semaphore there are some high level
language constructs have been introduced such as critical region and monitor.
Critical region is also known as conditional critical regions. It constructs guards against certain
simple errors associated with semaphore. This high level language synchronization construct
requires a variable V of type T which is to be shared among many processes. It is declared as
V: shared T;
The variable V can be accessed only inside a region statement as like below:
Wait (mutex);
While (! B) {
First_count++;
if (second_count> 0)
Signal (second_delay);
Else
Signal (mutex);
Wait (first_delay);
First_count--;
Second_count++;
if (first_count> 0)
Signal (first_delay);
Else
Signal (second_delay);
Wait (second_delay);
Second_count --;
}
S;
if (first_count> 0)
Signal (first_delay);
Else if (second_count> 0)
Signal (second_delay);
Else
Signal (mutex);
(Implementation of the conditional region constructs)
Where B is a Boolean variable which governs the access to the critical regions which is initialized
to false.Mutex, First_delay and Second_delay are the semaphores which are initialized to 1, 0, and 0
respectively. First_count and Second_count are the integer variables which are initialized to zero.
Monitor:
It is characterized as a set of programmer defined operators. Its representation consists of declaring
of variables, whose value defines the state of an instance. The syntax of monitor is as follows.
Monitor monitor_name
{
Shared variable declarations
Procedure body P1 (………) {
. . . . . . . .
}
Procedure body P2 (………) {
. . . . . . . .
}
.
.
.
Procedure body Pn (………) {
. . . . . . . .
}
{
Initialization Code
}
}
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

More Related Content

PPTX
Semophores and it's types
Nishant Joshi
 
PPT
Ch7 OS
C.U
 
PPT
Semaphores OS Basics
Shijin Raj P
 
PPT
Process Synchronization
Sonali Chauhan
 
PDF
Semaphores
Mohd Arif
 
PPT
Semaphores and Monitors
sathish sak
 
PPT
Process Synchronization
vinay arora
 
Semophores and it's types
Nishant Joshi
 
Ch7 OS
C.U
 
Semaphores OS Basics
Shijin Raj P
 
Process Synchronization
Sonali Chauhan
 
Semaphores
Mohd Arif
 
Semaphores and Monitors
sathish sak
 
Process Synchronization
vinay arora
 

What's hot (20)

PPT
Mca ii os u-2 process management & communication
Rai University
 
PPTX
Process synchronization
Ali Ahmad
 
PPTX
Operating system critical section
Harshana Madusanka Jayamaha
 
PPT
Process synchronization(deepa)
Nagarajan
 
PDF
ITFT_Semaphores and bounded buffer
Sneh Prabha
 
PPT
OSCh7
Joe Christensen
 
PPT
OS Process Synchronization, semaphore and Monitors
sgpraju
 
PDF
6 Synchronisation
Dr. Loganathan R
 
PPT
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
PPTX
Critical section problem in operating system.
MOHIT DADU
 
PDF
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
PDF
Monitors
Mohd Arif
 
PDF
Control Structures in Python
Sumit Satam
 
PPT
Lec11 semaphores
anandammca
 
PPTX
SYNCHRONIZATION
atanuanwesha
 
PDF
Storage classes arrays & functions in C Language
Jenish Bhavsar
 
PPSX
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
Anne Lee
 
PPT
Peterson Critical Section Problem Solution
Bipul Chandra Kar
 
Mca ii os u-2 process management & communication
Rai University
 
Process synchronization
Ali Ahmad
 
Operating system critical section
Harshana Madusanka Jayamaha
 
Process synchronization(deepa)
Nagarajan
 
ITFT_Semaphores and bounded buffer
Sneh Prabha
 
OS Process Synchronization, semaphore and Monitors
sgpraju
 
6 Synchronisation
Dr. Loganathan R
 
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
Critical section problem in operating system.
MOHIT DADU
 
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
Monitors
Mohd Arif
 
Control Structures in Python
Sumit Satam
 
Lec11 semaphores
anandammca
 
SYNCHRONIZATION
atanuanwesha
 
Storage classes arrays & functions in C Language
Jenish Bhavsar
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
Anne Lee
 
Peterson Critical Section Problem Solution
Bipul Chandra Kar
 
Ad

Similar to Os unit 3 (20)

PPT
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
PPTX
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
PPTX
Process cooperation and synchronisation
JayeshGadhave1
 
PPT
Process Synchronization -1.ppt
jayverma27
 
PPT
Chapter 5-Process Synchronization OS.ppt
ssuser09d6cd1
 
PDF
Ch5 process synchronization
Welly Dian Astika
 
DOCX
UNIT III Process Synchronization.docx
karthikaparthasarath
 
PPTX
Classical problems of process synchronization
sangrampatil81
 
DOCX
Process synchronization
lodhran-hayat
 
PPT
operating-system-notes-for -chapters--- 2
insdcn
 
PDF
OS Process synchronization Unit3 synchronization
subhamchy2005
 
PPT
process syn.ppt
Pandiya Rajan
 
PPT
Inter process communication
Gift Kaliza
 
PPT
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
PPTX
Interprocess Communication important topic in iOS .pptx
thetale999
 
PDF
CH05.pdf
ImranKhan880955
 
DOCX
Critical section operating system
Muhammad Baqar Kazmi
 
PDF
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
PPT
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
Process cooperation and synchronisation
JayeshGadhave1
 
Process Synchronization -1.ppt
jayverma27
 
Chapter 5-Process Synchronization OS.ppt
ssuser09d6cd1
 
Ch5 process synchronization
Welly Dian Astika
 
UNIT III Process Synchronization.docx
karthikaparthasarath
 
Classical problems of process synchronization
sangrampatil81
 
Process synchronization
lodhran-hayat
 
operating-system-notes-for -chapters--- 2
insdcn
 
OS Process synchronization Unit3 synchronization
subhamchy2005
 
process syn.ppt
Pandiya Rajan
 
Inter process communication
Gift Kaliza
 
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
Interprocess Communication important topic in iOS .pptx
thetale999
 
CH05.pdf
ImranKhan880955
 
Critical section operating system
Muhammad Baqar Kazmi
 
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Ad

Recently uploaded (20)

PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
CDH. pptx
AneetaSharma15
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Basics and rules of probability with real-life uses
ravatkaran694
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 

Os unit 3

  • 1. Process Synchronization: A co-operation process is one that can affect or be affected by other processes executing in the system. Co-operating process may either directly share a logical address space or be allotted to the shared data only through files. This concurrent access is known as Process synchronization. Critical Section Problem: Consider a system consisting of n processes (P0, P1, ………Pn -1) each process has a segment of code which is known as critical section in which the process may be changing common variable, updating a table, writing a file and so on. The important feature of the system is that when the process is executing in its critical section no other process is to be allowed to execute in its critical section. The execution of critical sections by the processes is a mutually exclusive. The critical section problem is to design a protocol that the process can use to cooperate each process must request permission to enter its critical section. The section of code implementing this request is the entry section. The critical section is followed on exit section. The remaining code is the remainder section. Example: While (1) { Entry Section; Critical Section; Exit Section; Remainder Section; } A solution to the critical section problem must satisfy the following three conditions. 1. Mutual Exclusion: If process Pi is executing in its critical section then no any other process can be executing in their critical section. 2. Progress: If no process is executing in its critical section and some process wish to enter their critical sections then only those process that are not executing in their remainder section can enter its critical section next. 3. Bounded waiting: There exists a bound on the number of times that other processes are allowed to enter their critical sections after a process has made a request. Semaphores: For the solution to the critical section problem one synchronization tool is used which is known as semaphores. A semaphore ‗S‘ is an integer variable which is accessed through two standard
  • 2. operations such as wait and signal. These operations were originally termed ‗P‘ (for wait means to test) and ‗V‘ (for single means to increment). The classical definition of wait is Wait (S) { While (S <= 0) { Test; } S--; } The classical definition of the signal is Signal (S) { S++; } In case of wait the test condition is executed with interruption and the decrement is executed without interruption. Binary Semaphore: A binary semaphore is a semaphore with an integer value which can range between 0 and 1. Let ‗S‘ be a counting semaphore. To implement the binary semaphore we need following the structure of data. Binary Semaphores S1, S2; int C; Initially S1 = 1, S2 = 0 and the value of C is set to the initial value of the counting semaphore ‗S‘. Then the wait operation of the binary semaphore can be implemented as follows. Wait (S1) C--; if (C < 0) { Signal (S1); Wait (S2); }
  • 3. Signal (S1); The signal operation of the binary semaphore can be implemented as follows: Wait (S1); C++; if (C <=0) Signal (S2); Else Signal (S1); Classical Problem on Synchronization: There are various types of problem which are proposed for synchronization scheme such as Bounded Buffer Problem: This problem was commonly used to illustrate the power of synchronization primitives. In this scheme we assumed that the pool consists of ‗N‘ buffer and each capable of holding one item. The ‗mutex‘ semaphore provides mutual exclusion for access to the buffer pool and is initialized to the value one. The empty and full semaphores count the number of empty and full buffer respectively. The semaphore empty is initialized to ‗N‘ and the semaphore full is initialized to zero. This problem is known as procedure and consumer problem. The code of the producer is producing full buffer and the code of consumer is producing empty buffer. The structure of producer process is as follows: do { produce an item in nextp . . . . . . . . . . . . Wait (empty); Wait (mutex); . . . . . . . . . . . add nextp to buffer . . . . . . . . . . . . Signal (mutex); Signal (full); } While (1); The structure of consumer process is as follows:
  • 4. do { Wait (full); Wait (mutex); . . . . . . . . . . . Remove an item from buffer to nextc . . . . . . . . . . . Signal (mutex); Signal (empty); . . . . . . . . . . . . Consume the item in nextc; . . . . . . . .. . . .. . } While (1); Reader Writer Problem: In this type of problem there are two types of process are used such as Reader process and Writer process. The reader process is responsible for only reading and the writer process is responsible for writing. This is an important problem of synchronization which has several variations like o The simplest one is referred as first reader writer problem which requires that no reader will be kept waiting unless a writer has obtained permission to use the shared object. In other words no reader should wait for other reader to finish because a writer is waiting. o The second reader writer problem requires that once a writer is ready then the writer performs its write operation as soon as possible. The structure of a reader process is as follows: Wait (mutex); Read count++; if (read count == 1) Wait (wrt); Signal (mutex); . . . . . . . . . . . Reading is performed . . . . . . . . . . . Wait (mutex);
  • 5. Read count --; if (read count == 0) Signal (wrt); Signal (mutex); The structure of the writer process is as follows: Wait (wrt); Writing is performed; Signal (wrt); Dining Philosopher Problem: Consider 5 philosophers to spend their lives in thinking & eating. A philosopher shares common circular table surrounded by 5 chairs each occupies by one philosopher. In the center of the table there is a bowl of rice and the table is laid with 6 chopsticks as shown in below figure. When a philosopher thinks she does not interact with her colleagues. From time to time a philosopher gets hungry and tries to pickup two chopsticks that are closest to her. A philosopher may pickup one chopstick or two chopsticks at a time but she cannot pickup a chopstick that is already in hand of the neighbor. When a hungry philosopher has both her chopsticks at the same time, she eats without releasing her chopsticks. When she finished eating, she puts down both of her chopsticks and starts thinking again. This problem is considered as classic synchronization problem. According to this problem each chopstick is represented by a semaphore. A philosopher grabs the chopsticks by executing the wait operation on that semaphore. She releases the chopsticks by executing the signal operation on the appropriate semaphore. The structure of dining philosopher is as follows: do{ 32 | P a g e
  • 6. Wait ( chopstick [i]); Wait (chopstick [(i+1)%5]); . . . . . . . . . . . . . Eat . . . . . . . . . . . . . Signal (chopstick [i]); Signal (chopstick [(i+1)%5]); . . . . . . . . . . . . . Think . . . . . . . . . . . . . } While (1); Critical Region: According to the critical section problem using semaphore all processes must share a semaphore variablemutex which is initialized to one. Each process must execute wait (mutex) before entering the critical section and execute the signal (mutex) after completing the execution but there are various difficulties may arise with this approach like: Case 1: Suppose that a process interchanges the order in which the wait and signal operations on the semaphore mutex are executed, resulting in the following execution: Signal (mutex); . . . . . . . . . . Critical Section . . . . . . . . . . . Wait (mutex); In this situation several processes may be executing in their critical sections simultaneously, which is violating mutual exclusion requirement. Case 2: Suppose that a process replaces the signal (mutex) with wait (mutex). The execution is as follows: Wait (mutex); . . . . . . . . . . . Critical Section . . . . . . . . . . . Wait (mutex); 33 | P a g e
  • 7. In this situation a deadlock will occur Case 3: Suppose that a process omits the wait (mutex) and the signal (mutex). In this case the mutual exclusion is violated or a deadlock will occur. To illustrate the various types or error generated by using semaphore there are some high level language constructs have been introduced such as critical region and monitor. Critical region is also known as conditional critical regions. It constructs guards against certain simple errors associated with semaphore. This high level language synchronization construct requires a variable V of type T which is to be shared among many processes. It is declared as V: shared T; The variable V can be accessed only inside a region statement as like below: Wait (mutex); While (! B) { First_count++; if (second_count> 0) Signal (second_delay); Else Signal (mutex); Wait (first_delay); First_count--; Second_count++; if (first_count> 0) Signal (first_delay); Else Signal (second_delay); Wait (second_delay); Second_count --; } S; if (first_count> 0) Signal (first_delay); Else if (second_count> 0) Signal (second_delay);
  • 8. Else Signal (mutex); (Implementation of the conditional region constructs) Where B is a Boolean variable which governs the access to the critical regions which is initialized to false.Mutex, First_delay and Second_delay are the semaphores which are initialized to 1, 0, and 0 respectively. First_count and Second_count are the integer variables which are initialized to zero. Monitor: It is characterized as a set of programmer defined operators. Its representation consists of declaring of variables, whose value defines the state of an instance. The syntax of monitor is as follows. Monitor monitor_name { Shared variable declarations Procedure body P1 (………) { . . . . . . . . } Procedure body P2 (………) { . . . . . . . . } . . . Procedure body Pn (………) { . . . . . . . . } { Initialization Code } }