Question

In: Computer Science

5.25. The following pseudocode (next page) is a correct implementation of the producer/consumer problem with a...

5.25. The following pseudocode (next page) is a correct implementation of the producer/consumer problem with a bounded buffer:

Labels p1, p2, p3 and c1, c2, c3 refer to the lines of code shown above (p2 and c2 each cover three lines of code). Semaphores empty and full are linear semaphores that can take unbounded negative and positive values. There are multiple producer processes, referred to as Pa, Pb, Pc, etc., and multiple consumer processes, referred to as Ca, Cb, Cc, etc. Each semaphore maintains a FIFO (first-in-first-out) queue of blocked processes. In the scheduling chart below, each line represents the state of the buffer and semaphores after the scheduled execution has occurred. To simplify, we assume that scheduling is such that processes are never interrupted while executing a given portion of code p1, or p2, . . . , or c3. Your task is to complete the following chart.

item[3] buffer; // initially empty

semaphore empty; // initialized to +3

semaphore full; // initialized to 0

binary_semaphore mutex; // initialized to 1

void producer()

void consumer()

{

{

   ...

   ...

   while (true) {

   while (true) {

       item = produce();

c1:     wait(full);

p1:   wait(empty);

 /    wait(mutex);

 /         wait(mutex);

c2:     item = take();

p2:   append(item);

 \    signal(mutex);

 \      signal(mutex);

c3:     signal(empty);

p3:   signal(full);

         consume(item);

   }

   }

}

}

Scheduled Step of Execution

full’s State and Queue

Buffer

empty’s State and Queue

Initialization

full=0full=0

OOO

empty=+3empty= +3

Ca executes c1

full=−1full= −1 (Ca)

OOO

empty=+3empty= +3

Cb executes c1

full=−2full= −2 (Ca, Cb)

OOO

empty=+3empty= +3

Pa executes p1

full=−2full= −2 (Ca, Cb)

OOO

empty=+2empty= +2

Pa executes p2

full=−2full= −2 (Ca, Cb)

X   OO

empty=+2empty= +2

Pa executes p3

full=−1full= −1 (Cb) Ca

X   OO

empty=+2empty= +2

Ca executes c2

full=−1full= −1 (Cb)

OOO

empty=+2empty= +2

Ca executes c3

full=−1full= −1 (Cb)

OOO

empty=+3empty= +3

Pb executes p1

full= full=

empty= empty=

Pa executes p1

full= full=

empty= empty=

Pa executes __

full= full=

empty= empty=

Pb executes __

full= full=

empty= empty=

Pb executes __

full= full=

empty= empty=

Pc executes p1

full= full=

empty= empty=

Cb executes __

full= full=

empty= empty=

Pc executes __

full= full=

empty= empty=

Cb executes __

full= full=

empty= empty=

Pa executes __

full= full=

empty= empty=

Pb executes p1-p3

full= full=

empty= empty=

Pc executes __

full= full=

empty= empty=

Pa executes p1

full= full=

empty= empty=

Pd executes p1

full= full=

empty= empty=

Ca executes c1-c3

full= full=

empty= empty=

Pa executes __

full= full=

empty= empty=

Cc executes c1-c2

full= full=

empty= empty=

Pa executes __

full= full=

empty= empty=

Cc executes c3

full= full=

empty= empty=

Pd executes p2-p3

full= full=

empty=

Solutions

Expert Solution

Solution:


The completed chart for scheduling processes with corresponding values for full and =pc> semaphores are shown below.

i hope it helps..

If you have any doubts please comment and please don't dislike.

PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME


Related Solutions

Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes,...
Below is an attempt to solve the producer-consumer problem. Is this a correct solution? If yes, what is achieved and how? If not, what is the problem? Explain your answer in either case. (Note: Assume no syntax or compilation errors. You’re asked about the concurrency logic/correctness and the use of semaphores for this.) #define N 100                 /* number of slots in the buffer */ semaphore mutex = 1;          /* controls access to critical */ semaphore empty = N;          /* counts...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to...
The bounded buffer problem is a producer-consumer problem where a producer process adds new items to a shared buffer and a consumer process consumes items from that buffer (in the first-in-first-out (FIFO) order). When a producer adds a new item, the buffer size grows by one, and whenever a consumer consumes an item from the buffer, the size is reduced by one. The producer process must wait when the buffer is full likewise the consumer process must wait when the...
The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode...
The following problem should be done in pseudocode. Problem 1 The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way we've been generating random numbers in the book; however, there appears to be an error. Assume that the random() function is a built-in library function. Correct the pseudocode so that the program works as it should (This has 1 error and is easy to spot) //This program displays...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem...
Intro to java Problem, Please provide code and Pseudocode. Not able to compile correct numbers. Problem 7: Simple Calculator (10 points) (General) Calculators represent the most basic, general-purpose of computing machines. Your task is to reduce your highly capable computer down into a simple calculator. You will have to parse a given mathematical expression, and display its result. Your calculator must support addition (+), subtraction (-), multiplication (*), division (/), modulus(%), and exponentiation (**). Facts ● Mathematical expressions in the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the...
Research and Program Producer-Consumer Problem In this assignment you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in the lecture notes. The solution presented in the notes uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary semaphore that protects the actual insertion or removal of items in the buffer. For this assignment, standard counting semaphores will be...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using...
Implement the Producer-Consumer Problem programming assignment at the end of Chapter 5 in the textbook using the programming language Java instead of the described C code. You must use Java with Threads instead of Pthreads.A brief overview is below. This program should work as follows: The user will enter on the command line the sleep time, number of producer threads, and the number of consumer threads. One example of the Java application from the command line could be “java ProducerConsumer...
In Java, Modify “Producer and Consumer Problem” from lecture note so that it can use all...
In Java, Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffer_size – 1” as in the lecture note. This program should work as follows: 1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffer size and counter limit. 2. The main program will then create two separate threads, producer and consumer thread. 3. Producer thread generates a random...
The following problem applies to a perfectly competitive producer of widgets. A typical producer, say Widget...
The following problem applies to a perfectly competitive producer of widgets. A typical producer, say Widget Enterprises Inc., can sell widgets at a constant price of $30/pound. Widget Enterprises has the following costs in the short-run. Its total fixed costs are $45. QUANTITY (LBS) TOTAL COST $ 0 45 1 65 2 80 3 90 4 105 5 125 6 150 7 180 8 215 9 255 a. What does it mean to say that Widget Enterprises is a price...
For the scenario on the next page, write a paper that explains the following to a...
For the scenario on the next page, write a paper that explains the following to a prospective client: A) Formulate both null and alternative hypotheses for the client, and explain why the hypotheses need to be directional or non-directional. B) Determine what statistical test should be used to analyze the data. C) Summarize all information used to determine the correct statistical test (e.g., number of groups, type of data collected, independent or repeated measures) D) Provide a sample size and...
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip...
Write the Pseudocode for the following programming problem. Write a program that will calculate a tip based on the meal price and a 6% tax on a meal price. The user will enter the meal price and the program will calculate tip, tax, and the total. The total is the meal price plus the tip plus the tax. Your program will then display the values of tip, tax, and total. The tip amounts based on the mean price are as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT