SlideShare a Scribd company logo
Interprocess
Communication
Mutual exclusion
And
synchronizations
Interprocess Communication
• Communication of processes running
concurrently on a computer system.
• Major issues
– Passing information to another process
– Not disturbing other processes when
doing critical activities.
– Process synchronization
Race conditions
• Occurs when multiple processes access
and manipulate the same shared data
concurrently and the outcome of the
execution depends on the particular order in
which the access takes place.
• Example
– Assume two processes incrementing the value
of the same shared variable.
– The value of the var will depend on the
sequence the processes access the var.
Critical Region/section
• A piece of code that access a shared
resource.
• A process is said to be in its critical
section when it is executing critical code.
Mutual exclusion
• When one process is executing in its critical
section, no other process can execute in its
critical section
• Each process takes the form
– Repeat
• Entry section
• Critical section
– Exit critical section
• Remainder section
– Forever
Mutual exclusion using critical
region
Process A
Process B
t1 t2 t3
t4
Time
Enter Critical section
A leaves critical section
B attempts to critical section and is blocked
B leaves critical section
Conditions for mutual exclusion
solution
• No two processes may be
simultaneously inside their critical
region
• No assumption may be made about
the speeds or the number of CPUs
• No process running outside its critical
section may block other processes
• No process should have to wait
forever to enter its critical region.
Solutions to Mutual exclusion
• Disabling interrupts
• Using lock variables
• Strict alternation
• Using TLS instruction
• Strict alternation
• Petersons solution
• Semaphores
• Monitors
Semaphores
• Suggested by E W Dijkstra(1965)
• Nonnegative integer variable(S) that can be
operated upon by wait(s) and Signal(s) and
the initialization operation.
• Used in mutual exclusion and process
synchronization
• Types
– Binary /mutex semaphore
– Counting semaphores
Semaphore operations
• Signal(S)
– Increases the value of S
– Atomic operation (cannot be interrupted)
• Signal()
– S++
• Wait(S)
– Decreases the value of S
– Atomic operation (cannot be interrupted)
• Signal()
– S--
Semaphore Types
• Types
– Binary /mutex semaphore
• Used for mutual exclusion
• S only takes on values 0,1
– Counting semaphores
• Used for process synchronization
• Represents the number of resources available
Binary /mutex semaphore
• Blocking in semaphores
– Associated with each semaphore is a queue of
waiting processes
– If s > 0 /*Test entry point
• Wait(S);
• Enter critical section; /* Semaphore is open, Process proceeds
– Else Block; /* Semaphore is closed, process blocks
– Signal(s); /*waiting process on a queue is unblocked
/*If no process is on the queue the signal is
/*remembered
Producer consumer problem using
semaphore
• Program for producers
• Repeat indefinitely
• Begin
– Produce item;
– Wait(space available);
– Wait(buffer
manipulation);
– Deposit item in buffer;
– Signal(buffer
manipulation);
– Signal(item available);
• End;
• Program for
consumers
• Repeat indefinitely
• Begin
– Wait(item available);
– Wait(buffer
manipulation)
– Excract item from buffer;
– Signal(buffer
manipulation);
– Signal(space available);
– Consume item;
• End;
Producer consumer problem using
semaphore
• #Define N 100
• typedef int semaphore;
• semaphore mutex = 1;
• semaphore empty = N;
• semaphore full = 0;
• Void producer(void)
– {
• Int item;
– While (TRUE){
• Item = producer_item();
• wait(&empty);
• wait(&mutex);
• Insert_item(item);
• signal(&mutex);
• signal(&full));
• }
– {
– Void consumer(void)
– {
• Int item;
• While(True){
– wait(&full);
– wait(&mutex);
– Item = remove_item();
– signal(&mutex);
– signal(&empty);
– consume-_item;
– }
• }
Semaphore summary
• Semaphores can be used to solve any of the
synchronization problem
• However ,thy have some drawbacks
– They are shared variables.
– No connection between the semaphore and the data
being controlled
– No guarantee of proper usage
• Hard to use
– Use programming language support
Monitors
• Programming language construct that controls access to shared
data
• Mutual exclusion code added by the compiler, enforced at runtime
• Consists of
– Shared data structures and variables
– Procedures that operate on (1)
– Piece of program that initialize the variables
• Protects its data from unstructured access
Example of Monitor
• Monitor example
– Integer i;
– Condition c;
– Procedure consumer;
• .
• .
– End;
– Procedure producer;
• .
• .
– End;
• End monitor;
Monitors and mutual exclusion
• Monitors guarantees mutual exclusion
• Only one process can be active in a monitor at any time
– Compiler must handle monitor procedures differently from other
procedures
– Compiler must implement mutual exclusion on monitor entries
• If a second process invokes a monitor procedure when
the first one is within the monitor, it blocks.
– Monitor has to have a wait queue
• If no process is using the monitor, the calling process
may enter.
Condition Variables
• Provides a mechanism to wait for
events
• Support two operations
• Wait
– Causes the calling process to block
• Signal
– Wake up sleeping process
Signal Semantics
• Hoare monitors
– Allow newly awakened process to run, suspending the
other
• Brinch Hansen proposal
– Process doing a signal must exit the monitor immediately
– Putting signal statement as the final statement in a monitor
procedure
• Mesa Monitor
– Signal() places a waiter on the ready queue,but signaler
continues inside the monitor
– Condition is not necessarily true when waiter runs again
Condition variables vs
semaphores
• Semaphores
– Can be used anywhere in a program, but should not be used in a monitor Wait()
– Wait() does not always block the caller (i.e., when the semaphore counter is greater than
zero).
– Signal() either releases a blocked thread, if there is one, or increases the semaphore
counter.
– If Signal() releases a blocked thread, the caller and the released thread both continue.
• Condition Variables
– Can only be used in monitors
– Wait always block the caller
– Signal() either releases a blocked thread, if there is one, or the signal is lost as if it never
happens.
– If Signal() releases a blocked thread, the caller yields the monitor (Hoare type) or continues
(Mesa Type). Only one of the caller or the released thread can continue, but not both.

More Related Content

What's hot

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
Ravindra Raju Kolahalam
 
Group Communication (Distributed computing)
Group Communication (Distributed computing)Group Communication (Distributed computing)
Group Communication (Distributed computing)
Sri Prasanna
 
Interprocess communication
Interprocess communicationInterprocess communication
Interprocess communication
Sushil Singh
 

What's hot (20)

Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]Inter Process Communication Presentation[1]
Inter Process Communication Presentation[1]
 
Message passing ( in computer science)
Message   passing  ( in   computer  science)Message   passing  ( in   computer  science)
Message passing ( in computer science)
 
Group Communication (Distributed computing)
Group Communication (Distributed computing)Group Communication (Distributed computing)
Group Communication (Distributed computing)
 
Inter Process Communication-R.D.Sivakumar
Inter Process Communication-R.D.SivakumarInter Process Communication-R.D.Sivakumar
Inter Process Communication-R.D.Sivakumar
 
Lecture3
Lecture3Lecture3
Lecture3
 
Interprocess communication
Interprocess communicationInterprocess communication
Interprocess communication
 
Message Passing Systems
Message Passing SystemsMessage Passing Systems
Message Passing Systems
 
Operating system support in distributed system
Operating system support in distributed systemOperating system support in distributed system
Operating system support in distributed system
 
Asynchronous Message Passing
Asynchronous Message PassingAsynchronous Message Passing
Asynchronous Message Passing
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Reader/writer problem
Reader/writer problemReader/writer problem
Reader/writer problem
 
Wireless LAN in Data Communication & Networking
Wireless LAN in Data Communication & NetworkingWireless LAN in Data Communication & Networking
Wireless LAN in Data Communication & Networking
 
Message Passing, Remote Procedure Calls and Distributed Shared Memory as Com...
Message Passing, Remote Procedure Calls and  Distributed Shared Memory as Com...Message Passing, Remote Procedure Calls and  Distributed Shared Memory as Com...
Message Passing, Remote Procedure Calls and Distributed Shared Memory as Com...
 
CS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMSCS6601 DISTRIBUTED SYSTEMS
CS6601 DISTRIBUTED SYSTEMS
 
Processes and Processors in Distributed Systems
Processes and Processors in Distributed SystemsProcesses and Processors in Distributed Systems
Processes and Processors in Distributed Systems
 
2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts2. Distributed Systems Hardware & Software concepts
2. Distributed Systems Hardware & Software concepts
 
Distributed System by Pratik Tambekar
Distributed System by Pratik TambekarDistributed System by Pratik Tambekar
Distributed System by Pratik Tambekar
 
mmWeb
mmWebmmWeb
mmWeb
 
Communications is distributed systems
Communications is distributed systemsCommunications is distributed systems
Communications is distributed systems
 

Viewers also liked

Mutual Exclusion in Wireless Sensor and Actor Networks
Mutual Exclusion in Wireless Sensor and Actor NetworksMutual Exclusion in Wireless Sensor and Actor Networks
Mutual Exclusion in Wireless Sensor and Actor Networks
Zhenyun Zhuang
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algos
Akhil Sharma
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
Souvik Roy
 
Mutual Exclusion Election (Distributed computing)
Mutual Exclusion Election (Distributed computing)Mutual Exclusion Election (Distributed computing)
Mutual Exclusion Election (Distributed computing)
Sri Prasanna
 

Viewers also liked (20)

NTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC PresentationNTP Software Jan 2012 Monthly Meeting IPC Presentation
NTP Software Jan 2012 Monthly Meeting IPC Presentation
 
Memory, IPC and L4Re
Memory, IPC and L4ReMemory, IPC and L4Re
Memory, IPC and L4Re
 
Mutual Exclusion in Wireless Sensor and Actor Networks
Mutual Exclusion in Wireless Sensor and Actor NetworksMutual Exclusion in Wireless Sensor and Actor Networks
Mutual Exclusion in Wireless Sensor and Actor Networks
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual Exclusion
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Pbcbt an improvement of ntbcbt algorithm
Pbcbt an improvement of ntbcbt algorithmPbcbt an improvement of ntbcbt algorithm
Pbcbt an improvement of ntbcbt algorithm
 
Chapter05 new
Chapter05 newChapter05 new
Chapter05 new
 
A New Function-based Framework for Classification and Evaluation of Mutual Ex...
A New Function-based Framework for Classification and Evaluation of Mutual Ex...A New Function-based Framework for Classification and Evaluation of Mutual Ex...
A New Function-based Framework for Classification and Evaluation of Mutual Ex...
 
Mutual exclusion and synchronization
Mutual exclusion and synchronizationMutual exclusion and synchronization
Mutual exclusion and synchronization
 
Multiprocessors(performance and synchronization issues)
Multiprocessors(performance and synchronization issues)Multiprocessors(performance and synchronization issues)
Multiprocessors(performance and synchronization issues)
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algos
 
Ipc
IpcIpc
Ipc
 
Mutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's AlgorithmMutual Exclusion using Peterson's Algorithm
Mutual Exclusion using Peterson's Algorithm
 
Mutual exclusion
Mutual exclusionMutual exclusion
Mutual exclusion
 
Ipc
IpcIpc
Ipc
 
Lamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusionLamport’s algorithm for mutual exclusion
Lamport’s algorithm for mutual exclusion
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Mutual Exclusion Election (Distributed computing)
Mutual Exclusion Election (Distributed computing)Mutual Exclusion Election (Distributed computing)
Mutual Exclusion Election (Distributed computing)
 
Semaphores
SemaphoresSemaphores
Semaphores
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 

Similar to Inter process communication

Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
Kadri20
 
ch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included picturesch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included pictures
PranjalRana4
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrency
yoavrubin
 

Similar to Inter process communication (20)

Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphores
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Operating system 27 semaphores
Operating system 27 semaphoresOperating system 27 semaphores
Operating system 27 semaphores
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Os3
Os3Os3
Os3
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
ch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included picturesch13 here is the ppt of this chapter included pictures
ch13 here is the ppt of this chapter included pictures
 
Ipc feb4
Ipc feb4Ipc feb4
Ipc feb4
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
Process management in Operating System_Unit-2
Process management in Operating System_Unit-2Process management in Operating System_Unit-2
Process management in Operating System_Unit-2
 
Lecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdfLecture 5- Process Synchonization_revised.pdf
Lecture 5- Process Synchonization_revised.pdf
 
Concurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptxConcurrency Control in Distributed Systems.pptx
Concurrency Control in Distributed Systems.pptx
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrency
 
process syn.ppt
process syn.pptprocess syn.ppt
process syn.ppt
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
UiPath New York Community Day in-person event
UiPath New York Community Day in-person eventUiPath New York Community Day in-person event
UiPath New York Community Day in-person event
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
КАТЕРИНА АБЗЯТОВА «Ефективне планування тестування ключові аспекти та практ...
КАТЕРИНА АБЗЯТОВА  «Ефективне планування тестування  ключові аспекти та практ...КАТЕРИНА АБЗЯТОВА  «Ефективне планування тестування  ключові аспекти та практ...
КАТЕРИНА АБЗЯТОВА «Ефективне планування тестування ключові аспекти та практ...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 

Inter process communication

  • 2. Interprocess Communication • Communication of processes running concurrently on a computer system. • Major issues – Passing information to another process – Not disturbing other processes when doing critical activities. – Process synchronization
  • 3. Race conditions • Occurs when multiple processes access and manipulate the same shared data concurrently and the outcome of the execution depends on the particular order in which the access takes place. • Example – Assume two processes incrementing the value of the same shared variable. – The value of the var will depend on the sequence the processes access the var.
  • 4. Critical Region/section • A piece of code that access a shared resource. • A process is said to be in its critical section when it is executing critical code.
  • 5. Mutual exclusion • When one process is executing in its critical section, no other process can execute in its critical section • Each process takes the form – Repeat • Entry section • Critical section – Exit critical section • Remainder section – Forever
  • 6. Mutual exclusion using critical region Process A Process B t1 t2 t3 t4 Time Enter Critical section A leaves critical section B attempts to critical section and is blocked B leaves critical section
  • 7. Conditions for mutual exclusion solution • No two processes may be simultaneously inside their critical region • No assumption may be made about the speeds or the number of CPUs • No process running outside its critical section may block other processes • No process should have to wait forever to enter its critical region.
  • 8. Solutions to Mutual exclusion • Disabling interrupts • Using lock variables • Strict alternation • Using TLS instruction • Strict alternation • Petersons solution • Semaphores • Monitors
  • 9. Semaphores • Suggested by E W Dijkstra(1965) • Nonnegative integer variable(S) that can be operated upon by wait(s) and Signal(s) and the initialization operation. • Used in mutual exclusion and process synchronization • Types – Binary /mutex semaphore – Counting semaphores
  • 10. Semaphore operations • Signal(S) – Increases the value of S – Atomic operation (cannot be interrupted) • Signal() – S++ • Wait(S) – Decreases the value of S – Atomic operation (cannot be interrupted) • Signal() – S--
  • 11. Semaphore Types • Types – Binary /mutex semaphore • Used for mutual exclusion • S only takes on values 0,1 – Counting semaphores • Used for process synchronization • Represents the number of resources available
  • 12. Binary /mutex semaphore • Blocking in semaphores – Associated with each semaphore is a queue of waiting processes – If s > 0 /*Test entry point • Wait(S); • Enter critical section; /* Semaphore is open, Process proceeds – Else Block; /* Semaphore is closed, process blocks – Signal(s); /*waiting process on a queue is unblocked /*If no process is on the queue the signal is /*remembered
  • 13. Producer consumer problem using semaphore • Program for producers • Repeat indefinitely • Begin – Produce item; – Wait(space available); – Wait(buffer manipulation); – Deposit item in buffer; – Signal(buffer manipulation); – Signal(item available); • End; • Program for consumers • Repeat indefinitely • Begin – Wait(item available); – Wait(buffer manipulation) – Excract item from buffer; – Signal(buffer manipulation); – Signal(space available); – Consume item; • End;
  • 14. Producer consumer problem using semaphore • #Define N 100 • typedef int semaphore; • semaphore mutex = 1; • semaphore empty = N; • semaphore full = 0; • Void producer(void) – { • Int item; – While (TRUE){ • Item = producer_item(); • wait(&empty); • wait(&mutex); • Insert_item(item); • signal(&mutex); • signal(&full)); • } – { – Void consumer(void) – { • Int item; • While(True){ – wait(&full); – wait(&mutex); – Item = remove_item(); – signal(&mutex); – signal(&empty); – consume-_item; – } • }
  • 15. Semaphore summary • Semaphores can be used to solve any of the synchronization problem • However ,thy have some drawbacks – They are shared variables. – No connection between the semaphore and the data being controlled – No guarantee of proper usage • Hard to use – Use programming language support
  • 16. Monitors • Programming language construct that controls access to shared data • Mutual exclusion code added by the compiler, enforced at runtime • Consists of – Shared data structures and variables – Procedures that operate on (1) – Piece of program that initialize the variables • Protects its data from unstructured access
  • 17. Example of Monitor • Monitor example – Integer i; – Condition c; – Procedure consumer; • . • . – End; – Procedure producer; • . • . – End; • End monitor;
  • 18. Monitors and mutual exclusion • Monitors guarantees mutual exclusion • Only one process can be active in a monitor at any time – Compiler must handle monitor procedures differently from other procedures – Compiler must implement mutual exclusion on monitor entries • If a second process invokes a monitor procedure when the first one is within the monitor, it blocks. – Monitor has to have a wait queue • If no process is using the monitor, the calling process may enter.
  • 19. Condition Variables • Provides a mechanism to wait for events • Support two operations • Wait – Causes the calling process to block • Signal – Wake up sleeping process
  • 20. Signal Semantics • Hoare monitors – Allow newly awakened process to run, suspending the other • Brinch Hansen proposal – Process doing a signal must exit the monitor immediately – Putting signal statement as the final statement in a monitor procedure • Mesa Monitor – Signal() places a waiter on the ready queue,but signaler continues inside the monitor – Condition is not necessarily true when waiter runs again
  • 21. Condition variables vs semaphores • Semaphores – Can be used anywhere in a program, but should not be used in a monitor Wait() – Wait() does not always block the caller (i.e., when the semaphore counter is greater than zero). – Signal() either releases a blocked thread, if there is one, or increases the semaphore counter. – If Signal() releases a blocked thread, the caller and the released thread both continue. • Condition Variables – Can only be used in monitors – Wait always block the caller – Signal() either releases a blocked thread, if there is one, or the signal is lost as if it never happens. – If Signal() releases a blocked thread, the caller yields the monitor (Hoare type) or continues (Mesa Type). Only one of the caller or the released thread can continue, but not both.