[comp.unix.questions] Implementing `fork' in a single address space

pardo@cs.washington.edu (David Keppel) (12/10/89)

I'd like to write a micro-unix to run in a single Un*x address space.
I'm having problems figuring out how to implement `fork' correctly in
one address space.  Perhaps it's impossible.  I'm willing to have
stack frames point at the other guy's data, but the only ways that I
can think of to get stacks to return right are:

* To walk back the stack and patch up the retunr addresses -- scary!

* Have all stacks live at one place in the address space.  That
  requries swapping stacks at every context switch.

Perhaps a related question:

* Are there any pd (public-domain, or freeware) micro-unicies that I
  can look at?  I remember that you used to be able to get a 3-ring
  binder with the version 6 source, but I don't know if you had to
  have a source liscence and all that.

Thoughts?  Thanks!

	;-D on  ( Unix is a tratemark of Bell Laboratories )  Pardo
-- 
		    pardo@cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo

meissner@dg-rtp.dg.com (Michael Meissner) (12/12/89)

In article <10131@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:

|  I'd like to write a micro-unix to run in a single Un*x address space.
|  I'm having problems figuring out how to implement `fork' correctly in
|  one address space.  Perhaps it's impossible.  I'm willing to have
|  stack frames point at the other guy's data, but the only ways that I
|  can think of to get stacks to return right are:
|  
|  * To walk back the stack and patch up the retunr addresses -- scary!
|  
|  * Have all stacks live at one place in the address space.  That
|    requries swapping stacks at every context switch.
|  
|  Perhaps a related question:
|  
|  * Are there any pd (public-domain, or freeware) micro-unicies that I
|    can look at?  I remember that you used to be able to get a 3-ring
|    binder with the version 6 source, but I don't know if you had to
|    have a source liscence and all that.
|  
|  Thoughts?  Thanks!

Another way to do it is with a consenting compiler, and no hostile
assembly language programs.  Basically you teach the compiler to
ignore one or two of the {address} registers, and then every reference
to data and text is based off of these registers.  To do a context
switch, the kernel merely saves the registers, and loads up the new
registers.  To do a fork, the OS then allocates a new area somewhere,
sets up the one or two registers as appropriate, and fires off the
child.
--
--
Michael Meissner, Data General.
Until 12/15:	meissner@dg-rtp.DG.COM
After 12/15:	meissner@osf.org

peter@ficc.uu.net (Peter da Silva) (12/13/89)

This is why I wish POSIX provided an alternative to the fork() semantics:

In article <10131@june.cs.washington.edu> pardo@june.cs.washington.edu (David Keppel) writes:
> I'm having problems figuring out how to implement `fork' correctly in
> one address space.

You have to copy the entire stack and data segment on a context switch while
the two forks of the program are both running. This is typically implemented
by forcing them to swap. Once one exits or execs, you can quit doing it.

> * Are there any pd (public-domain, or freeware) micro-unicies that I
>   can look at?

MINIX, by Andy Tannenbaum. ast@cs.vu.nl
-- 
`-_-' Peter da Silva. +1 713 274 5180. <peter@ficc.uu.net>.
 'U`  Also <peter@ficc.lonestar.org> or <peter@sugar.lonestar.org>.
"It was just dumb luck that Unix managed to break through the Stupidity Barrier
and become popular in spite of its inherent elegance." -- gavin@krypton.sgi.com

roland@cochise (12/13/89)

pardo@cs.washington.edu (David Keppel) writes:

>I'm having problems figuring out how to implement `fork' correctly in
>one address space.  Perhaps it's impossible.

>Perhaps a related question:

>* Are there any pd (public-domain, or freeware) micro-unicies that I
>  can look at?

You might look for the source-available unix-lookalike "Minix" by
Andy Tanenbaum ( published by Prentice Hall ).

  The problem of implementing the fork call on the Atari ST has been
disputed here on the net rather lengthy.


             I know that You believe You understand what You think I said, but
             I'm not sure You realize that what You heard is not what I meant.

Roland Rambau

  rra@cochise.pcs.com,   {unido|pyramid}!pcsbst!rra,   2:507/414.2.fidonet