SlideShare a Scribd company logo
1 of 14
Download to read offline
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 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
 

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

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, 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 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 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
 

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

Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 

Recently uploaded (20)

FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.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