Asking "Are You Alive?"
This is my solution to the "Are You Alive?" problem, discussed in this forum thread:
Download the script alivedemo.ijs
Launch two independent j602 (jwd) sessions and load alivedemo.ijs into each session.
On the Mac you can fire off a second independent process by copying and renaming the "red-J" app, then dragging it into the Dock alongside the original.
On Windows it will probably work to click the red-J shortcut a second time.
On being loaded, the script prompts with:
Decide whether this process is client or server... CLIENT=: 1 NB. CLIENT=0 (server) by default To run the "alive" demo, enter: start'' NB. built-in default # of duty cycles start 100 NB. 100 duty cycles Terminate the duty cycle using yellow-J.
Unfortunately the yellow-J isn't as much use as it might be, because it will halt both processes, not just one.
Call these sessions (=processes) 0 and 1. Enter CLIENT=:1 into session 1.
Session 0 has CLIENT=0 set by default.
Enter: start 100 into session 0, and 3 or 4 seconds later enter: start 100 into session 1.
Thus when session 0 stops, session 1 will still be running.
When session 0 stops, a "die" message will appear in session 1, up to a second later.
This is sample input/output from session 1:
start 100 duty: starts... mapex: re-mapped: CTL0 to: /Users/ianclark/j602-user/temp/ctl0.jmf mapex: re-mapped: CTL1 to: /Users/ianclark/j602-user/temp/ctl1.jmf ┌───┬─┬──────┬──────┬──┐ │die│1│709149│709151│55│ └───┴─┴──────┴──────┴──┘ duty: end.
Mode of operation
Sessions 0 and 1 run identical code except for having CLIENT=0 and CLIENT=1 respectively. So what is said about session 0 will apply also to session 1.
Session 0 maps nouns CTL0 and CTL1. Because CLIENT=0 it knows that CTL0 is its shared buffer to write to. It runs a hard duty cycle (using a while.-loop) which writes a timestamp to CTL0, an integer value derived from the time-of-day, to the nearest second. In principle a finer-grained timestamp is possible, modifying the code accordingly, but a technique that detects the death of the sister process up to 2 seconds later is probably good enough for most purposes.
When both duty cycles are running, CTL0 and CTL1 both get updated with the timestamp and keep within 1 of each other.
Viz diff=. | CTL1-CTL0 is either 0 or 1.
But when duty cycle 0 stops, CTL0 stops being updated and falls behind CTL1. The other duty cycle detects diff rising above the value 1 and accordingly calls the code-hook verb: die'' (once-only).
Note that each process can also detect its sister coming back to life, whereupon it calls the code-hook verb: resurrect'' (once-only).
Showing the process status
To trace execution, the data to show for each process is: (CLIENT , CTL0 , CTL1 , CYCLE) . We could have used smoutput to show it. But rather than clutter up the session window, or go to the effort of coding a UI form, we write this trace data to the actual jijx window caption using the verb cycle_caption. This offers a choice of ways of modifying the caption without losing the information it contains, viz to rotate it, or to prefix the 4 integers in square-brackets.
Contributed by IanClark