예제 #1
0
        public void run()
        {
            CSTimer timer         = new CSTimer();
            Random  rnd           = new Random();
            int     randomTimeout = rnd.Next(1, 10) * n * 500;

            timeout = timer.read() + randomTimeout;
            Debug.WriteLine(n + "Process timeout " + randomTimeout);

            Object x = input.read();

            Console.WriteLine(n + "Read value" + x);
            while (true)
            {
                x = input.read();
                Console.WriteLine(n + "Read value" + x);

                //output.write(x);
                long currentTime = timer.read();
                if (timeout <= currentTime) //if timeout should occur now
                {
                    timeout = timer.read() + rnd.Next(1, 10) * 500;
                    Debug.WriteLine(n + "Process timout ppassed " + randomTimeout);
                    output.write(n + "For Input guard" + x + "0000000");
                    timer.after(timeout);
                    output.write(n + "Finished");
                    timeout = timer.read() + rnd.Next(1, 10) * n * 500;
                }
            }
        }
        public void run()
        {
            StressedPacket stressedPacketA = new StressedPacket(writer);
            StressedPacket stressedPacketB = new StressedPacket(writer);

            stressedPacketA.n = writerID;
            Out.write(stressedPacketA);
            stressedPacketB.n = writerID;
            Out.write(stressedPacketB);
        }
        public void run()
        {
            int n = 1 * num;

            CSTimer     timer           = new CSTimer();
            Random      rnd             = new Random();
            int         randomTimeout   = rnd.Next(1, 10) * 500;
            Alternative barrierGroupAlt = new Alternative(new Guard[] { input, barrier });

            const int INPUT = 0, GROUP = 1, TIMER = 2;

            output.write(n.ToString());


            while (true)
            {
                bool pending = input.pending();
                while (!pending)
                {
                    Debug.WriteLine((char)num + " pending = " + pending);

                    n++;
                    output.write((char)num + " " + n.ToString());  // work with the barrier
                    pending = input.pending();
                }

                input.read();                               // must consume the input
                output.write("Read click " + n.ToString()); // work with the barrier

                Boolean group = true;
                while (group)
                {
                    switch (barrierGroupAlt.priSelect())
                    {
                    case INPUT:
                        Debug.WriteLine((char)num + "barier input");
                        string x = input.read().ToString(); // must consume the input
                        Console.WriteLine(x);
                        group = false;                      // end barrier working
                        break;

                    case GROUP:
                        n--;                        // work with the barrier
                        output.write(n.ToString()); // work with the barrier
                        Debug.WriteLine((char)num + "barier wrote " + n);
                        break;
                    }
                }
            }
        }
예제 #4
0
        public void run()
        {
            Guard[] guards = new Guard[In.Length + 1];
            Array.Copy(In, 0, guards, 0, In.Length);

            CSTimer tim        = new CSTimer();
            int     timerIndex = In.Length;

            guards[timerIndex] = tim;

            Alternative alt = new Alternative(guards);

            Boolean running = true;

            tim.setAlarm(tim.read() + timeout);
            while (running)
            {
                int index = alt.fairSelect();
                if (index == timerIndex)
                {
                    running = false;
                }
                else
                {
                    Out.write(In[index].read());
                }
            }

            Console.WriteLine("Goodbye from FairPlexTime ...");
            //System.exit(0);
        }
