Search English (United States)  தமிழ் (இந்தியா)
Tuesday, January 06, 2009 ..:: Articles ::.. Register  Login
Location: BlogsArticlesProgramming    
Posted by: ganesh Sunday, September 21, 2008 8:48 AM

The Producer-Consumer problem is the traditional one to explain the concept of multitasking with shared memory access in operating systems. This program demonstrates how a shared memory is being accessed by two different threads which run concurrently through an Applet. The producer thread continuously produces an item (here it generates a random number) and inserts into the stockpile (here it is an array limited in size), until space exists. The other thread named consumer, consumes an item (here it simply removes the item from the array). If there is no room to store new product, the producer stops producing and goes asleep. It will be awakened by the consumer thread when it removes an item and resulted in an empty space in the shared array. This scenario happens when the consumer is slow in execution than the producer. Similarly, when all the items are removed from the stock and producer is still in the task of producing an item, consumer goes asleep. It will be awakened by the producer thread once the produced item is available in the stock.

Screenshot

Since, both of these threads are accessing the same stockpile (array), there is a chance of sharing violation. So, this program utilizes the mechanism called Monitor. Monitors and semaphores are used to resolve the conflicts in shared memory/resource access in parallel and multiprogramming systems. Here, both of the threads use monitor to access the shared array (they don’t access the shared array directly).

The applet is having two slider controls which are used to control the speed of producer and consumer and the stockpile displays the items currently available in the array.

// Producer - Consumer Problem
import java.applet.*;
import java.awt.*;
public class ProdCons extends Applet
{
    static final int N=12;
    static Label lblProd,lblCons,lblPSpeed,lblCSpeed,lblDStock;
    static TextField txtStock;
    static Scrollbar scrProd, scrCons;
    static int lb, ub;
    static producer p=new producer();
    static consumer c= new consumer();
    static monitor mon = new monitor();
    public void init()
    {
        lblProd = new Label("Producer is Sleeping    ");
        lblCons = new Label("Consumer is Sleeping    ");
        lblDStock = new Label("Stock Available");
        txtStock = new TextField(50);
        scrProd = new Scrollbar(Scrollbar.HORIZONTAL,50,2,0,100);
        scrCons = new Scrollbar(Scrollbar.HORIZONTAL,50,2,0,100);
        lblPSpeed = new Label("Producing Speed:");
        lblCSpeed = new Label("Consuming Speed:");
        add(lblProd);
        add(lblPSpeed);
        add(scrProd);
        add(lblCons);
        add(lblCSpeed);
        add(scrCons);
        add(lblDStock);
        add(txtStock);
        lb=0;
        ub=0;
        p.start();
        c.start();
    }
    static class producer extends Thread
    {
        public void run()
        {
            int item;
            while(true)
            {
                item=produce();
                mon.insert(item);
            }
        }
        private int produce()
        {
            int x= (int)(Math.random()*100)+1;
            System.out.println("Produced: " + x);
            lblProd.setText("Producer is Producing");
            try
            {
                int t=(int)(1600-scrProd.getValue()*15);
                Thread.sleep(t);
            }
            catch(InterruptedException e){}
            return(x);
        }
    }
    static class consumer extends Thread
    {
        public void run()
        {
            int item;
            while(true)
            {
                item=mon.remove();
                consume(item);
            }
        }
        private void consume(int item)
        {
            System.out.println("Consumed: " + item);
            lblCons.setText("Consumer is Consuming  ");
            try
            {
                int t=(int)(1600-scrCons.getValue()*15);
                Thread.sleep(t);
            }
            catch(InterruptedException e){}
        }
    }
    static class monitor
    {
        public int buffer[]=new int[N];
        public int count=0;
        public synchronized void insert(int v)
        {
            if(count==N) GoSleep("Producer");
            buffer[ub]=v;
            ub=(ub+1)%N;
            count++;
            notify();
            String ss="";
            for(int i=0;i<=N-1;i=i+1)
                if(buffer[i]!=0) ss=ss + " " + buffer[i];
            txtStock.setText("" + ss);
        }
        public synchronized int remove()
        {
            int v;
            if(count<=0) GoSleep("Consumer");
            v=buffer[lb];
            buffer[lb]=0;
            lb=(lb+1)%N;
            String ss="";
            for(int i=0;i<=N-1;i=i+1)
                if(buffer[i]!=0) ss=ss + " " + buffer[i];
            txtStock.setText(""+ ss);
            count--;
            notify();
            return v;
        }
        private void GoSleep(String s)
        {
            try
            {
                if(s =="Producer")
                    lblProd.setText("Producer is Sleeping");
                else
                    lblCons.setText("Consumer is Sleeping");
                wait();
            }
            catch(InterruptedException ex)
            {
            }
        }
    }
}

 

Compile this program using java compiler and create a HTML file with following code to run the program or use appletviewer.


<html>
<head><title>Producer – Consumer Problem Demonstration</title></head>
<body> <applet code="ProdCons.class" width=400 height=125><body>
</html>

Copyright ©2008 technicalganesh.com
Permalink |  Trackback

Your name:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel 
Copyright 2007 by technicalganesh.com   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2009 by DotNetNuke Corporation