Subject: Re: forkexec(2)
To: None <tech-kern@NetBSD.ORG>
From: Ignatios Souvatzis <ignatios@theory.cs.uni-bonn.de>
List: tech-kern
Date: 04/13/1998 14:30:37
> 
> I think many of those cases can be handled with a carefully designed
> interface, and in some of the remaining cases the parent can contrive
> the proper environment for the child and then undo it afterwards.
> 
> Doing things this way though would probably be more costly than simply
> paying the price of a separate fork() + exec() [with COW, that is].

Yes, thats what "real" OS-9 programs used to do (that is: OS-9 level 1 and
level 2 on M6809); I don't know OS-9/m68000 personally.

All this dup()ing forth and back of file descriptors before/after
forkexec()ing the child... ugh...

You need something like:

/* save my versions */
savestdin = dup(0);
savestdout = dup(1);
savestderr = dup(2);
/* open childs versions */
close(0);
open(what I want to be at stdin in the child);
close(1);
open(what I want to be at stdout in the child);
close(2);
open(what I want to be at stderr in the child);
callexec();
/* restore my versions */
close(0); dup(savestdin);
close(1); dup(savestdout);
close(2); dup(savestderr);

Obviously, you can save a lot of overhead in a shell by pre-saving
std{in,out,err} and only using the _copies_ yourself ... no need for the 
restore path, and you only need to save once, anyway.

Regards,
	Ignatios