Question

In: Computer Science

A) Describe and explain in detail the producer-consumer principle for concurrent programming as we studied in...

A) Describe and explain in detail the producer-consumer principle for concurrent programming as we studied in this course

B) Choose one of the mechanisms we use studied to synchronize the producer and consumer threads and write and example demonstrator (5pts)

C) Explain in your comments in the Java code the most important aspects that enabled synchronization between the the 2 threads in (ii) above. (5pts)

Solutions

Expert Solution

A)

In computing, the producer-consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue.

  • The producer’s job is to generate data, put it into the buffer, and start again.
  • At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.

Problem
To make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.

Solution
The producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.
An inadequate solution could result in a deadlock where both processes are waiting to be awakened.

B & C)

Implementation of Producer Consumer Class:

  • A LinkedList list – to store list of jobs in queue.
  • A Variable Capacity – to check for if the list is full or not
  • A mechanism to control the insertion and extraction from this list so that we do not insert into list if it is full or remove from it if it is empty.

// Java program to implement solution of producer
// consumer problem.

import java.util.LinkedList;

public class Threadexample {
   public static void main(String[] args)
       throws InterruptedException
   {
       // Object of a class that has both produce()
       // and consume() methods
       final PC pc = new PC();

       // Create producer thread
       Thread t1 = new Thread(new Runnable() {
           @Override
           public void run()
           {
               try {
                   pc.produce();
               }
               catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });

       // Create consumer thread
       Thread t2 = new Thread(new Runnable() {
           @Override
           public void run()
           {
               try {
                   pc.consume();
               }
               catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       });

       // Start both threads
       t1.start();
       t2.start();

       // t1 finishes before t2
       t1.join();
       t2.join();
   }

   // This class has a list, producer (adds items to list
   // and consumber (removes items).
   public static class PC {

       // Create a list shared by producer and consumer
       // Size of list is 2.
       LinkedList<Integer> list = new LinkedList<>();
       int capacity = 2;

       // Function called by producer thread
       public void produce() throws InterruptedException
       {
           int value = 0;
           while (true) {
               synchronized (this)
               {
                   // producer thread waits while list
                   // is full
                   while (list.size() == capacity)
                       wait();

                   System.out.println("Producer produced-"
                                   + value);

                   // to insert the jobs in the list
                   list.add(value++);

                   // notifies the consumer thread that
                   // now it can start consuming
                   notify();

                   // makes the working of program easier
                   // to understand
                   Thread.sleep(1000);
               }
           }
       }

       // Function called by consumer thread
       public void consume() throws InterruptedException
       {
           while (true) {
               synchronized (this)
               {
                   // consumer thread waits while list
                   // is empty
                   while (list.size() == 0)
                       wait();

                   // to retrive the ifrst job in the list
                   int val = list.removeFirst();

                   System.out.println("Consumer consumed-"
                                   + val);

                   // Wake up producer thread
                   notify();

                   // and sleep
                   Thread.sleep(1000);
               }
           }
       }
   }
}


Related Solutions

2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus...
2. Consumer Surplus and Producer Surplus Explain in words and graphically how consumer surplus, producer surplus and total surplus change when the minimum wage is removed. Assume the minimum wage is above the free market price. In your explanation please interpret the components of the changes in consumer surplus, producer surplus and total surplus; i.e. what each component represents. For additional points, what happens if the minimum wage is set below the free market price? please graph
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...
Assume we have a society with 1 producer and 1 consumer and we are looking at...
Assume we have a society with 1 producer and 1 consumer and we are looking at the market for coconuts. Let us simplify further and assume that the producer can harvest only 1 coconut with a marginal cost of $5. The marginal benefit to the consumer for the coconut is $10. From question b and onwards, assume the two economic agents agree to a price of $9 for the coconut. What is the maximum possible price for these two agents...
Describe in detail the core principle of "quantum key distribution".
Describe in detail the core principle of "quantum key distribution".
3. Markets and wellbeing: Why we want markets that make consumer and producer surplus bigger? Explain...
3. Markets and wellbeing: Why we want markets that make consumer and producer surplus bigger? Explain the concept of surplus and the relationship between price and consumer surplus. Please, build your argument trough an example (it can be fictitious). Answer must contain a brief introduction to concepts and facts, a part of analysis and critical evaluation and final conclusions.
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
Explain the controllability principle and exception by reporting. Explain in detail with examples
Explain the controllability principle and exception by reporting. Explain in detail with examples
explain the implications of the consumer surplus and producer surplus on economic welfare
explain the implications of the consumer surplus and producer surplus on economic welfare
using the concepts of producer and consumer surplus explain the welfare implications of major brought on...
using the concepts of producer and consumer surplus explain the welfare implications of major brought on producers and consumer
Describe the effects (changes in quantity, price, consumer surplus, and producer surplus) of a product ban...
Describe the effects (changes in quantity, price, consumer surplus, and producer surplus) of a product ban on asbestos insulation in each of the following markets: a. Market for Asbestos Insulation b. Market for Asbestos Fiber c. (6 points) Market for Asbestos Clothing d. Assuming the above are the only markets impacted by the ban on asbestos insulation, what is the Total Cost to society? e. Explain how you would determine if there is a Net Benefit or a Net Cost...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT