Question

1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer...

1) Producer/Consumer again
Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer size is now 10. Assume the Producer process enters 3 items at a time into the buffer. It will only do this if there are at least 3 empty spots in the buffer. Assume the Consumer process will remove items two at a time, and will only do so if there are at least two items in the buffer. In addition to the Producer and Consumer processes, assume there is a third Hybrid process. Each time it accesses the buffer its role changes. One time it is a Producer entering a single item, and the next time it is a Consumer removing a single item. Write pseudocode for these three processes using semaphores, counters, etc. to handle this situation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int Buffer_size=10;
mutex=1;
empty=10;
full=0;
Solution for Producer process:
do{

//produce three items for inserting into buffer

wait(empty>=3);  //checks whether empty spots are atleast 3
wait(mutex);   //applys lock incase of empty 

//place in buffer 

signal(mutex);  //releases lock.
signal(full+3);  //increases the number of filled spots by 3.

}while(true)

Solution for Consumer problem:

do{

wait(full>=2);  //checks whether atleast two spots are filled in buffer.
wait(mutex);    //applys lock incase of available filled spots.

// remove 2 items from buffer

signal(mutex);   //releases lock.
signal(empty+2);  //increases the number of empty spots by 2.

// consume 2 items

}while(true)

Solution for Hybrid process:

int i=1;

do{

if(i%2!=0) //producer process at even times.

{

//produce an item

wait(empty); //checks for an empty spot

wait(mutex); //applys lock if any empty spot.

//place in buffer

signal(mutex); //releases lock

signal(full); //increases full by 1

i++;

}

if(i%2==0)

{

//consumer process at even times.

wait(full); //checks for filled spot.
wait(mutex); //applys lock.

// remove item from buffer

signal(mutex); //releases lock.
signal(empty); //increases empty spots by 1.

// consumes item

}

}while(true)

Hope this helps you..

Feel free to comment ,incase of any doubts :)

Add a comment
Know the answer?
Add Answer to:
1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining...

    Is This Correct? semaphore fillCount = 0; // Items produced semahore emptyCount = BUFFER_SIZE; // remaining space procedure producer() {    While (true) {    item = produceItem(); down(emptyCount); // emptyCount is decremented putItemIntoBuffer(item); up(fillCount); // fillcount is incremented } } procedure consumer() { While (true) {    down(fillCount); item = removeItem(); up(emptyCount); // emptyCount is incremented consumeItem(item); } } Instructions Programming Assignment Four In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer...

    Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer (C1). The maximum number of items in the queue is 100. Consider the following solution The functions qfull enque and deque are the standard functions discussed in class Assume they have been implemented correctly by one of the methods Semaphore ni=0, mutex=1; /* ni= number of items currently in the queue.*/ /* mutex is used to provide critical section. */ Code of a Producer...

  • I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I...

    I know is this posted else where BUT I NEED HELP IMPLEMENTING THE TIME DELAY!!!! I need a source comment next to the line that gives the delay so i can compare please. (Concurrency – Semaphores) 1.- In this assignment you will implement a deadlock free variant of the bounded-buffer producer/consumer using jBACI (C - -). C- - is a subset of C + + that allows you to declare semaphores and apply the operations P and V. In the...

  • 1. Consider a grocery supermarket planning to computerize their inventory management. The items on shelves will...

    1. Consider a grocery supermarket planning to computerize their inventory management. The items on shelves will be marked with Radio Frequency Identification (RFID) tags and a set of RFID reader-devices will be installed for monitoring the movements of the tagged items. Each tag carries a 96-bit EPC (Electronic Product Code) with a Global Trade Identification number, which is an international standard. The RFID readers are installed on each shelf on the sales floor. The RFID system consists of two types...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • Can you please write the two C++ modules below is a step by step instuctions to follow that will ...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

  • Can you please write the two C++ modules below is a step by step instuctions to...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

  • JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The...

    JAVA 3 LECTURE REVIEW PLEASE NEED ANSWERS ASAP. DUE IN AN HOUR!!! Question 12 points The best-case performance for a shell sort is: --- O(1) O(n2)   O(n) O(n log n)   Signaler cette question Question 22 points The best-case performance for an array of n items using insertion sort is: --- O(n2)   O(n) O(1) there is no best-case Signaler cette question Question 3 2 points A recursive method that processes a chain of linked nodes --- uses the first node in...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT