SlideShare a Scribd company logo
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

More Related Content

What's hot

Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
sathish sak
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
6 Synchronisation
6 Synchronisation6 Synchronisation
6 Synchronisation
Dr. Loganathan R
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphoresanandammca
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
Shijin Raj P
 
Semaphore
SemaphoreSemaphore
Semaphore
Arafat Hossan
 
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 Monitors
sgpraju
 
Operating System-Ch6 process synchronization
Operating System-Ch6 process synchronizationOperating System-Ch6 process synchronization
Operating System-Ch6 process synchronization
Syaiful Ahdan
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
Operating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and DeadlocksOperating Systems - Process Synchronization and Deadlocks
Operating Systems - Process Synchronization and Deadlocks
Mukesh 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 POWERPOINT
Anne Lee
 
Operating system critical section
Operating system   critical sectionOperating system   critical section
Operating system critical section
Harshana Madusanka Jayamaha
 
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 bank
anuradha raheja
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
Nishant Joshi
 

What's hot (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
 

Viewers also liked

Ssc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivilSsc main conventional paper 2014 electricalcivil
Ssc main conventional paper 2014 electricalcivil
Deepak 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 Challenge
GoUNESCO
 
Controller in asp.net mvc
Controller in asp.net mvcController in asp.net mvc
Controller in asp.net mvc
Reza 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 rates
Tambourine
 
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 KELUARAN0
dwi 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 rates
Tambourine
 
Presentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sasPresentazione due diligence assicurativa sas
Presentazione due diligence assicurativa sas
Michele 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 web
mytimemk
 

Viewers also liked (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 to 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, Twitter
Lucidworks
 
Ch5 process synchronization
Ch5   process synchronizationCh5   process synchronization
Ch5 process synchronization
Welly Dian Astika
 
Full solution to bounded buffer
Full solution to bounded bufferFull solution to bounded buffer
Full solution to bounded buffer
Syed Zaid Irshad
 
OS Semaphore.pptx
OS Semaphore.pptxOS Semaphore.pptx
OS Semaphore.pptx
AbuBakkarShayan
 
Switching 1
Switching 1Switching 1
Switching 1
Kishore Kumar
 
Process Synchronization -1.ppt
Process Synchronization -1.pptProcess Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptx
AbuBakkarShayan
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Adil Mehmoood
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
Gift Kaliza
 
Lecture 5 process synchronization
Lecture 5 process synchronizationLecture 5 process synchronization
Lecture 5 process synchronization
KlintonChhun
 
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 waiting
Roman 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
 
13.ppt
13.ppt13.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
jayasreenimmakuri777
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
ARUNPRANESHS
 

Similar to 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
 

More from Sneh Prabha

ITFT_Telephone network
ITFT_Telephone networkITFT_Telephone network
ITFT_Telephone network
Sneh Prabha
 
ITFT_Switching techniques in networking
ITFT_Switching techniques in networkingITFT_Switching techniques in networking
ITFT_Switching techniques in networking
Sneh Prabha
 
ITFT_Osi reference model
ITFT_Osi reference modelITFT_Osi reference model
ITFT_Osi reference model
Sneh Prabha
 
ITFT_Computer Network
ITFT_Computer NetworkITFT_Computer Network
ITFT_Computer Network
Sneh Prabha
 
ITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communicationITFT_Microwave, infrared & bluetooth communication
ITFT_Microwave, infrared & bluetooth communication
Sneh Prabha
 
ITFT_Introduction to Operating system
ITFT_Introduction to Operating systemITFT_Introduction to Operating system
ITFT_Introduction to Operating system
Sneh Prabha
 
ITFT_Inter process communication
ITFT_Inter process communicationITFT_Inter process communication
ITFT_Inter process communication
Sneh 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 System
Sneh Prabha
 
ITFT_Data Link Layer issues
ITFT_Data Link Layer  issuesITFT_Data Link Layer  issues
ITFT_Data Link Layer issues
Sneh Prabha
 
ITFT_Device management in Operating System
ITFT_Device management in Operating SystemITFT_Device management in Operating System
ITFT_Device management in Operating System
Sneh Prabha
 

More from 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
 

Recently uploaded

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 

Recently uploaded (20)

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

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