PRODUCER CONSUMER PROBLEM

PRODUCER CONSUMER PROBLEM


The producer-consumer problem(also know as bounded buffer problem) in real life is a classic problem faced while solving the multi-process synchronization problem.  As considered in real-life the producer-consumer use a fixed buffer (storage space) for producing or consuming goods(i.e the resources). The problem may arise if the producer and the consumer are not in synchronization. If the producer produced more than required or the consumer is trying to consume more than the produced material.


To solve this problem using semaphores the program is given below:
import java.util.concurrent.Semaphore;


 class globe{      //for counting the number of items the varialble is made global.
public class ConsumerProducer{
    public static void main(String[] args) 
   do
  ch=sc.nextInt();
       break;  
    
           
                       else
Output:
semaphoreProducer permit=1 | semaphoreConsumer permit=0
1



import java.lang.*;
import java.util.*;

  public static int x=0;
}


  {
       Scanner sc=new Scanner(System.in);
           int ch;
      
           Semaphore semaphoreProducer=new Semaphore(1);
           Semaphore semaphoreConsumer=new Semaphore(0);
           System.out.println("semaphoreProducer permit=1 | semaphoreConsumer permit=0");
           
       Producer producer=new Producer(semaphoreProducer,semaphoreConsumer);
       Consumer consumer=new Consumer(semaphoreConsumer,semaphoreProducer);
      
        Thread producerThread = new Thread(producer, "ProducerThread");
        Thread consumerThread = new Thread(consumer, "ConsumerThread");

      producerThread.start();
       consumerThread.start();

{
  System.out.println("Enter choice:");

 switch(ch)
{
case 1:producer.p();
        
       break;
case 2:  consumer.c();


   }
 }while(ch!=0);
}
}




/**
 *  * Producer Class.
 *   */
class Producer implements Runnable
{    
    Semaphore semaphoreProducer;
    Semaphore semaphoreConsumer;

    public Producer(Semaphore semaphoreProducer,Semaphore semaphoreConsumer) {
           this.semaphoreProducer=semaphoreProducer;
           this.semaphoreConsumer=semaphoreConsumer;
         
 }

    public void run() {System.out.println(" ");}
         
            public void p(){ 
                 try {
                      semaphoreProducer.release();//gives permit to producer.
                      semaphoreProducer.acquire();//producer acquires lock.
                      globe.x++;
                      System.out.println("Produced : "+globe.x);
                     
                        
                  } catch (InterruptedException e) {
                   
                           e.printStackTrace();
                  }
                     
                 
   
    }
}

/**
 *  * Consumer Class.
 *   */
class Consumer implements Runnable
 {
    Semaphore semaphoreConsumer;
    Semaphore semaphoreProducer;
   
    public Consumer(Semaphore semaphoreConsumer,Semaphore semaphoreProducer) {
           this.semaphoreConsumer=semaphoreConsumer;
           this.semaphoreProducer=semaphoreProducer;
              
}

    public void run() {System.out.println(" ");}

          
                   public void c(){
                     try {
                     semaphoreConsumer.release();//gives permit to consumer.
                      semaphoreConsumer.acquire();//consumer acquires lock.
                      if(globe.x>0)
                        {System.out.println("Consumed : "+globe.x);
                        globe.x--;
                        }

                        {System.out.println("No items present");}
                 
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
                   
          
    }
    
}




Enter choice:

Produced : 1
Enter choice:
1
Produced : 2
Enter choice:
2
Consumed : 2
Enter choice:
2
Consumed : 1
Enter choice:
2
No items present
Enter choice:
0

Comments

Popular posts from this blog

How to check if you're buying a stolen phone.

Refrigeration By ice cutting.

Life after Chernobyl