synchronous process cooperation object oriented simulation with oosiml chapter 26 (c) j. m. garrido1
TRANSCRIPT
Synchronous Process Synchronous Process CooperationCooperation
Object Oriented Simulation Object Oriented Simulation with OOSimLwith OOSimL
Chapter 26Chapter 26
(C) J. M. Garrido(C) J. M. Garrido 11
Process CooperationProcess Cooperation
Needed when two or more processes Needed when two or more processes are to carry out a are to carry out a joint activityjoint activity for a for a finite interval.finite interval.
Used to model Used to model direct cooperationdirect cooperation among processes among processes
In both cases, the In both cases, the simultaneous simultaneous participationparticipation of the processes involved of the processes involved is required.is required.
(C) J. M. Garrido(C) J. M. Garrido 22
Master-Slave Master-Slave SynchronizationSynchronization
One process is chosen as the One process is chosen as the mastermaster. . This is the dominant process during the This is the dominant process during the cooperation period.cooperation period.
The other processes are the The other processes are the slavesslaves. . These are modeled as These are modeled as dormantdormant participants during the period of participants during the period of cooperation.cooperation.
When the period of cooperation ends, When the period of cooperation ends, all processes continue with their all processes continue with their independent activities.independent activities.
(C) J. M. Garrido(C) J. M. Garrido 33
Joint ActivityJoint Activity
For two processes to cooperate:For two processes to cooperate: The The mastermaster process requests a process requests a
cooperation with a slave process while cooperation with a slave process while executing some operation.executing some operation.
The The slaveslave process requests a cooperation process requests a cooperation with a master process while executing with a master process while executing some operation some operation
When the slave process is not available, When the slave process is not available, the the mastermaster process has to process has to waitwait and is and is suspended until a slave process becomes suspended until a slave process becomes availableavailable
(C) J. M. Garrido(C) J. M. Garrido 44
Cooperation IntervalCooperation Interval
When the interaction starts, the When the interaction starts, the slaveslave process is suspended until the end of process is suspended until the end of the cooperation interval. the cooperation interval.
After the cooperation interval, the After the cooperation interval, the master process then master process then reactivatesreactivates the the slave processslave process
The two processes will continue their The two processes will continue their own independent activities.own independent activities.
(C) J. M. Garrido(C) J. M. Garrido 55
Cooperation IntervalCooperation Interval
(C) J. M. Garrido(C) J. M. Garrido 66
Process Cooperation in Process Cooperation in OOSimLOOSimL
OOSimL supports the cooperation of OOSimL supports the cooperation of processes via cooperation objects. processes via cooperation objects.
These objects implement a These objects implement a synchronization mechanism for the synchronization mechanism for the processes to cooperate. processes to cooperate.
This mechanism allows one or more This mechanism allows one or more processes to dominate and be treated as processes to dominate and be treated as mastermaster processes; the other processes processes; the other processes are treated as are treated as slaveslave processes. processes.
(C) J. M. Garrido(C) J. M. Garrido 77
Synchronization MechanismSynchronization Mechanism
The synchronization mechanism used for the The synchronization mechanism used for the process cooperation is provided by objects of process cooperation is provided by objects of class class WaitqWaitq, known as , known as cooperation objectscooperation objects. .
These objects support the cooperation of These objects support the cooperation of multiple slave processes and multiple master multiple slave processes and multiple master processes. processes.
Every cooperation object has two hidden Every cooperation object has two hidden queues:queues: the slave queuethe slave queue the master queuethe master queue
(C) J. M. Garrido(C) J. M. Garrido 88
Master - Slave InteractionMaster - Slave Interaction
(C) J. M. Garrido(C) J. M. Garrido 99
Synchronized Wait in the Synchronized Wait in the CooperationCooperation
When the master process requests When the master process requests cooperation, and there is no slave cooperation, and there is no slave process available, the master process is process available, the master process is suspendedsuspended and placed on a and placed on a master master queuequeue..
When a slave process requests When a slave process requests cooperation, and the master is not cooperation, and the master is not available, the slave process is available, the slave process is suspendedsuspended and placed on a and placed on a slave slave queuequeue..
(C) J. M. Garrido(C) J. M. Garrido 1010
Cooperation Statement in Cooperation Statement in OOSimLOOSimL
Processes initiate the cooperation by Processes initiate the cooperation by using the facilities provided by the using the facilities provided by the cooperating object of class cooperating object of class WaitqWaitq
A slave process that requests A slave process that requests cooperation with a master process, cooperation with a master process, executes the executes the waitwait statement. statement.
A master process that requests A master process that requests cooperation with a slave process, cooperation with a slave process, executes the executes the cooperatecooperate statement. statement.
(C) J. M. Garrido(C) J. M. Garrido 1111
Master-Slave CooperationMaster-Slave Cooperation
(C) J. M. Garrido(C) J. M. Garrido 1212
Objects of Class Objects of Class WaitqWaitq
Creating and declaring an object of Creating and declaring an object of class class Waitq Waitq ::define coop_obj of class Waitq define coop_obj of class Waitq
create coop_obj of class Waitq using create coop_obj of class Waitq using
““M-S cooperation”,17M-S cooperation”,17
……
// processes request cooperation // processes request cooperation
(C) J. M. Garrido(C) J. M. Garrido 1313
Wait StatementWait Statement
This allows a slave process to This allows a slave process to cooperate with a master process, when cooperate with a master process, when there is a master process available.there is a master process available.
This statement places current process This statement places current process in the in the slave queueslave queue of the cooperation of the cooperation object and passivates (suspends) to object and passivates (suspends) to wait for a master process. wait for a master process.
(C) J. M. Garrido(C) J. M. Garrido 1414
Slave Process Requests Slave Process Requests CooperationCooperation
In the following lines of code allows a In the following lines of code allows a slave process waits for a master slave process waits for a master process to cooperate using the process to cooperate using the cooperation object coopt_obj.cooperation object coopt_obj.
// wait for master process to cooperate// wait for master process to cooperate
wait for master in coopt_objwait for master in coopt_obj
(C) J. M. Garrido(C) J. M. Garrido 1515
Master Process Requests Master Process Requests CooperationCooperation
A master process waits to cooperate with a A master process waits to cooperate with a slave process. slave process.
A slave process is retrieved from the slave A slave process is retrieved from the slave queue of the cooperation object, if this queue of the cooperation object, if this queue is not empty. queue is not empty.
If there is no slave process available in the If there is no slave process available in the slave queue, the master process that slave queue, the master process that executes this statement is suspended and executes this statement is suspended and placed in the master queue. placed in the master queue.
(C) J. M. Garrido(C) J. M. Garrido 1616
Example of Master Process Example of Master Process Requests CooperationRequests Cooperation
In the following lines of code a master In the following lines of code a master process waits to cooperate with a slave process waits to cooperate with a slave process using the process using the coopt_objcoopt_obj synchronization synchronization object. object.
When the cooperation becomes possible, a When the cooperation becomes possible, a slave process is removed from the slave slave process is removed from the slave queue, the master process continues queue, the master process continues executing normally. executing normally.
Otherwise, the process is suspended to wait Otherwise, the process is suspended to wait for a slave process.for a slave process.
(C) J. M. Garrido(C) J. M. Garrido 1717
Cooperate StatementCooperate Statement
define custobj of class Customerdefine custobj of class Customer
......
cooperate with slave custobj of class Customer cooperate with slave custobj of class Customer in coopt_objin coopt_obj
// execute when a slave is found// execute when a slave is found
(C) J. M. Garrido(C) J. M. Garrido 1818
Length of Slave QueueLength of Slave Queue
The The assign lengthassign length statement assigns the statement assigns the length (number of processes) in the slave length (number of processes) in the slave queue to a specified variable. queue to a specified variable.
This value represents the number of slave This value represents the number of slave processes waiting on the cooperation processes waiting on the cooperation (synchronization) object.(synchronization) object.
assign length of slave queue assign length of slave queue
< ref_var > to < var_name >< ref_var > to < var_name >
(C) J. M. Garrido(C) J. M. Garrido 1919
Length of Master QueueLength of Master Queue
The The assign lengthassign length statement gets the statement gets the length of the master queue (i.e., the length of the master queue (i.e., the number of master processes waiting number of master processes waiting on the cooperation object), and on the cooperation object), and assigns it to the specified variable. assigns it to the specified variable.
assign length of master queue assign length of master queue
< ref_var > to < var_name >< ref_var > to < var_name >
(C) J. M. Garrido(C) J. M. Garrido 2020
Deciding to CooperateDeciding to Cooperate
A master process may decide not to A master process may decide not to cooperate if there are no slave cooperate if there are no slave processes in the slave queue:processes in the slave queue:
define num_slaves of type integerdefine num_slaves of type integer
......
assign length of slave queue coopt_obj to num_slavesassign length of slave queue coopt_obj to num_slaves
If num_slaves > 0 thenIf num_slaves > 0 then
… … // joint activity with slave process// joint activity with slave process
else else
// carry out some other activity// carry out some other activity
endifendif(C) J. M. Garrido(C) J. M. Garrido 2121
Cooperation with Several Cooperation with Several SlavesSlaves
A master process can cooperate in a A master process can cooperate in a joint activity with several slave objects. joint activity with several slave objects.
The master process creates a The master process creates a separate separate queuequeue to place the slave processes. to place the slave processes.
For example, a master process that For example, a master process that needs to cooperate with needs to cooperate with NN slave slave processes defines and creates a queue processes defines and creates a queue to place each reference to slave process.to place each reference to slave process.
(C) J. M. Garrido(C) J. M. Garrido 2222
Example of Cooperating Example of Cooperating with Multiple Slaveswith Multiple Slaves
set numslaves = 0set numslaves = 0
create s_queue = of class Pqueue using create s_queue = of class Pqueue using
"Slave queue""Slave queue"
. . .. . .
while numslaves < N dowhile numslaves < N do
cooperate with slave slave_ref of class Slavecooperate with slave slave_ref of class Slave
in waitq_objin waitq_obj
// enqueue into slave queue// enqueue into slave queue
insert slave_ref into s_queue insert slave_ref into s_queue
increment numslavesincrement numslaves
endwhileendwhile ////
(C) J. M. Garrido(C) J. M. Garrido 2323
Example with Multiple Example with Multiple Slaves(2)Slaves(2)
At the end of the cooperation interval, At the end of the cooperation interval, the master process removes each the master process removes each slave process and reactivates it. slave process and reactivates it.
The master process then performs its The master process then performs its own activities. own activities.
The synchronization object, The synchronization object, waitq_objwaitq_obj of class Waitq, is defined in the top of class Waitq, is defined in the top class of the model.class of the model.
(C) J. M. Garrido(C) J. M. Garrido 2424
Reactivating the Slave Reactivating the Slave ProcessesProcesses
// carry out joint activity for// carry out joint activity for
// cooperation interval// cooperation interval
hold self for coop_int hold self for coop_int
// now release slave processes// now release slave processes
for j = 0 to N-1 dofor j = 0 to N-1 do
remove slave_ref of class Slave remove slave_ref of class Slave from s_queue from s_queue
reactivate slave_ref nowreactivate slave_ref now
endforendfor(C) J. M. Garrido(C) J. M. Garrido 2525
Case Studies that Use Case Studies that Use Process CooperationProcess Cooperation
Process communication modelProcess communication model The paging disk systemThe paging disk system
(C) J. M. Garrido(C) J. M. Garrido 2626
(C) J. M. Garrido(C) J. M. Garrido 2727
Model with Synchronous Model with Synchronous CommunicationCommunication
There are several There are several sendersender processes processes There are several There are several receiverreceiver processes processes A A channelchannel connects a receiver process connects a receiver process
to a sender process. to a sender process. A sender process directly A sender process directly
communicates to with a receiver communicates to with a receiver process in order to process in order to transfertransfer a a message.message.
Synchronous Synchronous CommunicationCommunication
(C) J. M. Garrido(C) J. M. Garrido 2828
(C) J. M. Garrido(C) J. M. Garrido 2929
Synchronous Synchronous Communication ModelCommunication Model
The sender processes are objects of class The sender processes are objects of class SenderSender
The receiver processes are objects of The receiver processes are objects of class class ReceiverReceiver
The channels are passive objects of class The channels are passive objects of class ComchanComchan that relates with class that relates with class WaitqWaitq
There is a channel for each pair of sender There is a channel for each pair of sender and receiverand receiver
Model ImplementationModel Implementation
The simulation model consists of five pairs The simulation model consists of five pairs of sender and receiver processes and is of sender and receiver processes and is implemented with five classes written with implemented with five classes written with OOSimL. OOSimL.
The source files are: Scomm.osl, The source files are: Scomm.osl, Sender.osl, Receiver.osl, Comchan.osl, and Sender.osl, Receiver.osl, Comchan.osl, and Message.osl. Message.osl.
These source files are archived in the file These source files are archived in the file scomm.jarscomm.jar, which also includes the output , which also includes the output listings of a sample simulation run.listings of a sample simulation run.
(C) J. M. Garrido(C) J. M. Garrido 3030
(C) J. M. Garrido(C) J. M. Garrido 3131
Results of a Simulation RunResults of a Simulation Run
Trace that shows sequence of events: Trace that shows sequence of events: the times that a pair of processes the times that a pair of processes communicatecommunicate
Total wait period for each receiver Total wait period for each receiver processprocess
Total wait period for each sender Total wait period for each sender processprocess
Channel utilization Channel utilization
(C) J. M. Garrido(C) J. M. Garrido 3232
Synchronous Communication Synchronous Communication Between Two ProcessesBetween Two Processes
Paging DiskPaging Disk A page disk receives random requests from A page disk receives random requests from
various processes.various processes. Each track of the disk is partitioned into Each track of the disk is partitioned into
NUM_SECTORS sectors that store one page NUM_SECTORS sectors that store one page each.each.
Requesting processes select a sector and Requesting processes select a sector and then wait for the completion of the then wait for the completion of the request. The paging disk rotates once request. The paging disk rotates once every 17.5 msec.every 17.5 msec.
(C) J. M. Garrido(C) J. M. Garrido 3333
Model DesignModel Design The model defines an array of The model defines an array of
NUM_SECTOR objects of class Waitq.NUM_SECTOR objects of class Waitq. The arrivals process generates The arrivals process generates
randomly the sector number for the randomly the sector number for the next arriving process, which requests next arriving process, which requests cooperation with the disk.cooperation with the disk.
The disk continuously scans the The disk continuously scans the sectors for requests.sectors for requests.
(C) J. M. Garrido(C) J. M. Garrido 3434