Results 1 to 15 of 15
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By Norm

Thread: Semaphor

  1. #1
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Semaphor

    Hello Everyone,
    Last edited by TonyDaniels; 03-10-2012 at 12:03 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    the program never finishes.
    Is it looping or blocked waiting for some event?
    Where is it executing? What do your printouts show?

  3. #3
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Semaphor

    It executes for a bit, but then it just stalls/blocks and nothing seems to happen.
    Last edited by TonyDaniels; 03-10-2012 at 12:03 AM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    C8 consumed the item. Remaining demand = 5 items.
    Is that the last line printed before the code stops?

    numbers might be a little different since the values are random):
    For debugging use a constant number so you always get the same execution until you figure out what is happening.

  5. #5
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Semaphor

    I put a few more print statements,
    Last edited by TonyDaniels; 03-10-2012 at 12:03 AM.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    Continue debugging into the getName() method and the wait() method. What keeps wait() from returning?

    BTW Using the name of a well documented method for the name of your method is confusing.
    The wait() method is one of the Object class's methods that is important for threading.

  7. #7
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Semaphor

    I do apologies, I have a lot to get to grips with in terms of coding standards.
    Last edited by TonyDaniels; 03-10-2012 at 12:04 AM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    When does the value of status get changed? Where is the code that is supposed to change status's value? What is it doing?

  9. #9
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Semaphor

    the status value changes when .signal() is called:
    Last edited by TonyDaniels; 03-10-2012 at 12:05 AM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    If you are getting a "deadly embrace" you may need to redesign the program.

  11. #11
    TonyDaniels is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Semaphor

    Could you suggest how I might do that please?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    Sorry, that is the students job.

    One thing I's suggest is minimize the length of the runs to allow closer looking at the print outs.

    For debugging, add a line like this in main for simpler debugging:
    args= new String[]{"4", "2"};
    Last edited by Norm; 03-10-2012 at 12:09 AM.
    DarrylBurke likes this.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,561
    Rep Power
    25

    Default Re: Semaphor

    Here's my copy:
    Java Code:
    /* http://www.java-forums.org/new-java/56496-semaphor.html#post270806
    
    Code hangs in a tight loop: CPU=99%
    
    */
    
    public class BoundedBuffer	  {
        public static void main(String[] args){
            args= new String[]{"4", "2"};
            if(args.length != 2)
            {
                System.out.println("\nUSAGE: java BoundedBuffer <number of producers> <number of consumers>\n");
            }
            else
            {
                System.out.println();
                int n = Integer.valueOf(args[0]).intValue();
                Consumer.proc_count = n;
                for(int i=0;i<n;i++){
                    new Consumer();
                }
                System.out.println();
                n = Integer.valueOf(args[1]).intValue();
                Producer.proc_count = n;
                for(int i=0;i<n;i++){
                    new Producer();
                }
                System.out.println();
            }
        }
    //}
     
    static class Semaphore
    {
        public String name;
        private int status;
        public static int num = 0;
         
        public Semaphore(String nm)
        {
            name=nm;
            status=0;
            System.out.println("Semaphore "+ name + " Created");
        }
     
        public Semaphore(String nm, int initl)
        {
            name=nm;
            status=initl;
            System.out.println("Semaphore " + name + " Created and initialised to " + initl);
        }
     
        public void wait(String user)       {
            int success = 0;
            while(success==0)           {
                System.out.println(">>> " + user + " wait before synch status=" + status);
                synchronized (this)              { 
                    if (status > 0)                 {
                        delay(1000);
                        status--;
                        success++;
                    }
                } // end synch
                System.out.println(">>> " + user + " wait after synch status=" + status);
            } // end while
        }
         
        public synchronized void signal(String user)     {
            status++;
        }
     
        public int getStatus()      {
            return(status);
        }
     
        public void delay(int n)      {
            // Just some dummy calculations:
            double x=1000;  
            for(int i=0;i<n;i++)         {
                x=Math.sqrt(x);    
            }
        }
    }
     
    //--------------------------------------
    static class Process extends Thread
    {
        public int count = 0;
        public static int num = 0;
        public static int proc_count = 0;
        public static int buffer = 0;
        public static final int buffer_size = 10;
     
        static Semaphore mutex = new Semaphore("Mutex",1);
        static Semaphore full = new Semaphore("Full",0);
        static Semaphore empty = new Semaphore("Empty", buffer_size);
     
        public static Monitor monitor = new Monitor();
     
        public Process(String label)
        {
            super(label);
            count = 5; //(int)(10*Math.random()+1);
        }
     
        public void run()
        {
            while(count>0)
            {
                count--;
            }
            proc_count--;
            System.out.println(getName() + " finished");
        }
    }
     
    static class Monitor extends Thread
    {
        Monitor()
        {
            System.out.println("\nMonitor for the buffer created\n");
            start();
        }
     
        public void run()
        {
            int Collision_count = 0;
            while(Producer.proc_count >0 || Consumer.proc_count > 0)
            {
                if(Process.buffer > Process.buffer_size || Process.buffer < 0) 
                {
                    System.out.println("COLLISION !!!  Buffer bounds violated." );
                    Collision_count++;
                }
            }
             
            if (Collision_count>0) 
                System.out.println("Total: " + Collision_count + " collisions detected");
            else
                System.out.println("All finished. No collision detected");
        }
    }
     
     
    static class Producer extends Process
    {
        public static int num = 0;
        public static int proc_count = 0;
     
        public Producer()
        {
            super("P" + ++num);
            System.out.println("Producer " + getName() + " created with random supply = " + count);
            start();
        }
     
        public void run()
        {
            while(count>0)
            {
                count--;
                System.out.println(getName() + " produced an item. Remaining supply = " + count );
                empty.wait(getName());
                System.out.println("producer has empty");
                mutex.wait(getName());
                System.out.println("producer has muteex");
                buffer++;
                System.out.println(getName() + " added the item to the buffer. Buffer contains " + buffer + " items");
                mutex.signal(getName());
                System.out.println("prducer signalled mutex");
                full.signal(getName());
                System.out.println("procuer signalled full");
            }
            proc_count--;
            System.out.println(getName() + " finished");
        }
    }
     
    static class Consumer extends Process
    {
        public static int num = 0;
        public static int proc_count = 0;
     
        public Consumer()
        {
            super("C"+ ++ num);
            System.out.println("Consumer " + getName() + " created with random demand = " + count + " items.");
            start();
        }
     
        public void run()
        {
            while(count>0)
            {
                full.wait(getName());
                System.out.println("consumer has full");
                mutex.wait(getName());
                System.out.println("consumer has muteex");
                buffer--;
                System.out.println(getName() + " removed the item from the buffer. Buffer contains " + buffer + " items");
                mutex.signal(getName());
                System.out.println("consumer signallyed mutex");
                empty.signal(getName());
                System.out.println("consumer signalled empty");
                count--;
                System.out.println(getName() + " consumed the item. Remaining demand = " + count + " items.");
            }
            proc_count--;
            System.out.println( getName() + " finished");
        }
    }
    } // end class
    DarrylBurke likes this.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

    Default Re: Semaphor

    Tony, this is a forum. Apart from being plain rude, it's against all principles to remove content after replies have been posted.

    If you do this again, you may be banned.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Bounded Buffer
    By babs2010 in forum New To Java
    Replies: 0
    Last Post: 04-08-2010, 05:24 PM
  2. Buffer Example help.
    By bert682 in forum Threads and Synchronization
    Replies: 0
    Last Post: 11-27-2009, 09:26 AM
  3. Buffer
    By QPRocky in forum Networking
    Replies: 2
    Last Post: 02-02-2009, 03:54 PM
  4. Bounded Array
    By bugger in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 09:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •