[comp.sys.amiga.tech] MultiTasking HELP! Please...

misterx@sun.ufnet.ufl.edu ( ) (10/02/89)

Hello multi-tasking GURUs,

	I have some questions about startup code, processes, and tasks
that I hope someone out there can answer!

1.	What is the correct sequence of events for starting a process?
I have come up with LoadSeg the program, CreateProc it and it should
go,go,go!  But it doesn't.  I think I have a problem with the startup
code provided (in my case) by Lattice.  This leads to question 2.
2.	What is necessary in the startup code for a process started by
another process?  Do tasks need startup code?  What I mean by startup
code is the absolute minimum needed for a process (or task) to store
whatever info it needs from the parent (or whereever) so that it doesn't
munge up.
3.	What is the correct sequence of events for starting a task?  Can
a task be LoadSeg()ed in by a process?  I looked at Matt Dillon's
many-task example (written in Aztec C) and was completely baffled by his
loading of the A4 (?) register.  Why is he saving A4?  Must I always save
A4 in all my tasks?  Is Dan Quayle really VP?
Any pointers to commented source code, english language descriptions, underground
newspapers, or a good resturant welcome; as always, I will summarize the
responses.

Thanks!

--
"Hey Wang, any of these guys savvy English?"
UUCP: ...!gatech!uflorida!sun.ufnet.ufl.edu!misterx  
Internet: misterx@sun.ufnet.ufl.edu | vishnu@pine.circa.ufl.edu

cmcmanis%pepper@Sun.COM (Chuck McManis) (10/07/89)

In article <20974@uflorida.cis.ufl.EDU> misterx@sun ( ) writes:
>1.	What is the correct sequence of events for starting a process?
>I have come up with LoadSeg the program, CreateProc it and it should
>go,go,go!  But it doesn't.  I think I have a problem with the startup
>code provided (in my case) by Lattice.  This leads to question 2.

Hmmm, I haven't used CreateProc() but I have used CreateTask() a few
times...

>2.	What is necessary in the startup code for a process started by
> another process? 
If the process is composed of C code, it should, at a minimum, have 
ExecBase initialized (and SysBase). Other than that there are no requirements.
If you aren't using main.c (linking with c.o) then you will want to use
	struct ExecBase	**SysBase = (struct ExecBase **)4;
	struct ExecBase *ExecBase;

	ExecBase = *SysBase; /* Get ExecBase loaded up. */

> Do tasks need startup code?  What I mean by startup
> code is the absolute minimum needed for a process (or task) to store
> whatever info it needs from the parent (or whereever) so that it doesn't
> munge up.

No, tasks can be nothing more than a subroutine in your program if you so
choose.

>3.	What is the correct sequence of events for starting a task?

The easiest way in Lattice to set up a task, it to put the source to your
subroutine in a different file, and declare that routine :
__saveds void 
MyRoutine()
{
}

Note that you don't pass it any parameters, and it doesn't return any.
Then you can start it with 
	CreateTask(MyRoutine, 0, 0); /* I forget the parameters, but you
				        only need the start address */ 

>  Can a task be LoadSeg()ed in by a process?  

It can be, but in general it is easier to make the Task code just a subroutine
in your program. That way it will be LoadSeg'd at when the program runs and 
then you don't have to do that yourself.

> Why is he saving A4?  Must I always save A4 in all my tasks?  

The way Aztec and Lattice C run in "small" model, is to store in A4 a pointer
to the area where all the data is, and then references to variables and such
can just be references off of A4. Unfortunately, when you start a new task
it gets a bright and shiny new set of registers and A4 does *not* point to your
data. So with Aztec you have to use saveA4() to put a copy of A4 somewhere safe
and then geta4() in the task to restore it to its proper value. The __saveds
keyword does this for you in Lattice. 

The other caveat for Lattice users is that you have to compile with stackchecking
disabled on the task code because it will have a different stack than the parent 
and that will cause problems when the stack code goes to check what it thinks the
boundaries of the stack are. 

Of course the easiest way to start a separate process in Lattice is to use 
fork(). Just write another program, compile and link it, and then fork it when
you want it to run. 

--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.
"If I were driving a Macintosh, I'd have to stop before I could turn the wheel."