SlideShare a Scribd company logo
Bounded Buffer
Producer/Consumer Problem
หลายๆเทรดที่ทางานอยู่ในหนึ่งโปรเซสจะใช้หน่วยความจาและทรัพยากรต่างๆ
ร่วมกัน ดังนั้นจึงต้องมีการประสานงานการทางาน ไม่ให้มีการรบกวนกระบวนการทางาน
จากเทรดอื่นๆ กลไกการทางานทางด้าน synchronization ทั้งหมดของวินโดว์
สามารถใช้งานได้กับ เทรด ที่อยู่ใน โปรเซส
Traffic Intersection
CriticalSections
shared double balance;
Code for p1 Code for p2
. . . . . .
balance = balance + amount; balance = balance - amount;
. . . . . .
Traffic Intersection(2)
…
load R1, balance
load R2, amount
…
load R1, balance
load R2, amount
sub R1, R2
store R1, balance
…
add R1, R2
store R1, balance
…
Execution of p1 Execution of p2
//R1=1000
//R2=500
//R1=1000
//R2=500
//R1-R2=500
//balance=500
//R1+R2=1500
//balance=1500
A Semaphore
ผู้สร้างSemaphores
• ถูกสร้างในปี 1968 ประมาณ 44 ปีมาแล้ว
• เป็นพื้นฐานของเทคนิคสมัยใหม่ในการแก้ปัญหาการ
synchronization
และยังคงเป็นวิธีที่ใช้จัดการการติดต่อสื่อสาร
กันของ process ที่ทางานร่วมกัน
Edsger Wybe Dijkstra
Operation of Semaphores
A Semaphores
คือ ตัวแปรจานวนเต็มตั้งแต่ 0 ขึ้นไป ซึ่งเวลาจะเพิ่มหรือลดค่าตัวแปร
จะทาผ่าน 2 ฟังก์ชันดังนี้
V(s): [s = s + 1]
P(s): [while(s == 0) {wait}; s = s - 1]
Bounded Buffer Problem
Producer/Consumer Problem
Producer Consumer
Empty Pool
Full Pool
คือปัญหาคลาสสิคทางด้าน synchronization ที่นาเสนอโดย Dijkstra เพื่อนแสดงให้เห็น
วิธีการใช้งาน semaphores ในสองรูปแบบ
3 Semaphores
Type 1 : Binary semaphores
1. A mutual exclusion semaphore to prevent the
producer and consumer from manipulating the list of
buffers at the same time
Type 2 : General semaphores (counting semaphores)
2. A semaphore so that the producer can signal the
consumer to start processing when it creates a full
buffer
3. A semaphore for the consumer to signal the
producer when it creates an empty buffer
Animation : Bounded Buffer Problem
• A mutual exclusion semaphore (bufManip)
• General semaphores (empty , full)
Declaration 3 semaphores
Size of Buffers
Design and implement a process with a producer and consumer
thread using N different buffers (use a fixed size for N of 20).
Create semaphores
A producer and a consumer thread
are createdby a parent thread
Producer thread
Consumer thread
Runtime
Terminate thread & สิ่งที่ทาเพิ่มเติม

More Related Content

What's hot

04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
Sokngim Sa
 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introduction
cscarcas
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory Structure
Kevin OBrien
 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's Problem
Yash Mittal
 
Process management
Process managementProcess management
Process management
Birju Tank
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
Sourabh Sahu
 
Multiprocessor Scheduling
Multiprocessor SchedulingMultiprocessor Scheduling
Multiprocessor Scheduling
Khadija Saleem
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databases
James Serra
 
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptxEX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
vishal choudhary
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
Mohd Tousif
 
Locks In Disributed Systems
Locks In Disributed SystemsLocks In Disributed Systems
Locks In Disributed Systems
mridul mishra
 
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
Agile Testing Alliance
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Top 5 Server Operating Systems
Top 5 Server Operating SystemsTop 5 Server Operating Systems
Top 5 Server Operating Systems
Sinbad Konick
 
Cassandra/Hadoop Integration
Cassandra/Hadoop IntegrationCassandra/Hadoop Integration
Cassandra/Hadoop Integration
Jeremy Hanna
 
Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithms
achinth
 
