SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Department of I.T. Engineering
Presentation
On
Semaphore and critical section problem solving
Academic year 2018-19
Laxmi Institute of Technology, Sarigam
Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad
Enrolment
number
Name
160860116018 Nishant P. Joshi
160860116029 Raj M. Patel
160860116032 Udit A. Patel
Content
 What is Semaphore?
 Methods for Semaphore
 Types of semaphore
 Semaphore Implementation
 Bounded-Buffer Problem
 Readers-Writers Problem
 Dining-Philosophers Problem
Semaphore
 A semaphore is a variable or abstract data type used to
control access to a common resource by
multiple processes in a concurrent system such as
a multitasking operating system.
 A semaphore is an object that consists of a counter, a
waiting list of processes and two methods:
signal and wait.
 Synchronization tool that does not require busy waiting
(spinlock).
Facts about semaphore
 It is a mechanism that can be used to provide
synchronization of tasks.
 It is a low level synchronization mechanism.
 It was devised in 1965 by Edsger Dijkstra to provide
competition synchronization and also cooperation
synchronization.
 It is a data structure that contains an integer and a queue
that store tasks descriptors.
 Semaphore has only two operations. They are pass/wait
and release. Originally named P and V by Dijkstra, after
two Dutch words passeren (to pass) and vrygeren (to
release).
Semaphore Method: wait( )
void wait(sem S)
{
S.count--;
if (S.count < 0)
{
add the caller to the waiting list; block();
}
}
• After decreasing the counter by 1, if the counter value
becomes negative, then
 add the caller to the waiting list, and then
 block itself.
• Resources are occupied for a process to occur.
Semaphore Method: signal( )
void signal(sem S)
{ S.count++;
if (S.count <= 0)
{
remove a process P from the waiting list; resume(P);
}
}
 After increasing the counter by 1, if the new counter value is
not positive, then
 remove a process P from the waiting list,
 resume the execution of process P, and return
 Resources are released for other process to occupy.
Semaphores Types
Counting semaphore – integer value can range over
an unrestricted domain.
• full: counts the number of slots that are full
• empty: counts the number of slots empty
Binary semaphore – integer value can range only
between 0 and 1; can be simpler to implement.
• mutex: makes sure the producer and consumer don’t
access the buffer at the same time
Wait() and Signal() are performed atomically
Semaphore Implementation
Mutual exclusion implementation with semaphores
Shared data:
semaphore mutex; //initially mutex = 1
Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
}while(1);
Bounded-Buffer Problem
Shared data
semaphore full, empty, mutex;
● pool of n buffers, each can hold one item
● mutex provides mutual exclusion to the buffer pool
● empty and full count the number of empty and full buffers
Initially:
full = 0, empty = n, mutex = 1;
// Consumer
do {
wait(full)
wait(mutex);
…
remove an item from buffer to
nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
}while(1);
Bounded-Buffer Problem
// Producer
do{
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
}while(1);
Readers-Writers Problem
● readcount keeps track of how many processes are reading the
object
● mutex provides mutual exclusion for changes to readcount
● wrt provides mutual exclusion for the writers
● when writer exits and signals wrt, either another waiting
writer executes or another waiting reader – the scheduler
decides
Readers-Writers Problem
● Sharing a data object (file or record) among several
concurrent processes.
● If one processes wants to write and another process wants to
reads or write, synchronization problems can arise.
• Require that the writers have exclusive access to the shared
object.
Shared data
semaphore mutex, wrt;
Initially
mutex = 1, wrt = 1, readcount = 0
Readers-Writers Problem
// Write process
wait(wrt);
…
writing is performed
… signal(wrt);
// Read process
wait(mutex);
readcount++;
if (readcount == 1) wait(wrt);
signal(mutex);
…
reading is performed
… wait(mutex);
readcount--;
if (readcount == 0) signal(wrt);
signal(mutex):
Dining-Philosophers Problem
Processes are competing for exclusive access to a
limited number of resources
● Philosophers eat/think
● Eating needs 2 chopsticks
● Pick up one chopstick at a time
● How to prevent deadlock
Shared data
semaphore chopstick[5];
Initially all values are 1
Dining-Philosophers Problem
● All could pick up left fork simultaneously, see right fork not
available , put down left fork simultaneously and repeat the
process – starvation
● Each could wait a random amount of time before pick up right
fork but that is not a good solution for all applications
● Could use a binary semaphore, would enable only one
philosopher to eat at a time
Dining-Philosophers Problem
Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
think
} while (1);
Semophores and it's types

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Process scheduling
Process schedulingProcess scheduling
Process scheduling
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Memory organization (Computer architecture)
Memory organization (Computer architecture)Memory organization (Computer architecture)
Memory organization (Computer architecture)
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
Introduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed ComputingIntroduction to Parallel and Distributed Computing
Introduction to Parallel and Distributed Computing
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Interrupts
InterruptsInterrupts
Interrupts
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Process state in OS
Process state in OSProcess state in OS
Process state in OS
 
