Title: JCSP Tutorial
1JCSP Tutorial Homework Hints
2JCSP Tutorial
- The JCSP Library
- JCSP Processes
- Channels Interfaces
- Alting Channels Event Selection
- The Parallel Construct
- Putting It Together A Simple Example
3The JCSP Library
- Developed by Peter Welch and Paul Austin
- Download Here
- http//www.cs.kent.ac.uk/projects/ofa/jcsp/
- Include the JCSP library in your project with
- import jcsp.lang.
4JCSP Processes
- The process is the basic construct in JCSP
- Simply a class that implements the CSProcess
interface. - Ex
- public class MyProcess implements CSProcess
-
- public void run()
-
5Channels Interfaces (1)
- Processes are wired together using channels.
- ChannelInput interface
- Interface for reading from a channel
- Consists of single method called read
- If called on an object, it will block until an
object is actually written on the channel by a
process at the other end of the channel. - ChannelOutput interface
- Interface for writing from a channel
- Consists of a single method called write(Object
o). - A process that calls this method on an object
will block until the object is accepted by the
channel.
6Channels Interfaces (2)
- Types of channel classes
- One2OneChannel ? implements a "single-writer-singl
e-reader" type channel - One2AnyChannel ?implements a "single-writer-multip
le-readers" object channel. (Note This is not a
broadcast mechanism in that the multiple readers
actually compete with each other for reading from
the channel only one reader can use the channel
along with the writer at any given time.) - Any2OneChannel ? implements a "multiple-writers-si
ngle-reader" object channel. As in the above
case, writing processes compete with each other
to use the channel. Only the reader and one of
the multiple writers can actually use the channel
at any given time - Any2AnyChannel ?implements a "multiple-writers-mul
tiple-readers" object channel. Reading processes
compete with each other to use the channel, as do
writing processes. Only one reader and one writer
can actually use the channel at any one time.
7Channels Interfaces (3)
- Channels are non buffering by default, but JCSP
supports buffering. - Buffering is delegated to another class, which
must implement an interface called
ChannelDataStore. JCSP provides multiple built-in
implementations for this interface, including the
following - ZeroBuffer default non buffering channel
- Buffer provides a blocking FIFO buffered
semantics for the channel with which it is
associated. - InfiniteBuffer provides a FIFO semantics,
except that only readers can be blocked if the
buffer is empty. Writers are never blocked
because the buffer capacity can be expanded
indefinitely, or at least until the limit imposed
by the underlying memory system is reached.
8Alting Channels Event Selection
- A channel guard in JCSP can be of the following
types AltingChannelInput / AltingChannelInputInt,
which are ready whenever object/integer data
(respectively) is pending in the corresponding
channel The Alternative class provides methods
for event-selection - The method select() of the Alternative class
corresponds to an arbitrary selection strategy. - The method priSelect() corresponds to the highest
priority strategy. - The method fairSelect is fair in choosing between
the more-than-one ready guards
9The Parallel Construct
- Takes an array of individual CSProcess instances
and runs them in parallel. - A run of a Parallel process terminates when, and
only when, all its component processes terminate.
- A mechanism for composing multiple individual
processes together, using channels as "wires" to
connect them. - Example
- new Parallel
- (
- new CSProcess
-
- new SendEvenIntsProcess (chan),
- new ReadEvenIntsProcess (chan)
-
- ).run ()
10Putting It All TogetherA Simple Example (1)
- Consider a simple process that produces the
integers from 1 to 100 and another process which
consumes them. - Need 3 Processes
- Producer Process
- Consumer Process
- Driver Process to instantiate and start the
producer and consumer processes. - Producer and Consumer are wired together using
ChannelInput and ChannelOutput interfaces.
11Putting It All TogetherA Simple Example (2)
Int Producer Process
Int Consumer Process
Channel
Channel Input
Channel Output
12Putting It All TogetherA Simple Example (3)
- Producer process to generate even integers
between 1 and 100 - import jcsp.lang.
- public class SendEvenIntsProcess implements
CSProcess -
- private ChannelOutput out
- public SendEvenIntsProcess(ChannelOutput out)
-
- this.out out
-
- public void run()
-
- for (int i 2 i lt 100 i i 2)
-
- out.write (new Integer (i))
-
13Putting It All TogetherA Simple Example (4)
- Consumer process to read even integers between 1
and 100 - import jcsp.lang.
- public class ReadEvenIntsProcess implements
CSProcess -
- private ChannelInput in
- public ReadEvenIntsProcess(ChannelInput in)
-
- this.in in
-
- public void run()
-
- while (true)
-
- Integer d (Integer)in.read()
- System.out.println("Read "
d.intValue())
14Putting It All TogetherA Simple Example (5)
- The Driver Program
- import jcsp.lang.
- public class DriverProgram
-
- public static void main(String args)
-
- One2OneChannel chan new One2OneChannel()
- new Parallel
- (
- new CSProcess
-
- new SendEvenIntsProcess (chan),
- new ReadEvenIntsProcess (chan)
-
- ).run ()
-
-
15Homework Hints (problem 1)Producer / Consumer
- The Producer process takes in the number of
items and the speed at which it will produce an
item as parameters. - The Buffer process implements a circular queue of
size 20. Once the Buffer process detects that the
queue has been emptied, it will send a signal to
the two consumers to indicate that the buffer is
empty. - The two Consumers will run at different speeds as
defined by a parameter passed to it . The two
consumers should then terminate normally when the
Buffer process informs that there are no more
items to consume.
16Homework Hints (problem 1)Producer / Consumer
- 4 Processes
- 1 driver process (i.e. main)
- Producer process
- Consumer process
- Buffer Process
17Homework Hints (problem 1)Producer / Consumer
Consumer1 receives items from the
buffer One2OneChannelInt chanConToBuff1 One2OneCha
nnelInt chanConFromBuff1
NOTE Buffer / Consumer channels are
Bidirectional. Consumers send request To buffer
when wanting ton consume. Buffer will send item
to consumer if requesting Items are available
in the buffer.
Producer sends items (ints) to the
buffer One2OneChannelInt chanProdBuff
Consumer2 receives items from the
buffer One2OneChannelInt chanConToBuff2 One2OneCha
nnelInt chanConFromBuff2
Note Buffer has potential to receive multiple
events (receiving an item from a consumer and
message that data is available from the producer.
Need event selection (i.e. Alting Channel).
18Homework Hints (problem 1)Producer / Consumer
- Producer Process
- public class Producer implements CSProcess
- public Producer(int numItems, int interval,
One2OneChannelInt toBuffer) - Create an int and send it to the buffer using
toBuffer.write(i) - Consumer Process
- public class Consumer implements CSProcess
- public Consumer(String str, int interval,
ChannelOutputInt toBuffer, One2OneChannelInt
fromBuffer) - signal the buffer we are ready to consume using
toBuffer.write(MORE) - read the data from the channel int item
fromBuffer.read()
19Homework Hints (problem 1)Producer / Consumer
- Driver Process
- Instantiate channels
- One2OneChannelInt ProdBuff new
One2OneChannelInt() - One2OneChannelInt ConBuff1 new
One2OneChannelInt() - One2OneChannelInt ConBuff2 new
One2OneChannelInt() - Instantiate producer, consumer, buffer processes
- Example Consumer consumer1 new Consumer(100,
ConToBuff1,ConFromBuff1) - Start processes running in parallel
- Example new Parallel ( new CSProcess
buffer,producer,consumer1, consumer2 ).run()
20Homework Hints (problem 2)Optimal Algorithm
- Ricart-Agrawal Algorithm Implement the Optimal
Algorithm for Mutual Exclusion for Computer
Networks as given by Ricart-Agrawal (the paper
was presented in class). The link for the paper
can be found in the reading list. - The number of nodes in the network (N) will be
passed as parameter. - Note Use message passing to resolve all
synchronizations. (Do not use semaphores).
21Homework Hints (problem 2)Optimal Algorithm
- Reviewing The Algorithm (1)
22Homework Hints (problem 2)Optimal Algorithm
- Reviewing The Algorithm (1)
23Homework Hints (problem 2)Optimal Algorithm
- Reviewing The Algorithm (3)
24Homework Hints (problem 2)Optimal Algorithm
- Parallel Inside Of Parallel Architecture
- Node Processes consist of 3 Sub Processes
- Mutex Process invokes mutual exclusion for the
node - Request Process process that receives request
messages - Reply Process process that receives reply
messages - Node contained a database of shared variables
between the 3 sub-processes must provide
protected access to them via mutex.
25Homework Hints (problem 2)Optimal Algorithm
Node (shared database)
Node (shared database)
26Project 1 Due Date Oct 12