The Hadoop Ecosystem
The Hadoop EcosystemThe Hadoop Ecosystem
The Hadoop EcosystemJ Singh
 
Naming in Distributed System
Naming in Distributed SystemNaming in Distributed System
Naming in Distributed System
MNM Jain Engineering College
 
Inter-Process Communication in distributed systems
Inter-Process Communication in distributed systemsInter-Process Communication in distributed systems
Inter-Process Communication in distributed systems
Aya Mahmoud
 

What's hot (20)

04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
 
Operating System - Unit I - Introduction
Operating System - Unit I - IntroductionOperating System - Unit I - Introduction
Operating System - Unit I - Introduction
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory Structure
 
Dining Philosopher's Problem
Dining Philosopher's ProblemDining Philosopher's Problem
Dining Philosopher's Problem
 
Process management
Process managementProcess management
Process management
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
 
Multiprocessor Scheduling
Multiprocessor SchedulingMultiprocessor Scheduling
Multiprocessor Scheduling
 
Relational databases vs Non-relational databases
Relational databases vs Non-relational databasesRelational databases vs Non-relational databases
Relational databases vs Non-relational databases
 
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptxEX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
EX-6-Implement Matrix Multiplication with Hadoop Map Reduce.pptx
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 
Locks In Disributed Systems
Locks In Disributed SystemsLocks In Disributed Systems
Locks In Disributed Systems
 
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
Introduction To Big Data with Hadoop and Spark - For Batch and Real Time Proc...
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
 
Local storage
Local storageLocal storage
Local storage
 
Top 5 Server Operating Systems
Top 5 Server Operating SystemsTop 5 Server Operating Systems
Top 5 Server Operating Systems
 
Cassandra/Hadoop Integration
Cassandra/Hadoop IntegrationCassandra/Hadoop Integration
Cassandra/Hadoop Integration
 
Garbage collection algorithms
Garbage collection algorithmsGarbage collection algorithms
Garbage collection algorithms
 
The Hadoop Ecosystem
The Hadoop EcosystemThe Hadoop Ecosystem
The Hadoop Ecosystem
 
Naming in Distributed System
Naming in Distributed SystemNaming in Distributed System
Naming in Distributed System
 
Inter-Process Communication in distributed systems
Inter-Process Communication in distributed systemsInter-Process Communication in distributed systems
Inter-Process Communication in distributed systems
 

Viewers also liked

Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronizationShakshi Ranawat
 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
READER/WRITER SOLUTION
READER/WRITER SOLUTIONREADER/WRITER SOLUTION
READER/WRITER SOLUTION
Shahzeb Pirzada
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
Ruaha Catholic university
 
DPP
DPPDPP
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
Shijin Raj P
 
Application Layer
Application Layer Application Layer
Application Layer
Dr Shashikant Athawale
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OSC.U
 
ITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded bufferITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded buffer
Sneh Prabha
 
SlideShare 101
SlideShare 101SlideShare 101
SlideShare 101
Amit Ranjan
 

Viewers also liked (13)

Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Producer Consumer Problem
Producer Consumer Problem  Producer Consumer Problem
Producer Consumer Problem
 
Operating systems
Operating systemsOperating systems
Operating systems
 
READER/WRITER SOLUTION
READER/WRITER SOLUTIONREADER/WRITER SOLUTION
READER/WRITER SOLUTION
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
 
DPP
DPPDPP
DPP
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Application Layer
Application Layer Application Layer
Application Layer
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Ch7 OS
Ch7 OSCh7 OS
Ch7 OS
 
ITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded bufferITFT_Semaphores and bounded buffer
ITFT_Semaphores and bounded buffer
 
SlideShare 101
SlideShare 101SlideShare 101
SlideShare 101
 

More from Bhuridech Sudsee

Kafka for developer
Kafka for developerKafka for developer
Kafka for developer
Bhuridech Sudsee
 
Xss and sql injection
Xss and sql injectionXss and sql injection
Xss and sql injection
Bhuridech Sudsee
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
Bhuridech Sudsee
 
$ Spark start
$  Spark start$  Spark start
$ Spark start
Bhuridech Sudsee
 
เสี่ยวเอ้อสอน Spark
เสี่ยวเอ้อสอน Sparkเสี่ยวเอ้อสอน Spark
เสี่ยวเอ้อสอน Spark
Bhuridech Sudsee
 