8 memory management strategies
8 memory management strategies8 memory management strategies
8 memory management strategies
 
Distributed Operating System_1
Distributed Operating System_1Distributed Operating System_1
Distributed Operating System_1
 
Deadlock
DeadlockDeadlock
Deadlock
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Segmentation in Operating Systems.
Segmentation in Operating Systems.Segmentation in Operating Systems.
Segmentation in Operating Systems.
 
Asynchronous and synchronous
Asynchronous and synchronousAsynchronous and synchronous
Asynchronous and synchronous
 

Ähnlich wie Semophores and it's types

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Classic synchronization
Classic synchronizationClassic synchronization
Classic synchronizationhina firdaus
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationGift Kaliza
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 
Go Observability (in practice)
Go Observability (in practice)Go Observability (in practice)
Go Observability (in practice)Eran Levy
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitorssathish sak
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communicationRai University
 
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)Brian Brazil
 
Master Thesis Presentation
Master Thesis PresentationMaster Thesis Presentation
Master Thesis PresentationMohamed Sobh
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Performance engineering methodologies
Performance engineering  methodologiesPerformance engineering  methodologies
Performance engineering methodologiesManeesh Chaturvedi
 
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 semaphoresVaibhav Khanna
 

Ähnlich wie Semophores and it's types (20)

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Os unit 3
Os unit 3Os unit 3
Os unit 3
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
Classic synchronization
Classic synchronizationClassic synchronization
Classic synchronization
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
Go Observability (in practice)
Go Observability (in practice)Go Observability (in practice)
Go Observability (in practice)
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
Mca ii os u-2 process management & communication
Mca  ii  os u-2 process management & communicationMca  ii  os u-2 process management & communication
Mca ii os u-2 process management & communication
 
OS UNIT3.pptx
OS UNIT3.pptxOS UNIT3.pptx
OS UNIT3.pptx
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)
Counting with Prometheus (CloudNativeCon+Kubecon Europe 2017)
 
Master Thesis Presentation
Master Thesis PresentationMaster Thesis Presentation
Master Thesis Presentation
 
Distributed System
Distributed System Distributed System
Distributed System
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Performance engineering methodologies
Performance engineering  methodologiesPerformance engineering  methodologies
Performance engineering methodologies
 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
 
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
 
Bt0070
Bt0070Bt0070
Bt0070
 

Kürzlich hochgeladen

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfryanfarris8
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyAnusha Are
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456KiaraTiradoMicha
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 

Kürzlich hochgeladen (20)

MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 

