[comp.sys.amiga.tech] background jobs

scotth@harlie.sgi.com (Scott Henry) (03/13/89)

In article <1243@hudson.acc.virginia.edu> pmy@vivaldi.acc.Virginia.EDU (Pete Yadlowsky) writes:
>  Can someone show me a way to make a program detach itself from the
>  CLI from which it was invoked? That is, I'd like to have a particular
>  program put itself in the background if it was not explicitly 'run'.
>  Thanks for any help.

If you are using Aztec C V3.6, they include a function (and instructions)
on how to do this. You need to set up a couple of externals in your main,
then link with detach.(o|o32) (depending on 16/32 bit integers). It is
adequately documented in the manual (though hard to find).

>	   - Pete

--
---------------------
              Scott Henry <scotth@sgi.com>
#include <std_disclaimer.h>

pmy@vivaldi.acc.Virginia.EDU (Pete Yadlowsky) (03/14/89)

Can someone show me a way to make a program detach itself from the
CLI from which it was invoked? That is, I'd like to have a particular
program put itself in the background if it was not explicitly 'run'.
Thanks for any help.

	- Pete


Peter M. Yadlowsky
Academic Computing Center
University of Virginia
pmy@Virginia.EDU

G35@DHDURZ1.BITNET (Werner Guenther) (03/17/89)

[....]

In article <1243@hudson.acc.virginia.edu> Pete Yadlowsky <pmy@vivaldi.acc.v
irginia.edu> writes:
>Can someone show me a way to make a program detach itself from the
>CLI from which it was invoked? That is, I'd like to have a particular
>program put itself in the background if it was not explicitly 'run'.


The code to detach from your CLI process is quite simple. If you are using
a C compiler, just use one of their detaching startup files, both Lattice and
Manx did put them into their libraries.
 If you are writing a program in assembler, this is the general way to go:

Programs are loaded by the scatter loader into different hunks (sections,
segments, whatever). These segmets are linked together by a so called
segment list. Each entry starts with two longwords containing:
the length of this hunk (in longwords),
a pointer to the next hunk (BCPL).

What you have to do now, is to split your program into (at least) two
hunks: the first one containing your detach routine, the second one your
main program.
The detach routine has to read the pointer to the next hunk (the main program)
and starts the second hunk as a process (using CreateProc()). Then zero
the pointer to the next hunk (still the main program), and exit (rts or
Exit()).
 The only change you have to do in the main program, is that it has to
UnLoad() itself before exiting.

I don't have any manuals here at work, so I won't add a sample listing,
but I promise to send a short one tomorrow.
Hope it helps,
Werner

G35@DHDURZ1.BITNET (Werner Guenther) (03/17/89)

In article <8903161837.AA02387@jade.berkeley.edu> Werner Guenther <G35@dhdurz1.b
itnet> writes:

>In article <1243@hudson.acc.virginia.edu> Pete Yadlowsky <pmy@vivaldi.acc.v
>irginia.edu> writes:
>>Can someone show me a way to make a program detach itself from the
>>CLI from which it was invoked? That is, I'd like to have a particular
>>program put itself in the background if it was not explicitly 'run'.

[....]

>I don't have any manuals here at work, so I won't add a sample listing,
>but I promise to send a short one tomorrow.

Here it is:

            XDEF    _SysBase
            XDEF    _DOSBase
            XDEF    _stdout

            XREF    _LVOOldOpenLibrary
            XREF    _LVOCreateProc
            XREF    _LVOForbid
            XREF    _LVOUnLoadSeg
            XREF    _LVOOutput
            XREF    _printf

AbsExecBase equ     4

start       CODE    FireUp

            lea     input,a1           ;commadline has to be copied
getparms    move.b  (a0)+,(a1)+
            dbf     d0,getparms

            move.l  AbsExecBase,a6
            move.l  a6,_SysBase

            lea     dosname,a1
            jsr     _LVOOldOpenLibrary(a6)
            move.l  d0,_DOSBase

            move.l  d0,a6
            jsr     _LVOOutput(a6)
            move.l  d0,_stdout
*
* This is the segment splitting routine
*
            lea     start(PC),a0        ;pointer to the first byte in our hunk
            move.l  -4(a0),d3           ;save BPTR to next hunk
            clr.l   -4(a0)              ;split segment
            move.l  d3,segptr           ;we need this one later
            move.l  #name,d1            ;Name
            move.l  #5,d2               ;Priority
            move.l  #2000,d4            ;Stack
            jsr     _LVOCreateProc(a6)  ;start our main program

            rts                         ;and exit to CLI


Main        CODE

            pea     input            ;print something
            move.l  segptr(PC),d1
            lsl.l   #2,d1
            move.l  d1,-(a7)
            pea     text(PC)
            jsr     _printf
            lea     12(a7),a7

            move.l  _SysBase(PC),a6
            jsr     _LVOForbid(a6)        ;better Forbid() or we may
            move.l  _DOSBase(PC),a6       ;get UnLoaded() before exiting
            move.l  segptr(PC),d1
            jsr     _LVOUnLoadSeg(a6)     ;UnLoad() own Task
quit        rts                                ;and exit

segptr      dc.l    0
_SysBase    dc.l    0
_DOSBase    dc.l    0
_stdout     dc.l    0
name        dc.b    'TestProc',0
dosname     dc.b    'dos.library',0
text        dc.b    'Task has been started at 0x%08lx '
            dc.b    'with these parameters:,10,'%s',0

            BSS
input       ds.b    256
            END

Later,
Werner