Question

9. Here are the codes for producer and consumer problems Producer: while (true) {                 /*...

9. Here are the codes for producer and consumer problems

Producer:

while (true) {

                /* produce an item in next produced */

                while (counter == BUFFER_SIZE) ;

                                /* do nothing */

                buffer[in] = next_produced;

                in = (in + 1) % BUFFER_SIZE;

                counter++;

}

Consumer:

              while (true) {

                while (counter == 0)

                                ; /* do nothing */

                next_consumed = buffer[out];

                out = (out + 1) % BUFFER_SIZE; counter--;

                /* consume the item in next consumed */

           }

      counter++ could be implemented by Producer as

         register1 = counter

         register1 = register1 + 1

          counter = register1

     counter-- could be implemented by Consumer as

        register2 = counter

        register2 = register2 - 1

        counter = register2

If we set the current value of “counter “ as 4, when Producer and Consumer concurrently execute, what are the possible values of ‘’counter”, please give an example for each value, point out which value is the right one and analysis the reasons for the wrong values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If there is a switch happens during counter++ or counter--, we will get a wrong value for the counter.

counter++   counter--

         register1 = counter register2 = counter

         register1 = register1 + 1   register2 = register2 - 1

         counter = register1 counter = register2

For example, assume that counter=4,means that there are 4 items which are produced already. Now look at the following steps and value of counter after each step.

1.Producer process is updating counter++(happens at machine level) and during that when it completed register1=counter counter=4

2. Suppose there is a context switch happens to consumer process and it is trying to update counter--(happens at machine level) and during that when it completed register2=counter counter=4

register2=register2-1

counter=register2 counter=3

3. Assume now context switch happened back to producer process and it continues from where it stopped(see step 1)

register1=register1+1

counter=register1 counter=5

See the value of counter variable. It became 5. Consumption of one item is not reflected in the counter variable. To avoid this we need to allow the atomic execution of all these instructions. Means whichever process starts updating the counter,the other one should wait for the completion of it.

4. if producer executes all these machine level instructions at once,then there is no inconsistancy on this.

Add a comment
Know the answer?
Add Answer to:
9. Here are the codes for producer and consumer problems Producer: while (true) {                 /*...
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
  • Producer and Consumer Code in C++ The program will contain two methods- one each for the...

    Producer and Consumer Code in C++ The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption). Enhancements and modifications: You...

  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • 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...

  • 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...

  • 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...

  • 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...

  • Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use...

    Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffersize – 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 buffersize and counter limit. 2. The program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...

  • You are given a sample code that when edited/ improved/completed -will work as a producer consumer...

    You are given a sample code that when edited/ improved/completed -will work as a producer consumer synchronized solution. In addition to the counter, you will implement a circular linked list as a buffer of size 200 that stores data items 0 or 1. The producer and consumer take *random* turns to produce and consume- *random* numbers of elements ( turning 1 to a 0 and visa-versa-each time, as discussed in class). After each turn of the producer and/or consumer, your...

  • a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems....

    a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class {    struct class *next;    int id;    int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...

  • In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer...

    In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...

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