예제 #5
0
        public void run()
        {
            CSTimer tim      = new CSTimer();
            int     CHICKENS = 4;

            toConsole.write("Starting ... \n");
            while (true)
            {
                toConsole.write("Cooking ... \n"); // cook 4 chickens;
                tim.after(tim.read() + 2000);      // this takes 2 seconds to cook;
                toConsole.write(CHICKENS + "chickens ready ... \n");
                supply.write(CHICKENS);
                toConsole.write("Taking chickens to Canteen ... \n");
                supply.write(0);
            }
        }
        public void run()
        {
            CSTimer tim = new CSTimer();

            toConsole.write("Starting ... \n");
            while (true)
            {
                toConsole.write("Thinking ... \n");
                if (id > 0)
                {
                    tim.sleep(3000);
                }
                else
                {
                    // Philosopher 0, has a 0.1 second think
                    tim.sleep(100);
                }

                toConsole.write("Need a chicken ...\n");
                service.write(id);
                int gotOne = (int)deliver.read();
                if (gotOne > 0)
                {
                    toConsole.write("Eating ... \n");
                    tim.sleep(2000);
                    toConsole.write("Brrrp ... \n");
                }
                else
                {
                    toConsole.write("                   Oh dear No chickens left \n");
                }
            }
        }
        /**
         * Sends some data back to the client after a request
         * has been received. The closed parameter indicates whether or not
         * the connection should be closed. The connection will be closed
         * iff close is <code>true</code>.
         *
         * @param	data	the data to send to the client.
         * @param  close   <code>boolean</code> indicating whether or not the
         *                  connection should be closed.
         */
        public void reply(Object data, Boolean close)// throws
        {
            try
            {
                if (currentServerState != SERVER_STATE_RECEIVED)
                {
                    throw new InvalidOperationException("Cannot call reply(Object, boolean) on a ConnectionServer that has not received an unacknowledge request.");
                }

                //set open to true before replying
                msg.data = data;
                msg.open = !close;
                toClient.write(msg);
                if (close)
                {
                    currentServerState = SERVER_STATE_CLOSED;
                    toClient           = null;
                    setAltingChannel(openIn);
                    currentInputChannel = openIn;
                }
                else
                {
                    currentServerState = SERVER_STATE_OPEN;
                }
            }
            catch (InvalidOperationException)
            {
                throw;
            }
        }
예제 #8
0
 /**
  * Sends some data over the connection to server once the
  * connection has been opened.
  *
  * @param data	the <code>Object</code> to send to the server.
  */
 public void request(Object data)// throws IllegalStateException
 {
     try
     {
         if (currentClientState == CLIENT_STATE_MADE_REQ)
         {
             //throw new IllegalStateException ("Cannot call request(Object) twice without calling reply().");
             //this will Claim the use of the client
             if (currentClientState == CLIENT_STATE_CLOSED)
             {
                 claim();
                 messageOpen.data         = data;
                 messageOpen.replyChannel = backToClient;
                 openToServer.write(messageOpen);
             }
             else
             {
                 message.data = data;
                 reqToServer.write(message);
             }
         }
         currentClientState = CLIENT_STATE_MADE_REQ;
     }
     catch (InvalidOperationException)
     {
         throw;
     }
 }
        public void run()
        {
            int input = Int32.Parse(inChannel.read().ToString());

            Console.WriteLine(input + " - It is an Int");
            outChannel.write(input);
        }
예제 #10
0
 /**
  * The main body of this process.
  */
 public void run()
 {
     while (true)
     {
         Out.write(In.read());
     }
 }
예제 #11
0
        public void run()
        {
            string input = inChannel.read().ToString();

            Console.WriteLine(input);
            outChannel.write(input);
        }
예제 #12
0
        public void run()
        {
            CSTimer tim   = new CSTimer();
            CSTimer reset = new CSTimer();

            Guard[]   guards = { reset, tim, In as Guard }; // prioritised order
            const int RESET  = 0;                           // index into guards
            const int TIM    = 1;                           // index into guards
            const int IN     = 2;                           // index into guards

            Alternative alt = new Alternative(guards);

            int  x       = 0;
            long timeout = tim.read() + 3000;

            tim.setAlarm(timeout);

            long resetting = reset.read() + 5000;

            reset.setAlarm(resetting);

            while (true)
            {
                switch (alt.priSelect())
                {
                case RESET:
                    resetting = reset.read() + 5000;
                    reset.setAlarm(resetting);
                    Out.write("Numbers reset");
                    break;

                // fall through
                case TIM:
                    timeout = tim.read();
                    Out.write(x * 10);
                    timeout += 3000;
                    tim.setAlarm(timeout);
                    break;

                case IN:
                    x = (int)In.read();
                    Out.write(x);
                    break;
                }
            }
        }
예제 #13
0
 /**
  * The main body of this process.
  */
 public void run()
 {
     while (true)
     {
         int i = Int32.Parse(In.read().ToString());
         Out.write(i + 1);
     }
 }
 public Object request()
 {
     if (connectionServerToUse.getServerState() == AltingConnectionServerImpl.SERVER_STATE_CLOSED)
     {
         synchOut.write(null);
     }
     return(connectionServerToUse.request());
 }
예제 #15
0
 public void write(Object o)
 {
     if (!syncDone)
     {
         ab.sync();
     }
     syncDone = false;
     Out.write(o);
 }
