[comp.unix.questions] Making a popen replacement with vfork

steved@longs.LANCE.ColoState.Edu (Steve Dempsey) (05/03/90)

In article <6830@jarthur.Claremont.EDU>, jseidman@jarthur.Claremont.EDU
(James Seidman) writes:
> I'm working on a program which uses popen() several times to run on an
> ULTRIX workstation.  ...  My problem is that every time a popen()
> occurs, the system grinds to a halt.  What I suspect is happening is that
> popen() calls fork()... 

I've just looked at the source and it is indeed fork().

>  Since the system is ULTRIX I can't
> just pull it out of the BSD source, of course.

It's certainly worth a try.  Before we had Ultrix source, I often
used BSD source to replace uncooperative pieces of Ultrix, both
kernel and user code.  In this case, the diffs between Ultrix3.1
and 4.3BSD (MORE/BSD) versions of /usr/src/sys/lib/libc/gen/popen.c
are minimal.

If that fails, you should be able to roll your own popen() using
pipe(), vfork(), and exec?() to suit your application.


        Steve Dempsey,  Center for Computer Assisted Engineering
  Colorado State University, Fort Collins, CO  80523    +1 303 491 0630
INET: steved@longs.LANCE.ColoState.Edu, dempsey@handel.CS.ColoState.Edu
UUCP: boulder!ccncsu!longs.LANCE.ColoState.Edu!steved, ...!ncar!handel!dempsey

stefan@wheaton.UUCP (Stefan Brandle ) (05/04/90)

In article <6830@jarthur.Claremont.EDU> jseidman@jarthur.Claremont.EDU (James Seidman) writes:
>I'm working on a program which uses popen() several times to run on an
>ULTRIX workstation.  The program's image is very large, occupying almost 50%
>of the computer's core memory.  My problem is that every time a popen()
>occurs, the system grinds to a halt.  What I suspect is happening is that
>popen() calls fork(), fork() tries to copy the entire program image, and
>therefore a lot of swapping has to occur.

vfork might do nicely.  Another possible approach is to have a small stub
program that sets up all necessary pipes, file descriptors, then forks off and
execs the pipe-children and the parent execs the real program you want to run.
That way the program that is getting duplicated would actually be quite small.
This works ok if you have a relatively small number of children and know the
number in advance.  If not, you may need to do it by having a process that acts
as a `daemon' and communicates with your main program by way of sockets or your
favorite IPC method, and gets told by the main program what to popen/fork/exec,
and then sends the results back to the main program.

Of course, if vfork works right, that's an easier solution.

Stefan Brandle
-- 
---------------------------------------------- MA Bell: (708) 260-5019 ---------
Stefan Brandle                  UUCP: ...!{obdient,uunet!tellab5}!wheaton!stefan
Wheaton College			or	stefan@wheaton.UUCP
Wheaton, IL 60187 		"But I never claimed to be sane!"