Jpa sa-60
Jpa sa-60Jpa sa-60
Jpa sa-60
Bhuridech Sudsee
 
18 ฝ่ามือพิชิต docker
18 ฝ่ามือพิชิต docker18 ฝ่ามือพิชิต docker
18 ฝ่ามือพิชิต docker
Bhuridech Sudsee
 
Cloud computing
Cloud computingCloud computing
Cloud computing
Bhuridech Sudsee
 
หนังสือภาษาไทย Spark Internal
หนังสือภาษาไทย Spark Internalหนังสือภาษาไทย Spark Internal
หนังสือภาษาไทย Spark Internal
Bhuridech Sudsee
 
VBoxManage tutorial
VBoxManage tutorialVBoxManage tutorial
VBoxManage tutorial
Bhuridech Sudsee
 
Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2
Bhuridech Sudsee
 
sample plot 3D form depth map using OpenCV
sample plot 3D form depth map using OpenCVsample plot 3D form depth map using OpenCV
sample plot 3D form depth map using OpenCV
Bhuridech Sudsee
 
illustrator & design workshop
illustrator & design workshopillustrator & design workshop
illustrator & design workshop
Bhuridech Sudsee
 
operating system
operating systemoperating system
operating system
Bhuridech Sudsee
 
Producer and Consumer problem
Producer and Consumer problemProducer and Consumer problem
Producer and Consumer problem
Bhuridech Sudsee
 
OPD System with ZK Grails
OPD System with ZK GrailsOPD System with ZK Grails
OPD System with ZK Grails
Bhuridech Sudsee
 
Market management with ZK Grails
Market management with ZK GrailsMarket management with ZK Grails
Market management with ZK Grails
Bhuridech Sudsee
 
Websocket & HTML5
Websocket & HTML5Websocket & HTML5
Websocket & HTML5
Bhuridech Sudsee
 
Networking section present
Networking section presentNetworking section present
Networking section present
Bhuridech Sudsee
 
Breast Cancer data mining KDD
Breast Cancer data mining KDDBreast Cancer data mining KDD
Breast Cancer data mining KDD
Bhuridech Sudsee
 

More from Bhuridech Sudsee (20)

Kafka for developer
Kafka for developerKafka for developer
Kafka for developer
 
Xss and sql injection
Xss and sql injectionXss and sql injection
Xss and sql injection
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
$ Spark start
$  Spark start$  Spark start
$ Spark start
 
เสี่ยวเอ้อสอน Spark
เสี่ยวเอ้อสอน Sparkเสี่ยวเอ้อสอน Spark
เสี่ยวเอ้อสอน Spark
 
Jpa sa-60
Jpa sa-60Jpa sa-60
Jpa sa-60
 
18 ฝ่ามือพิชิต docker
18 ฝ่ามือพิชิต docker18 ฝ่ามือพิชิต docker
18 ฝ่ามือพิชิต docker
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
หนังสือภาษาไทย Spark Internal
หนังสือภาษาไทย Spark Internalหนังสือภาษาไทย Spark Internal
หนังสือภาษาไทย Spark Internal
 
VBoxManage tutorial
VBoxManage tutorialVBoxManage tutorial
VBoxManage tutorial
 
Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2
 
sample plot 3D form depth map using OpenCV
sample plot 3D form depth map using OpenCVsample plot 3D form depth map using OpenCV
sample plot 3D form depth map using OpenCV
 
illustrator & design workshop
illustrator & design workshopillustrator & design workshop
illustrator & design workshop
 
operating system
operating systemoperating system
operating system
 
Producer and Consumer problem
Producer and Consumer problemProducer and Consumer problem
Producer and Consumer problem
 
OPD System with ZK Grails
OPD System with ZK GrailsOPD System with ZK Grails
OPD System with ZK Grails
 
Market management with ZK Grails
Market management with ZK GrailsMarket management with ZK Grails
Market management with ZK Grails
 
Websocket & HTML5
Websocket & HTML5Websocket & HTML5
Websocket & HTML5
 
Networking section present
Networking section presentNetworking section present
Networking section present
 
Breast Cancer data mining KDD
Breast Cancer data mining KDDBreast Cancer data mining KDD
Breast Cancer data mining KDD
 

The bounded buffer