solve this problem

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

solve this problem

Postby vikram » Thu Mar 17, 2005 4:48 am

Hi All,

Can anybody solve this problem?

Write a program in C++ and Win32 API to implement an unnamed pipe. Your program (a process) should contain two threads (“producer” and “consumer”) communicating through the pipe which is implemented using an array having 10 entries (i.e. 0..9). Access to the pipe should be synchronized using a semaphore implemented as a “class”. The P and V operations supported by the semaphore should be executed atomically which is enforced using Win32 API functions (e.g. the CreateSemaphore function). Note that P and V are the only operations supported by the semaphore class.

The producer thread gets an integer (a single digit, 0..9) from the user and enqueues it into the pipe. The consumer thread always dequeues the first number in the pipe (if there is any) and consumes it. Both threads then have to “move” the items already in the pipe toward the front of the pipe after an item has been deposited (by the producer) or consumed (by the consumer).

You should use another “class” (i.e. the pipe class) to implement the pipe supporting operations “enqueue”, dequeue”, and “move”. The first two operations have to be synchronized using the semaphore shown above (operation “move” is not synchronized because it is invoked by the first two operations which can directly access the shared pipe).

Note that your program should also be able to take care of the conditions when the pipe is either empty or full. When the consumer thread detects an empty pipe, it prints a message to the screen. Similarly, when the producer thread detects a full pipe, it prints a message to the screen too.

Note also that you should always delay the consumer thread by 3 seconds by inserting a “Sleep(3000)” (without quotes) function into the appropriate place in the consumer thread.

The producer thread should show the contents of the pipe after a number is enqueued. The consumer should also show the contents of the pipe after a number is dequeued.

The following shows a scenario of running the program, where user’s inputs are in boldfaces.

>smi3456p2 {an executable program}
enter a number:5 {from the producer}
the contents of the pipe (from producer):bbbbbbbbb5 {b stands for blank}
enter a number:2 {from the producer}
the contents of the pipe (from producer):bbbbbbbb25
the contents of the pipe (from consumer):bbbbbbbbb2
enter a number:7 {from the producer}
the contents of the pipe (from producer):bbbbbbbb72
the contents of the pipe (from consumer):bbbbbbbbb7
enter a number: {from the producer}
the contents of the pipe (from consumer):bbbbbbbbbb
*** an empty pipe is detected (from consumer)!!!
. . . . . . . . . . . . . . . . . . . .

Note that the command “enter a number:” from the producer thread cannot block the consumer from running.

Postby Damon » Fri Mar 18, 2005 11:20 pm

Hi vikram,
There are several articles here which should guide you in developing such an application...

M. Cashman, The basics of multi-tier applications

R. Nagal, Piping’s Hot!

M. Smith, Synchronization by Policy

Good luck,
Damon Chandler
C++Builder Developer's Journal
User avatar
BCBJ Editor and Admin
BCBJ Editor and Admin
Posts: 285
Joined: Wed May 26, 2004 11:25 pm
Location: Stillwater, OK, USA

Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 23 guests