Semophores and it's types

  • 1. Department of I.T. Engineering Presentation On Semaphore and critical section problem solving Academic year 2018-19 Laxmi Institute of Technology, Sarigam Approved by AICTE, New Delhi; Affiliated to Gujarat Technological University, Ahmedabad Enrolment number Name 160860116018 Nishant P. Joshi 160860116029 Raj M. Patel 160860116032 Udit A. Patel
  • 2. Content  What is Semaphore?  Methods for Semaphore  Types of semaphore  Semaphore Implementation  Bounded-Buffer Problem  Readers-Writers Problem  Dining-Philosophers Problem
  • 3. Semaphore  A semaphore is a variable or abstract data type used to control access to a common resource by multiple processes in a concurrent system such as a multitasking operating system.  A semaphore is an object that consists of a counter, a waiting list of processes and two methods: signal and wait.  Synchronization tool that does not require busy waiting (spinlock).
  • 4. Facts about semaphore  It is a mechanism that can be used to provide synchronization of tasks.  It is a low level synchronization mechanism.  It was devised in 1965 by Edsger Dijkstra to provide competition synchronization and also cooperation synchronization.  It is a data structure that contains an integer and a queue that store tasks descriptors.  Semaphore has only two operations. They are pass/wait and release. Originally named P and V by Dijkstra, after two Dutch words passeren (to pass) and vrygeren (to release).
  • 5. Semaphore Method: wait( ) void wait(sem S) { S.count--; if (S.count < 0) { add the caller to the waiting list; block(); } } • After decreasing the counter by 1, if the counter value becomes negative, then  add the caller to the waiting list, and then  block itself. • Resources are occupied for a process to occur.
  • 6. Semaphore Method: signal( ) void signal(sem S) { S.count++; if (S.count <= 0) { remove a process P from the waiting list; resume(P); } }  After increasing the counter by 1, if the new counter value is not positive, then  remove a process P from the waiting list,  resume the execution of process P, and return  Resources are released for other process to occupy.
  • 7. Semaphores Types Counting semaphore – integer value can range over an unrestricted domain. • full: counts the number of slots that are full • empty: counts the number of slots empty Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. • mutex: makes sure the producer and consumer don’t access the buffer at the same time Wait() and Signal() are performed atomically
  • 8. Semaphore Implementation Mutual exclusion implementation with semaphores Shared data: semaphore mutex; //initially mutex = 1 Process Pi: do { wait(mutex); critical section signal(mutex); remainder section }while(1);
  • 9. Bounded-Buffer Problem Shared data semaphore full, empty, mutex; ● pool of n buffers, each can hold one item ● mutex provides mutual exclusion to the buffer pool ● empty and full count the number of empty and full buffers Initially: full = 0, empty = n, mutex = 1;
  • 10. // Consumer do { wait(full) wait(mutex); … remove an item from buffer to nextc … signal(mutex); signal(empty); … consume the item in nextc … }while(1); Bounded-Buffer Problem // Producer do{ … produce an item in nextp … wait(empty); wait(mutex); … add nextp to buffer … signal(mutex); signal(full); }while(1);
  • 11. Readers-Writers Problem ● readcount keeps track of how many processes are reading the object ● mutex provides mutual exclusion for changes to readcount ● wrt provides mutual exclusion for the writers ● when writer exits and signals wrt, either another waiting writer executes or another waiting reader – the scheduler decides
  • 12. Readers-Writers Problem ● Sharing a data object (file or record) among several concurrent processes. ● If one processes wants to write and another process wants to reads or write, synchronization problems can arise. • Require that the writers have exclusive access to the shared object. Shared data semaphore mutex, wrt; Initially mutex = 1, wrt = 1, readcount = 0
  • 13. Readers-Writers Problem // Write process wait(wrt); … writing is performed … signal(wrt); // Read process wait(mutex); readcount++; if (readcount == 1) wait(wrt); signal(mutex); … reading is performed … wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex):
  • 14. Dining-Philosophers Problem Processes are competing for exclusive access to a limited number of resources ● Philosophers eat/think ● Eating needs 2 chopsticks ● Pick up one chopstick at a time ● How to prevent deadlock Shared data semaphore chopstick[5]; Initially all values are 1
  • 15. Dining-Philosophers Problem ● All could pick up left fork simultaneously, see right fork not available , put down left fork simultaneously and repeat the process – starvation ● Each could wait a random amount of time before pick up right fork but that is not a good solution for all applications ● Could use a binary semaphore, would enable only one philosopher to eat at a time
  • 16. Dining-Philosophers Problem Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) eat signal(chopstick[i]); signal(chopstick[(i+1) % 5]); think } while (1);