39. Interprocess Interaction
Part of
CS:2820 Object Oriented Software Development Notes, Fall 2017
|
Consider the following bit of code from the above:
class Vehicle {
... details omitted ...
// tail of loop
void leaveRoad( float t ) {
// called when vehicle arrives at the end of a road
[tell currentRoad that we're exiting it, setting currentIntersection]
/**/ [tell currentIntersection we're here, it may make us wait ]
currentRoad = null;
if is not a sink intersection {
schedule( t, (float time)->this.pickOutgoing( time ) );
} else {
vehicle destroyed
}
}
}
Most of the above code is easy to expand into Java. Consider this bit:
[tell currentRoad that we're exiting it, setting currentIntersection]
Assuming that we write the appropriate methods in the Road class, we can translate this to Java code something like the following:
currentIntersection = currentRoad.exit();
The one piece of code above that does not give way to this simple interpretation is the following:
/**/ [tell currentIntersection we're here, it may make us wait ]
The problem with this code is that it involves inter-process interaction. Each intersection can be seen as a process, waiting for one vehicle to clear the intersection before the next enters, or waiting for the stoplight to change, and each vehicle is similarly a process, traveling down roads and through intersections. These processes must interact each time a vehicle reaches an intersection, so this is an example of inter-process interaction.
The most fundamental of inter-process interaction involves one process waiting for some other process to do something before it continues. Interprocess interactions are common in many contexts: multithreaded programs, interaction between programs running under an operating system, interaction between programs running on a multiprocessor computer or a computer with a multicore processor, and interaction between programs that communicate over a computer network.
For example, in a network, a client program may send a message to a server, requesting a service. Typically, the client then waits for the server to reply. In our case the vehicle is acting in the role of the client, asking the intersection for permission to proceed, but we have no message passing facility.
On a multicore or multiprocessor computer, or in a multithreaded programming environment, one way for one process to wait for another is to use a shared variable:
Process A {
...
v = true;
while (v) { /* wait */ }
...
}
Process B {
...
v = false; /* signal that A can continue */
...
}
In general, this is extremely unsafe. Roll-your-own solutions to interprocess interaction are very difficult to debug. This kind of code was typical of how people worked in the 1960s, before general frameworks for interprocess interaction were developed.
In our case, we have a scheduler in our simulation framework, and we can use its interface in a sensible way. When a logical process has to wait, for another process, the code for that process must be broken at the point where the wait occurs so that the second half may be scheduled when it is supposed to occur. Here is what this does to the code we began with:
class Vehicle {
... mostly unchanged ...
// tail of loop, first half
void leaveRoad( float t ) {
// called when vehicle arrives at the end of a road
currentIntersection = currentRoad.exit( t, v );
currentIntersection.enter( t, currentRoad, /* WHAT GOES HERE */ );
}
// tail of loop, second half
void finishLeaveRoad( float t ) {
currentRoad = null;
if is not a sink intersection {
schedule( t, (float time)->this.pickOutgoing( time ) );
} else {
vehicle destroyed
}
}
}
The comment /* WHAT GOES HERE */ in the above code is best answered by looking in the code we'll need inside implementations of Intersection:
class SomeKindOfIntersection extends Intersection {
...
void enter( float t, Road r, Something s ) {
// A vehicle wants to enter this intersection from road r at time t
if (allowed) {
// the intersection is clear
Simulator.schedule( t, s );
} else {
// the intersection is blocked
getqueue( r ).add( s );
}
}
}
Here, we see that, if the intersection is clear, whatever was passed as a parameter is scheduled, while if it is blocked, that same thing is queued up in some queue that depends on the incoming road until such time as the intersection is clear. Clearing the intersection will depend, for example, on some other vehicle leaving the intersection, or on the state of the stoplight changing, or on some similar detail.
So, the thing we pass to the intersection will be the same as the thing we pass to the scheduler! This means the code in class vehicle will look like this:
// tail of loop, first half
void leaveRoad( float t ) {
// called when vehicle arrives at the end of a road
currentIntersection = currentRoad.exit( t, v );
currentIntersection.enter( t, currentRoad,
(float time)->finishLeaveRoad( t )
);
}
And, it means that the type of the class of the final parameter to the Intersection.enter method is Simulator.Action. That, is, the correct header for that method is:
class SomeKindOfIntersection extends Intersection {
...
void enter( float t, Road r, Simulator.Action s ) {
...
}
}
It took a long time for people to invent a systematic way of doing this back in the late 1960s. Sadly, the general solution, invented by a student of E. W. Dijkstra in the Netherlands, was published after significant parts of Unix were developed, and as a result, these ideas made it into Unix (and Linux, which is Unix compatible) as afterthoughts. Design by afterthought almost always produces messy results that are never used in many applications that need them.
What we really ought to do is introduce tools into our class Simulation to package the ideas above. The classid packaging, dating back to the time of Dijkstra, is as a new public scheduler class, Semaphore (named after railroad semaphore signals) with two methods, wait() and signal().
If s is a semaphore and a is a semaphore action, then s.wait(a) either schedules a immediately or it sets a aside until it can be done. s.signal() tells the semaphore that if some action is waiting, it can go. What Dijkstra's student discovered is that the semaphore should behave, logically, like an integer. To a user, it is as if the wait operaiton decrements the integer, guaranteeing that it will never go negative by waiting. To a user, it is as if the signal operation increments the integer, which might allow a blocked action to continue.
Much more can be said about semaphores, and if you take an operating systems course or a parallel programming course, you will see more on this.
In fact, however, the logic of semaphores is hidden inside class StopLight in the road network simulator example we have been working with, we just didn't pick it out and make it into a general mechanism.