Title: Simulation with PsimJ2 and Java
1Simulation with PsimJ2 and Java
2Synchronous Cooperation
- Is carried out by two or more processes that
execute a joint activity for a finite time
interval. - This involves the simultaneous participation of
the processes - One example is the modeling of direct
communication among processes (inter-process
communication).
3Master-Slave Synchronization
- In PsimJ2, one process is chosen as the master.
This is the dominant process during the
cooperation interval. - The other processes are the slaves. These are
modeled as passive participants during the
interval of cooperation. - When the cooperation ends, all processes continue
with their own independent activities.
4Joint Activity
- For two processes to cooperate
- The master process requests a cooperation with a
slave process to execute a joint activity. - The slave process requests a cooperation with a
master process in order to execute a joint
activity
5Joint Activity Between Master and Slave
6Synchronized Wait in the Cooperation
- When the master process requests cooperation, and
there is no slave process available, the master
process has to wait - If the master process has to wait, it is
suspended and placed on the master queue - When a slave process requests cooperation, and
the master is not available, the slave process
has to wait (suspended and placed on the slave
queue)
7Synchronization Object
- The mechanism that implements this type of
synchronization is defined in class Waitq. - An object of this class is used as the passive
object necessary for the synchronization of the
processes (active objects) involved
8Master-Slave Cooperation
9Waitq Class in PsimJ2
- The cooperation mechanism is implemented in
class Waitq - The synchronization mechanism is controlled by an
object of class Waitq. - The relevant public members of class Waitq
- length(), for length of the slave queue
- lengthm(), for length of the master queue
- qwait(), called by slave processes
- coopt(), called by master processes
10Defining an Object of Class Waitq
- Declare and create an object of class Waitq
- Waitq coop_obj
- coop_obj new Waitq(Coop)
-
11Cooperation Requests
- Method coopt() is invoked by the master process.
For example, - sl_obj (Slave ) coop_obj.coopt() // get ref
to slave - The call is invoked on object coop_obj.
12Cooperation Requests
- Method qwait() is invoked by the slave process.
For example, - coop_obj.qwait() // request cooperation
13Deciding to Cooperate
- As with other classes in PsimJ2, a process may
decide not to cooperate if there are no slave
processes in the slave queue - if(coop_obj.length() gt 0) // slaves available?
- sl_obj (Slave) waitq_obj.coopt()
- // joint activity with slave process
-
- else
- // perform some other activity
-
14Model with Synchronous Communication
- There are several sender processes
- There are several receiver processes
- A channel connects a receiver process to a sender
process. - A sender process directly communicates to with a
receiver process in order to transfer a message.
15Model Implementation
- The sender processes are objects of class Sender
- The receiver processes are objects of class
Receiver - The channels are passive objects of class Comchan
that include a reference to class Waitq - The model is stored in file scomm.jar.
16Results of a Simulation Run
- Trace that shows sequence of events the times
that a pair of processes communicate - Total wait period for each receiver process
- Total wait period for each sender process
- Channel utilization
17Synchronous Communication Between Two Processes
18Multiple Slave Processes
- A single master process may need to cooperate
with several slave processes simultaneously
during cooperation interval - A simple solution is for the master process to
place every slave process in a queue until the
end of the cooperation interval
19Java Implementation for Several Slave Processes
int slave 1 // current number of slave
processes Squeue s_queue new Squeue(Slaves)
while ( slave lt NUM_SLAVES ) slave_ref
(Slave) waitq_obj.coopt() s_queue.into(slave_r
ef) // place slave process in s_queue
slave // perform the joint activity //
dequeue and reactivate the slave processes