Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ucbvax!CORY.BERKELEY.EDU!dillon
From: dillon@CORY.BERKELEY.EDU (Matt Dillon)
Newsgroups: comp.sys.amiga.tech
Subject: Re: Need info on exceptions
Message-ID: <8808190629.AA04614@cory.Berkeley.EDU>
Date: 19 Aug 88 06:29:24 GMT
Sender: daemon@ucbvax.BERKELEY.EDU
Lines: 49

:Hello to everyone from a new reader.
:
:I am looking for a way to cause my program to take an exception periodically
:so that it can write out intermediate results of a long calculation* (see
:below). Essentially, I would like something similar to the Unix 
	...
:    3) Worst comes to worst, I have been considering running a separate
:         timing task which has received a message from the calculation task 
:         containing (ready for this?) the address of the calculation data.


	The latter, using a separate process, is exactly how you want to
do it.  Unfortunetly, AmigaDOS handlers are not setup properly to handle
multiple requests from the *SAME* process at the same time, so you can't
really trust a DOS library call from an exception handler.

	Also, exceptions are a bitch.

	In anycase, to be able to make DOS calls you need a process, so use
CreateProc() to startup a watchdog process and the timer.device to make it
wait in intervals of (whatever)... then use a simple locking mechanism between
the two processes so the data isn't in the middle of being updated while the
watchdog proc is writing it to disk.

	CreateProc() takes a segment rather than a simple pointer.  This can
be easily faked.  Even better though, compile the watchdog process separately
from the master process so you can simply LoadSeg() it (and get the segment
needed for CreateProc())... again, a simple signalling mechanism can be used
to sync up the two processes. 

	Faking a segment is easy.  Essentially, a segment is a BPTR to a 
segment, so to fake one we just apply reverse engineering:

	StartPC = (segment << 2) + 4

	segment = (StartPC - 4) >> 2,  where StartPC MUST be on a longword
boundry.  The first four bytes of the segment (the four bytes below the
startpc) should be 0. 

	Apart from that, you only have to worry about sharing non-runtime
library routines.  I.E. you can't use STDIO or malloc() in both processes
when they use the same LINK library (if you make the second a separate 
executable this isn't a problem).  However, all the run-time libraries like
intuition, exec, and dos are by virtue sharable.

	If the second process is another executable, I usually do not use
the standard main() entry point since the arguments will be garbage, but
use _main() instead.

					-Matt