예제 #16
0
        public void run()
        {
            CSTimer timer = new CSTimer();

            int n_chickens;

            Console.WriteLine("            Chef    : starting ... ");
            while (true)
            {
                // cook 4 chickens
                Console.WriteLine("            Chef    : cooking ... ");
                timer.after(timer.read() + 2000); // this takes 3 seconds to cook
                n_chickens = 4;
                Console.WriteLine("            Chef    : " + n_chickens + " chickens, ready-to-go ... ");
                supply.write(n_chickens); // supply the chickens
                supply.write(0);          // wait till they're set down
            }
        }
예제 #17
0
        public void run()
        {
            /*final*/
            Alternative clickGroup =
                new Alternative(new Guard[] { click, group });

            const int CLICK = 0, GROUP = 1;

            int n = 0;

            configure.write(n.ToString());

            while (true)
            {
                configure.write(Color.Green); // pretty

                while (!click.pending())
                {
                    n++;                           // work on our own
                    configure.write(n.ToString()); // work on our own
                }

                click.read();               // must consume the click

                configure.write(Color.Red); // pretty

                Boolean group = true;
                while (group)
                {
                    switch (clickGroup.priSelect())
                    {
                    case CLICK:
                        click.read();     // must consume the click
                        group = false;    // end group working
                        break;

                    case GROUP:
                        n--;                           // work with the group
                        configure.write(n.ToString()); // work with the group
                        break;
                    }
                }
            }
        }
        /**
         * The main body of this process.
         */
        public void run()
        {
            CSTimer tim = new CSTimer();

            Guard[]   guards = { reset, tim, In };           // prioritised order
            const int RESET  = 0;                            // index into guards
            const int TIM    = 1;                            // index into guards
            const int IN     = 2;                            // index into guards

            Alternative alt = new Alternative(guards);

            Object x = 1;                                      // holding object

            long interval = initialInterval;

            long timeout = tim.read() + interval;

            tim.setAlarm(timeout);

            while (true)
            {
                switch (alt.priSelect())
                {
                case RESET:
                    //interval = ((long)reset.read()).longValue();
                    interval = (long)reset.read();
                    timeout  = tim.read();                             // fall through
                    Out.write(x);
                    timeout += interval;
                    tim.setAlarm(timeout);
                    break;

                case TIM:
                    Out.write(x);
                    timeout += interval;
                    tim.setAlarm(timeout);
                    break;

                case IN:
                    x = In.read();
                    break;
                }
            }
        }
예제 #19
0
        public void run()
        {
            Skip        flush   = new Skip();
            Alternative vAlt    = new Alternative(new Guard[] { needToConsult as Guard, consultationOver as Guard, flush });
            int         index   = -1;
            int         filling = 0;

            int[]     counter = { 0, 0, 0, 0 };
            const int NEED    = 0;
            const int OVER    = 1;
            const int FLUSH   = 2;

            Boolean[] preCon = new Boolean[3];
            preCon[NEED]  = true;
            preCon[OVER]  = true;
            preCon[FLUSH] = false;
            openForBusiness.read();
            int removing = 0;


            while (true)
            {
                index = vAlt.select(preCon);
                switch (index)
                {
                case NEED:
                    needToConsult.read();
                    joinGroup.write(filling);
                    counter[filling] = counter[filling] + 1;
                    if (counter[filling] == 3)
                    {
                        filling = (filling + 1) % 4;
                    }
                    break;

                case OVER:
                    consultationOver.read();
                    removing = (removing + 1) % 4;
                    break;

                case FLUSH:
                    groups[removing].flush();
                    counter[removing] = 0;
                    break;
                }

                preCon[FLUSH] = (groups[removing].holding() == 3);
                // have to ensure that the the last elf to fallInto a bucket
                // has actually done so
                // we do not rely on the fact that the coounter for the bucket has reached 3
                // we test to make sure there are three processes in the bucket
                // which is next to be removed
                // changed as a result of the discussion at CPA2008 Fringe session from
                // preCon[FLUSH] = (counter[removing] == 3)
            }
        }
        public void run()
        {
            Alternative alt = new Alternative(In);

            while (true)
            {
                int index = alt.fairSelect();
                Out.write(In[index].read());
            }
        }
 public void run()
 {
     toConsole.write("Please input reset values\n");
     while (true)
     {
         var v = fromConverter.read();
         toClearOutput.write("\n");
         resetValue.write(v);
     }
 }
        public void run()
        {
            Guard[]   canteenGuards = { supply as Guard, service as Guard };
            Boolean[] precondition  = { true, false };
            var       canteenAlt    = new Alternative(canteenGuards);

            const int SUPPLY  = 0;
            const int SERVICE = 1;

            CSTimer tim      = new CSTimer();
            int     chickens = 0;

            toConsole.write("Canteen : starting ... \n");

            while (true)
            {
                precondition[SERVICE] = (chickens > 0);
                if (chickens == 0)
                {
                    toConsole.write("Waiting for chickens ...\n");
                }

                switch (canteenAlt.fairSelect(precondition))
                {
                case SUPPLY:
                    int value = (int)(long)supply.read();
                    toConsole.write("Chickens on the way ...\n");
                    tim.after(tim.read() + 3000);
                    chickens = chickens + value;
                    toConsole.write(chickens + " chickens now available ...\n");
                    supply.read();
                    break;

                case SERVICE:
                    int id = (int)(long)service.read();
                    chickens = chickens - 1;
                    toConsole.write("chicken ready for Philosoper " + id + " ... " + chickens +
                                    " chickens left \n");
                    deliver.write(1);
                    break;
                }
            }
        }
