SlideShare una empresa de Scribd logo
1 de 14
Descargar para leer sin conexión
Semaphores and
Bounded Buffer
Semaphores
• Semaphore is a type of generalized lock
• Defined by Dijkstra in the last 60s
• Main synchronization primitives used in UNIX
• Consist of a positive integer value
• Two operations
• P(): an atomic operation that waits for semaphore to
become positive, then decrement it by 1
• V(): an atomic operation that increments semaphore
by 1 and wakes up a waiting thread at P(), if any.
Semaphores vs. Integers
• No negative values
• Only operations are P() and V()
• Cannot read or write semaphore values
• Except at the initialization times
• Operations are atomic
• Two P() calls cannot decrement the value below
zero
• A sleeping thread at P() cannot miss a wakeup
from V()
Binary Semaphores
• A binary semaphore is initialized to 1
• P() waits until the value is 1
• Then set it to 0
• V() sets the value to 1
• Wakes up a thread waiting at P(), if any
Two Uses of Semaphores
1. Mutual exclusion
• Lock was designed to do this
lock->acquire();
// critical section
lock->release();
Two Uses of Semaphores
1. Mutual exclusion
1. The lock function can be realized with a binary
semaphore: semaphore subsumes lock.
• Semaphore has an initial value of 1
• P() is called before a critical section
• V() is called after the critical section
semaphore litter_box = 1;
P(litter_box);
// critical section
V(litter_box);
Two Uses of Semaphores
1. Mutual exclusion
• Semaphore has an initial value of 1
• P() is called before a critical section
• V() is called after the critical section
semaphore litter_box = 1;
P(litter_box);
// critical section
V(litter_box);
litter_box = 1
Two Uses of Semaphores
1. Mutual exclusion
• Semaphore has an initial value of 1
• P() is called before a critical section
• V() is called after the critical section
semaphore litter_box = 1;
P(litter_box); // purrr…
// critical section
V(litter_box);
litter_box = 1  0
Producer-Consumer with a
Bounded Buffer
• A classic problem
• A producer put things into a shared buffer
• A consumer takes them out
Problem Constraints
• The solution involves both synchronization
and mutual exclusion
• Constraints
• The consumer must wait if buffers are empty
(synchronization constraint)
• The producer must wait if buffers are full
(synchronization constraint)
• Only one thread can manipulate the buffer at a
time (mutual exclusion)
Implementing Semaphore
• How to implement semaphore?
• Almost exactly like lock.
• Using spinlock or queue
• What hardware support is needed?
• Interrupt disable
• Test-and-set
Implementing Semaphore
class semaphore {
int value;
}
semaphore::semaphore(int i) {
value = i;
}
semaphore::p() {
// disable interrupts
while (value == 0) {
// enable interrupts
// disable interrupts
}
value --;
// enable interrupts
}
semaphore::v() {
// disable interrupts
value ++;
// enable interrupts
}
Implementing Semaphore
with test and set
class semaphore {
int value;
}
semaphore::semaphore(int
i) {
value = i;
}
semaphore::p() {
while
(test_and_set(guard));
while (value == 0) {
// queue the
thread
// guard = 0 and
sleep
}
value --;
guard = 0;
semaphore::v() {
while
(test_and_set(guard));
if (anyone waiting) {
// wake up one thread
// put in on ready
queue
} else {
value ++;
}
guard = 0;
}
Semaphore in UNIX
• Managing concurrent access to shared memory.
• Semaphore system calls
• Creation: semget( … )
• Incr/Decr/set : semop(…)
• Deletion: semctl(semid, 0, IPC_RMID, 0);
• See examples: seminit.c, sema.c
semb.c

Más contenido relacionado

La actualidad más candente

Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitorssathish sak
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphoresanandammca
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS BasicsShijin Raj P
 
Synchronization
SynchronizationSynchronization
SynchronizationMohd Arif
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Operating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationOperating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationSyaiful Ahdan
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksMukesh Chinta
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINTAnne Lee
 
Synchronization linux
Synchronization linuxSynchronization linux
Synchronization linuxSusant Sahani
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Operating systems question bank
Operating systems question bankOperating systems question bank
Operating systems question bankanuradha raheja
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 

La actualidad más candente (20)

Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
OSCh7
OSCh7OSCh7
OSCh7
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphores
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Monitors
MonitorsMonitors
Monitors
 
Synchronization
SynchronizationSynchronization
Synchronization
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
Operating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationOperating System-Ch6 process synchronization
Operating System-Ch6 process synchronization
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
 
SYNCHRONIZATION
SYNCHRONIZATIONSYNCHRONIZATION
SYNCHRONIZATION
 
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
 
Synchronization linux
Synchronization linuxSynchronization linux
Synchronization linux
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Operating systems question bank
Operating systems question bankOperating systems question bank
Operating systems question bank
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 

Destacado

Ssc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivilSsc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivilDeepak Kumar
 
Sanghamitra Jayant on the GoUNESCO India 2014 Challenge
Sanghamitra Jayant on the GoUNESCO India 2014 ChallengeSanghamitra Jayant on the GoUNESCO India 2014 Challenge
Sanghamitra Jayant on the GoUNESCO India 2014 ChallengeGoUNESCO
 
Controller in asp.net mvc
Controller in asp.net mvcController in asp.net mvc
Controller in asp.net mvcReza Rahimy
 
5 Ways to Increase Mobile conversion rates
5 Ways to Increase Mobile conversion rates5 Ways to Increase Mobile conversion rates
5 Ways to Increase Mobile conversion ratesTambourine
 
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...Michele Borsoi
 
Bab ii pembahasan
Bab ii pembahasanBab ii pembahasan
Bab ii pembahasanAbd Halim
 
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0dwi kharisma
 
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...Michele Borsoi
 
Facultad de-educación-y-humanidades (2)
Facultad de-educación-y-humanidades (2)Facultad de-educación-y-humanidades (2)
Facultad de-educación-y-humanidades (2)Iandra Castillo
 
How to increase email conversion rates
How to increase email conversion ratesHow to increase email conversion rates
How to increase email conversion ratesTambourine
 
Presentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sasPresentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sasMichele Borsoi
 
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...American Academy of Pain Management
 
My time brochure april 2014 web
My time brochure april 2014 webMy time brochure april 2014 web
My time brochure april 2014 webmytimemk
 

Destacado (15)

Ssc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivilSsc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivil
 
Sanghamitra Jayant on the GoUNESCO India 2014 Challenge
Sanghamitra Jayant on the GoUNESCO India 2014 ChallengeSanghamitra Jayant on the GoUNESCO India 2014 Challenge
Sanghamitra Jayant on the GoUNESCO India 2014 Challenge
 
Controller in asp.net mvc
Controller in asp.net mvcController in asp.net mvc
Controller in asp.net mvc
 
5 Ways to Increase Mobile conversion rates
5 Ways to Increase Mobile conversion rates5 Ways to Increase Mobile conversion rates
5 Ways to Increase Mobile conversion rates
 
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...
Guidaallaconoscenzadellapolizzadiresponsabilitcivile 141122091752-conversion-...
 
Bab ii pembahasan
Bab ii pembahasanBab ii pembahasan
Bab ii pembahasan
 
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0
PRESENTAS OUTPUT DEVICE (UNIT KELUARAN0
 
Ylki ppt
Ylki pptYlki ppt
Ylki ppt
 
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...
Dottori Commercialisti - Guida alla conoscenza della polizza di responsabilit...
 
Facultad de-educación-y-humanidades (2)
Facultad de-educación-y-humanidades (2)Facultad de-educación-y-humanidades (2)
Facultad de-educación-y-humanidades (2)
 
How to increase email conversion rates
How to increase email conversion ratesHow to increase email conversion rates
How to increase email conversion rates
 
Presentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sasPresentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sas
 
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...
American Academy of Pain Management 25th Annual Clinical Meeting-Phoenix, AZ ...
 
Helsinki
HelsinkiHelsinki
Helsinki
 
My time brochure april 2014 web
My time brochure april 2014 webMy time brochure april 2014 web
My time brochure april 2014 web
 

Similar a ITFT_Semaphores and bounded buffer

Operating Systems 1 (8/12) - Concurrency
Operating Systems 1 (8/12) - ConcurrencyOperating Systems 1 (8/12) - Concurrency
Operating Systems 1 (8/12) - ConcurrencyPeter Tröger
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterLucidworks
 
Full solution to bounded buffer
Full solution to bounded bufferFull solution to bounded buffer
Full solution to bounded bufferSyed Zaid Irshad
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.pptjayverma27
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaAdil Mehmoood
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationGift Kaliza
 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronizationKlintonChhun
 
Testing multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problemsTesting multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problemsVassil Popovski
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waitingRoman Elizarov
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrencyyoavrubin
 
Verification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAVerification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAUniversität Rostock
 
Design for Test [DFT]-1 (1).pdf DESIGN DFT
Design for Test [DFT]-1 (1).pdf DESIGN DFTDesign for Test [DFT]-1 (1).pdf DESIGN DFT
Design for Test [DFT]-1 (1).pdf DESIGN DFTjayasreenimmakuri777
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptxARUNPRANESHS
 

Similar a ITFT_Semaphores and bounded buffer (20)

Operating Systems 1 (8/12) - Concurrency
Operating Systems 1 (8/12) - ConcurrencyOperating Systems 1 (8/12) - Concurrency
Operating Systems 1 (8/12) - Concurrency
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, Twitter
 
Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
 
Lect04
Lect04Lect04
Lect04
 
Full solution to bounded buffer
Full solution to bounded bufferFull solution to bounded buffer
Full solution to bounded buffer
 
OS Semaphore.pptx
OS Semaphore.pptxOS Semaphore.pptx
OS Semaphore.pptx
 
Switching 1
Switching 1Switching 1
Switching 1
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronization
 
Testing multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problemsTesting multithreaded java applications for synchronization problems
Testing multithreaded java applications for synchronization problems
 
Non blocking programming and waiting
Non blocking programming and waitingNon blocking programming and waiting
Non blocking programming and waiting
 
Clojure's take on concurrency
Clojure's take on concurrencyClojure's take on concurrency
Clojure's take on concurrency
 
Verification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLAVerification with LoLA: 4 Using LoLA
Verification with LoLA: 4 Using LoLA
 
13.ppt
13.ppt13.ppt
13.ppt
 
Design for Test [DFT]-1 (1).pdf DESIGN DFT
Design for Test [DFT]-1 (1).pdf DESIGN DFTDesign for Test [DFT]-1 (1).pdf DESIGN DFT
Design for Test [DFT]-1 (1).pdf DESIGN DFT
 
Lecture6
Lecture6Lecture6
Lecture6
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 

Más de Sneh Prabha

ITFT_Telephone network
ITFT_Telephone networkITFT_Telephone network
ITFT_Telephone networkSneh Prabha
 
ITFT_Switching techniques in networking
ITFT_Switching techniques in networkingITFT_Switching techniques in networking
ITFT_Switching techniques in networkingSneh Prabha
 
ITFT_Osi reference model
ITFT_Osi reference modelITFT_Osi reference model
ITFT_Osi reference modelSneh Prabha
 
ITFT_Computer Network
ITFT_Computer NetworkITFT_Computer Network
ITFT_Computer NetworkSneh Prabha
 
ITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communicationITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communicationSneh Prabha
 
ITFT_Introduction to Operating system
ITFT_Introduction to Operating systemITFT_Introduction to Operating system
ITFT_Introduction to Operating systemSneh Prabha
 
ITFT_Inter process communication
ITFT_Inter process communicationITFT_Inter process communication
ITFT_Inter process communicationSneh Prabha
 
ITFT_File system interface in Operating System
ITFT_File system interface in Operating SystemITFT_File system interface in Operating System
ITFT_File system interface in Operating SystemSneh Prabha
 
ITFT_Data Link Layer issues
ITFT_Data Link Layer  issuesITFT_Data Link Layer  issues
ITFT_Data Link Layer issuesSneh Prabha
 
ITFT_Device management in Operating System
ITFT_Device management in Operating SystemITFT_Device management in Operating System
ITFT_Device management in Operating SystemSneh Prabha
 

Más de Sneh Prabha (10)

ITFT_Telephone network
ITFT_Telephone networkITFT_Telephone network
ITFT_Telephone network
 
ITFT_Switching techniques in networking
ITFT_Switching techniques in networkingITFT_Switching techniques in networking
ITFT_Switching techniques in networking
 
ITFT_Osi reference model
ITFT_Osi reference modelITFT_Osi reference model
ITFT_Osi reference model
 
ITFT_Computer Network
ITFT_Computer NetworkITFT_Computer Network
ITFT_Computer Network
 
ITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communicationITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communication
 
ITFT_Introduction to Operating system
ITFT_Introduction to Operating systemITFT_Introduction to Operating system
ITFT_Introduction to Operating system
 
ITFT_Inter process communication
ITFT_Inter process communicationITFT_Inter process communication
ITFT_Inter process communication
 
ITFT_File system interface in Operating System
ITFT_File system interface in Operating SystemITFT_File system interface in Operating System
ITFT_File system interface in Operating System
 
ITFT_Data Link Layer issues
ITFT_Data Link Layer  issuesITFT_Data Link Layer  issues
ITFT_Data Link Layer issues
 
ITFT_Device management in Operating System
ITFT_Device management in Operating SystemITFT_Device management in Operating System
ITFT_Device management in Operating System
 

Último

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Último (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

ITFT_Semaphores and bounded buffer

  • 2. Semaphores • Semaphore is a type of generalized lock • Defined by Dijkstra in the last 60s • Main synchronization primitives used in UNIX • Consist of a positive integer value • Two operations • P(): an atomic operation that waits for semaphore to become positive, then decrement it by 1 • V(): an atomic operation that increments semaphore by 1 and wakes up a waiting thread at P(), if any.
  • 3. Semaphores vs. Integers • No negative values • Only operations are P() and V() • Cannot read or write semaphore values • Except at the initialization times • Operations are atomic • Two P() calls cannot decrement the value below zero • A sleeping thread at P() cannot miss a wakeup from V()
  • 4. Binary Semaphores • A binary semaphore is initialized to 1 • P() waits until the value is 1 • Then set it to 0 • V() sets the value to 1 • Wakes up a thread waiting at P(), if any
  • 5. Two Uses of Semaphores 1. Mutual exclusion • Lock was designed to do this lock->acquire(); // critical section lock->release();
  • 6. Two Uses of Semaphores 1. Mutual exclusion 1. The lock function can be realized with a binary semaphore: semaphore subsumes lock. • Semaphore has an initial value of 1 • P() is called before a critical section • V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box);
  • 7. Two Uses of Semaphores 1. Mutual exclusion • Semaphore has an initial value of 1 • P() is called before a critical section • V() is called after the critical section semaphore litter_box = 1; P(litter_box); // critical section V(litter_box); litter_box = 1
  • 8. Two Uses of Semaphores 1. Mutual exclusion • Semaphore has an initial value of 1 • P() is called before a critical section • V() is called after the critical section semaphore litter_box = 1; P(litter_box); // purrr… // critical section V(litter_box); litter_box = 1  0
  • 9. Producer-Consumer with a Bounded Buffer • A classic problem • A producer put things into a shared buffer • A consumer takes them out
  • 10. Problem Constraints • The solution involves both synchronization and mutual exclusion • Constraints • The consumer must wait if buffers are empty (synchronization constraint) • The producer must wait if buffers are full (synchronization constraint) • Only one thread can manipulate the buffer at a time (mutual exclusion)
  • 11. Implementing Semaphore • How to implement semaphore? • Almost exactly like lock. • Using spinlock or queue • What hardware support is needed? • Interrupt disable • Test-and-set
  • 12. Implementing Semaphore class semaphore { int value; } semaphore::semaphore(int i) { value = i; } semaphore::p() { // disable interrupts while (value == 0) { // enable interrupts // disable interrupts } value --; // enable interrupts } semaphore::v() { // disable interrupts value ++; // enable interrupts }
  • 13. Implementing Semaphore with test and set class semaphore { int value; } semaphore::semaphore(int i) { value = i; } semaphore::p() { while (test_and_set(guard)); while (value == 0) { // queue the thread // guard = 0 and sleep } value --; guard = 0; semaphore::v() { while (test_and_set(guard)); if (anyone waiting) { // wake up one thread // put in on ready queue } else { value ++; } guard = 0; }
  • 14. Semaphore in UNIX • Managing concurrent access to shared memory. • Semaphore system calls • Creation: semget( … ) • Incr/Decr/set : semop(…) • Deletion: semctl(semid, 0, IPC_RMID, 0); • See examples: seminit.c, sema.c semb.c