ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Project for 423302 Operating System
Term 1/2555
Bounded Buffer Problem in
Microsoft Windows OS
By Present Group 01 Section 01
B5302338 KANOKON CHANSOM
B5302567 JATUPHON PANKAEO
B5306664 AITSARUT NENCHOO
B5304516 POORIDACH SUDSEE
Background:
In concurrent programming a critical section is a piece of code that accesses a shared resource
(data structure or device) that must not be concurrently accessed by more than one thread of execution.
A critical section will usually terminate in fixed time, and a thread, task or process will have to wait a
fixed time to enter it (aka bounded waiting). Some synchronization mechanism is required at the entry
and exit of the critical section to ensure exclusive use, for example a semaphore.
Semaphore Type
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
Highlight:
The bounded buffer (producer-consumer) problem is a classic synchronization problem
introduced by Dijkstra to illustrate two different ways to use his semaphores. In this report, we will design
two threads to execute in a single address space. A producer thread creates ¡°widgets,¡± and places each
widget in an empty buffer for consumption by the consumer thread. The consumer retrieves the widget
from the buffer, then releases the buffer to an empty buffer pool. If there are no full buffers, the
consumer is blocked until new widgets are produced. If there are no empty buffers available when the
producer creates a widget, the producer thread must wait until the consumer thread releases an empty
buffer.
This problem asks we to design and implement a process with a producer and consumer thread
using N different buffers (use a fixed size for N of 20). Base your solution on the solution to the producer-
consumer problem. You will need a mutual exclusion semaphore to prevent the producer and consumer
from manipulating the list of buffers at the same time, a semaphore so that the producer can signal the
consumer to start processing when it creates a full buffer, and another semaphore for the consumer to
signal the producer when it creates an empty buffer.
Details:
3 steps for projects
1) Compile and run
- Using Dev C++ 4.9.9.2 for compile and run program
2) Try to study code.
Source code have 8 important sections ??????
2.1) Declaration 3 semaphores
- A mutual exclusion semaphore (bufManip)
- General semaphores (empty , full)
2.2) 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).
2.3) Create semaphores
2.4) A producer and a consumer thread
are created by a parent thread
2.5) Producer thread function
2.6) Consumer thread function
2.7) Set Runtime and Sleep
2.8) Terminate thread
3) Modified code.
Add code for show number of empty and full semaphore after terminate producer and
consumer process.
Output:
- ¡°Project1¡± is execution file for run.
- ¡°2¡± define runtime in second. (because this parameter will be multiply with 1000 in Main thread)
- Line 2 show when Main thread create Producer thread
- Line 3 show when Main thread create Consumer thread
- Line 1 ¡°#45¡± refer to Producer run 46 times (0-45 block of buffer) and ¡°#5¡± mean current buffer
position for Producer.
- Line 2 ¡°#27¡± refer to Consumer run 28 times (0-27 block of buffer) and ¡°#7¡± mean current buffer
position for Consumer.
- Current buffer position can¡¯t over 20 (Position 0-19) because maximum define is 20 block.
- Line 1 Consumer Terminating thread.
- Line 2 Producer thread running
- Line 3 Producer Terminating thread.
- Both Producer and Consumer unnecessary terminated at the same time.
Extra-Output:
- Line 1 Last Empty
- Line 2 Last Full
- Line 4 Empty
- Line 5 Full
- Line 6 Terminate Main thread.
- Last Empty and Empty maybe Equal or not because Last Empty is last result from thread before
terminated but Empty get from all block of Buffer minus by Empty ( N ¨C Empty) and the same way for
Last Full and Full.
References:
1) http://www.bloodshed.net/devcpp.html
2) Gary NUTT, Operating Systems Third Edition
3) http://en.wikipedia.org/wiki/Critical_section

More Related Content

operating system

  • 1. Project for 423302 Operating System Term 1/2555 Bounded Buffer Problem in Microsoft Windows OS By Present Group 01 Section 01 B5302338 KANOKON CHANSOM B5302567 JATUPHON PANKAEO B5306664 AITSARUT NENCHOO B5304516 POORIDACH SUDSEE Background: In concurrent programming a critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will have to wait a fixed time to enter it (aka bounded waiting). Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use, for example a semaphore. Semaphore Type 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
  • 2. Highlight: The bounded buffer (producer-consumer) problem is a classic synchronization problem introduced by Dijkstra to illustrate two different ways to use his semaphores. In this report, we will design two threads to execute in a single address space. A producer thread creates ¡°widgets,¡± and places each widget in an empty buffer for consumption by the consumer thread. The consumer retrieves the widget from the buffer, then releases the buffer to an empty buffer pool. If there are no full buffers, the consumer is blocked until new widgets are produced. If there are no empty buffers available when the producer creates a widget, the producer thread must wait until the consumer thread releases an empty buffer. This problem asks we to design and implement a process with a producer and consumer thread using N different buffers (use a fixed size for N of 20). Base your solution on the solution to the producer- consumer problem. You will need a mutual exclusion semaphore to prevent the producer and consumer from manipulating the list of buffers at the same time, a semaphore so that the producer can signal the consumer to start processing when it creates a full buffer, and another semaphore for the consumer to signal the producer when it creates an empty buffer. Details: 3 steps for projects 1) Compile and run - Using Dev C++ 4.9.9.2 for compile and run program 2) Try to study code. Source code have 8 important sections ?????? 2.1) Declaration 3 semaphores - A mutual exclusion semaphore (bufManip) - General semaphores (empty , full)
  • 3. 2.2) 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). 2.3) Create semaphores 2.4) A producer and a consumer thread are created by a parent thread
  • 4. 2.5) Producer thread function 2.6) Consumer thread function
  • 5. 2.7) Set Runtime and Sleep 2.8) Terminate thread 3) Modified code. Add code for show number of empty and full semaphore after terminate producer and consumer process. Output: - ¡°Project1¡± is execution file for run. - ¡°2¡± define runtime in second. (because this parameter will be multiply with 1000 in Main thread) - Line 2 show when Main thread create Producer thread - Line 3 show when Main thread create Consumer thread - Line 1 ¡°#45¡± refer to Producer run 46 times (0-45 block of buffer) and ¡°#5¡± mean current buffer position for Producer. - Line 2 ¡°#27¡± refer to Consumer run 28 times (0-27 block of buffer) and ¡°#7¡± mean current buffer position for Consumer. - Current buffer position can¡¯t over 20 (Position 0-19) because maximum define is 20 block.
  • 6. - Line 1 Consumer Terminating thread. - Line 2 Producer thread running - Line 3 Producer Terminating thread. - Both Producer and Consumer unnecessary terminated at the same time. Extra-Output: - Line 1 Last Empty - Line 2 Last Full - Line 4 Empty - Line 5 Full - Line 6 Terminate Main thread. - Last Empty and Empty maybe Equal or not because Last Empty is last result from thread before terminated but Empty get from all block of Buffer minus by Empty ( N ¨C Empty) and the same way for Last Full and Full. References: 1) http://www.bloodshed.net/devcpp.html 2) Gary NUTT, Operating Systems Third Edition 3) http://en.wikipedia.org/wiki/Critical_section