예제 #23
0
        public void run()
        {
            int            n = 0;
            StressedPacket stressedPacketA = new StressedPacket(writer);
            StressedPacket stressedPacketB = new StressedPacket(writer);

            while (true)
            {
                // for (int i = 0; i < writer; i++) System.out.print ("  ");
                // System.out.println (id + " " + n);
                stressedPacketA.n = n;
                Out.write(stressedPacketA);
                n++;
                // for (int i = 0; i < writer; i++) System.out.print ("  ");
                // System.out.println (id + " " + n);
                stressedPacketB.n = n;
                Out.write(stressedPacketB);
                n++;
            }
        }
예제 #24
0
        public void run()
        {
            var alt = new Alternative(new Guard[] { resetChannel as Guard, inChannel as Guard });

            outChannel.write(prefixValue);
            while (true)
            {
                int index = alt.priSelect();
                if (index == 0)
                {
                    var resetValue = resetChannel.read();
                    inChannel.read();
                    outChannel.write(resetValue);
                }
                else
                {
                    outChannel.write(inChannel.read());
                }
            }
        }
예제 #25
0
        /**
         * The main body of this process.
         */
        public void run()
        {
            CSTimer tim = new CSTimer();

            while (true)
            {
                Object o = In.read();
                //Debug.WriteLine("Fixed delay sleeping for " + delayTime);
                tim.sleep(delayTime);
                Out.write(o);
            }
        }
예제 #26
0
        public void run()
        {
            CSTimer tim  = new CSTimer();
            int     tick = 0;

            while (true)
            {
                toConsole.write("Time: " + tick + " \n");
                tim.sleep(1000);
                tick = tick + 1;
            }
        }
예제 #27
0
        public void run()
        {
            CSTimer timer   = new CSTimer();
            long    timeout = timer.read(); // read the (absolute) time once only

            while (true)
            {
                N++;
                Out.write(N);
                timeout += interval;  // set the next (absolute) timeOut
                timer.after(timeout); // wait until that (absolute) timeOut
            }
        }
예제 #28
0
        /**
         * The main body of this process.
         */
        public void run()
        {
            ProcessRead[] parRead = { new ProcessRead(In1), new ProcessRead(In2) };
            CSPParallel   par     = new CSPParallel(parRead);

            while (true)
            {
                par.run();
                int i1 = (int)parRead[0].value;
                int i2 = (int)parRead[1].value;
                //Debug.WriteLine("Plus values are " + i1 + " and " + i2 );

                Out.write((int)(i1 + i2));
            }
        }
예제 #29
0
 public void run()
 {
     long[] sample = { 1000, 250, 100 };
     int[]  count  = { 10, 40, 100 };
     while (true)
     {
         for (int cycle = 0; cycle < sample.Length; cycle++)
         {
             reset.write(sample[cycle]);
             Console.WriteLine("\nSampling every " + sample[cycle] +
                               " ms ...\n");
             for (int i = 0; i < count[cycle]; i++)
             {
                 int n = (int)c.read();
                 Console.WriteLine("\t==> " + n);
             }
         }
     }
 }
예제 #30
0
        public void run()
        {
            int i = 1000;

            while (i > 0)
            {
                Console.Write("\nEnter next number (-100, 100):\t");
                string input = Console.ReadLine();

                if (!int.TryParse(input, out i))
                {
                    Console.WriteLine("You have entered invalid number");
                }
                else
                {
                    outChannel.write(i);
                }
            }
        }