Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucbvax!agate!apple!bridge2!csi!nsc!pyramid!leadsv!laic!nova!darin
From: darin@nova.laic.uucp (Darin Johnson)
Newsgroups: comp.sys.amiga.tech
Subject: Re: Exiting, but leaving some code "resident"
Message-ID: <658@laic.UUCP>
Date: 10 Aug 89 18:27:45 GMT
References: <188@teslab.lab.OZ>
Sender: news@laic.UUCP
Reply-To: darin@nova.UUCP (Darin Johnson)
Organization: Lockheed AI Center, Menlo Park
Lines: 52

In article <188@teslab.lab.OZ> andrew@teslab.lab.OZ (Andrew Phillips) writes:
>Most of the program will be concerned with setting up the sprite etc
>and just a little bit will need to remain resident after the setup is
>complete.  Under MSDOS it is quite easy to set up a TSR (terminate,
>stay resident) program that leaves a part of itself at the top of memory
>and frees the memory occupied by all of its initialization code back to DOS.
>I'm not sure, however, what the best way to do this is on the Amiga.

Look at DMouse.  I used the same technique in MyMenu and it isn't that
hard - no fiddling with segment lists, etc.

Basically, you have 2 programs.  The first program is the one that the
user uses.  It parses the command line, does all the tedious setup,
etc.  Then it creates a named port to communicate with the second
program.  The first program will LoadSeg the first program.  The
segment list loaded, and any other data you want to pass to the second
program is hooked onto the named port.  Then you start the second
program as a process or task.  The second program will then look for
name port to get the information that it needs.  To make things safe,
the first program should signal the second and wait for a reply to make
sure that the second program is working.  After that, the first
program can exit, leaving the second program/process free to do what
it does.

Then, when you want to terminate the second program, you send a
'terminate' signal to the second program.  The second program will
then clean up, signal back, and exit (do a Forbid before sending the
reply signal and then immediately exit so that the first program will
not get the signal until the second one is dead).  The first program
then UnloadSeg's the second program (you saved the segments in the
named port, remember?), does whatever other cleanup it needs, and then
exits.

This allows you to make the second program as small as possible, since
memory allocation, command line parsing, setup, etc. can be done in
the first program with the appropriate data passed by being hung
off the message port.  Basically, declare the port as:

  struct myport {
    struct MsgPort port;
    BPTR segments;
    whatever else you want;
  }

(also, you could send the data with a startup message, but I haven't
tried that).  Also, no poking around any segment lists are required.

Both DMouse and MyMenu have shown up in comp.sources.amiga in the past.
Other programs do similar things, so poke around.

Darin Johnson (leadsv!laic!darin@pyramid.pyramid.com)
	We now return you to your regularly scheduled program.