🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.tech
Article #12300

Re: CreateTask() problems...

#12300
From: Randell Jesup
Date: Wed, 19 Apr 1989 03:00
56 lines
2665 bytes
In article <740LK-KARI@FINTUVM> LK-KARI@FINTUVM.BITNET (Kari Sutela) writes:
>BUT I need to create a task that *outlives* its parent and I haven't managed
>to do this in a reliable manner.
...
>void main()
>{
...
>    MyTask = CreateTask( "MyTask", 0L, the_task, 4000L );
...
>    return;
>}
>void the_task()
>{
...
>}
>Any hints on this? Have I completely misunderstood the semantics of CreateTask
>and task behaviour in general? Am I mistaken in assuming that a task can use
>resources opened by its parent (even after the parent has died) and close the
>resources when it exits? The Exec manual isn't too clear on this.

	When your main() program exits, the memory it is stored in (the entire
compiled program) is freed back to the system.  The results will be random,
and depends on what is allocating memory and where your program was loaded.
I'm suprised the system didn't crash quickly.

	The system does no automatic resource tracking.  It has no knowlege
that you are still using code that was loaded by the original task (how
could it in this case?)  In general, programs must release resources they
allocated before exiting (often the C compiler exit() routine will free
C library-allocated items - malloc()ed memory, fopen()ed files, etc.)

	To have the new task last after the main one, you must somehow
stop the memory from being freed, or load it into memory seperately, or
if it is position-independant (C usually isn't on the Amiga) you can copy
it to memory you AllocMem()ed.

	I recommend LoadSeg()ing the task code seperately (note LoadSeg returns
a BPTR to the longword BEFORE the entry point).  Make sure the task releases
it's memory when exiting, or that memory will be lost forever.  A way to do
this is:

	void newtask ()
	{
	...
	Forbid();
	UnLoadSeg(seg);	/* note - you must have a way to get the BPTR returned
			   by LoadSeg().  Sending a message with it (and any
			   other parameters) after CreateTask()ing is a good
			   way. */
	}

	This works because you can use memory that has been freed while under
forbid (just don't make any calls that might Wait() for something...!)

-- 
Randell Jesup, Commodore Engineering {uunet|rutgers|allegra}!cbmvax!jesup
Technical Details
Message-ID: <6632@cbmvax.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!dptcdc!jarvis.csri.toronto.edu!mailrus!husc6!rutgers!apple!oliveb!amiga!cbmvax!jesup
References: <740LK-KARI@FINTUVM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!dptcdc!jarvis.csri.toronto.edu!mailrus!husc6!rutgers!apple!oliveb!amiga!cbmvax!jesup
From: jesup@cbmvax.UUCP (Randell Jesup)
Newsgroups: comp.sys.amiga.tech
Subject: Re: CreateTask() problems...
Message-ID: <6632@cbmvax.UUCP>
Date: 19 Apr 89 03:00:09 GMT
References: <740LK-KARI@FINTUVM>
Reply-To: jesup@cbmvax.UUCP (Randell Jesup)
Organization: Commodore Technology, West Chester, PA
Lines: 56