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

PPT
Process Synchronization
PPTX
Process synchronization
PPT
Ch7 OS
 
PPT
Process Synchronization
PPTX
Process synchronization
PDF
Semaphores
PPT
Chapter 6 - Process Synchronization
Process Synchronization
Process synchronization
Ch7 OS
 
Process Synchronization
Process synchronization
Semaphores
Chapter 6 - Process Synchronization

What's hot (20)

PPT
Semaphores and Monitors
PPT
PPT
Process synchronization(deepa)
PDF
6 Synchronisation
PPT
Lec11 semaphores
PPT
Semaphores OS Basics
PPTX
Semaphore
PDF
Monitors
PDF
Synchronization
PPT
OS Process Synchronization, semaphore and Monitors
PDF
Operating System-Ch6 process synchronization
PPTX
Concurrency: Mutual Exclusion and Synchronization
PDF
Operating Systems - Process Synchronization and Deadlocks
PPTX
SYNCHRONIZATION
PPSX
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
PPTX
Operating system critical section
PPT
Synchronization linux
PPT
Operating Systems - "Chapter 5 Process Synchronization"
PPTX
Operating systems question bank
PPTX
Semophores and it's types
Semaphores and Monitors
Process synchronization(deepa)
6 Synchronisation
Lec11 semaphores
Semaphores OS Basics
Semaphore
Monitors
Synchronization
OS Process Synchronization, semaphore and Monitors
Operating System-Ch6 process synchronization
Concurrency: Mutual Exclusion and Synchronization
Operating Systems - Process Synchronization and Deadlocks
SYNCHRONIZATION
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
Operating system critical section
Synchronization linux
Operating Systems - "Chapter 5 Process Synchronization"
Operating systems question bank
Semophores and it's types
Ad

Viewers also liked (15)

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

Similar to ITFT_Semaphores and bounded buffer (20)

PPT
memory management Operating System7-sema_mon.ppt
PPTX
Operating system 24 mutex locks and semaphores
PPT
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
PPTX
Process synchronization in Operating Systems
DOCX
Implementing of classical synchronization problem by using semaphores
PPT
Classic synchronization
PPTX
Interprocess Communication important topic in iOS .pptx
PPT
Lecture18-19 (1).ppt
PPTX
Chapter05 new
PPT
Mca ii os u-2 process management & communication
PPT
14-Semaphores.ppt
PPT
Process Synchronization -1.ppt
PPT
Inter process communication
PPTX
Semaphore
PDF
Process coordination
PPTX
Semaphores SEMAPHORES Semaphores SEMAPHORES
PDF
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PPTX
Operating system 27 semaphores
PPTX
Interprocess Communication
PDF
23 concurrency-examples
memory management Operating System7-sema_mon.ppt
Operating system 24 mutex locks and semaphores
CChapter4.pptCChapter4.pptCChapter4.pptCChapter4.pptCChapter4.ppt
Process synchronization in Operating Systems
Implementing of classical synchronization problem by using semaphores
Classic synchronization
Interprocess Communication important topic in iOS .pptx
Lecture18-19 (1).ppt
Chapter05 new
Mca ii os u-2 process management & communication
14-Semaphores.ppt
Process Synchronization -1.ppt
Inter process communication
Semaphore
Process coordination
Semaphores SEMAPHORES Semaphores SEMAPHORES
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
Operating system 27 semaphores
Interprocess Communication
23 concurrency-examples

More from Sneh Prabha (10)

PDF
ITFT_Telephone network
PDF
ITFT_Switching techniques in networking
PDF
ITFT_Osi reference model
PDF
ITFT_Computer Network
PDF
ITFT_Microwave, infrared & bluetooth communication
PDF
ITFT_Introduction to Operating system
PDF
ITFT_Inter process communication
PDF
ITFT_File system interface in Operating System
PDF
ITFT_Data Link Layer issues
PDF
ITFT_Device management in Operating System
ITFT_Telephone network
ITFT_Switching techniques in networking
ITFT_Osi reference model
ITFT_Computer Network
ITFT_Microwave, infrared & bluetooth communication
ITFT_Introduction to Operating system
ITFT_Inter process communication
ITFT_File system interface in Operating System
ITFT_Data Link Layer issues
ITFT_Device management in Operating System

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
master seminar digital applications in india
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
human mycosis Human fungal infections are called human mycosis..pptx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
102 student loan defaulters named and shamed – Is someone you know on the list?
Week 4 Term 3 Study Techniques revisited.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
master seminar digital applications in india
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
01-Introduction-to-Information-Management.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