From: kirk@ctms.gwinnett.com (R. Kirk Waterhouse)
Newsgroups: comp.unix.wizards,comp.unix.programmer,comp.lang.c,comp.unix.sysv386
Subject: fork() and forget..How?
Summary: fork() and forget..NEVER THINK ABOUT IT AGAIN!
Keywords: fork() wait() getpid() getppid()
Date: 7 Oct 92 19:40:20 GMT
Followup-To: comp.unix.wizards
Organization: Consolidated Traffic Management Services


I am looking for a way to fork() a process and forget about it.

Here is the scenario: 

I have a process that is constantly reading data from another process.
My process has been popen()'ed from this other process, so I have the 5120 byte
buffer from the pipe which allows me to do the necessary processing on the data.
My process is basicly queing up data to send to another process. 

This other process is my problem.

I need to have multiple instantiations of this process running in the backgroundto act as multiple channels. However, I want to send a batch of data to this 
other process, allow it to process this batch and die off when through, while my
current process is still recieving data from the process that is feeding it.

Wow!, did I say that right?  Yea, see picture below.

-----------     P      ----------- fork()---> Channel 3 }  These are not so much
|  Main   | --->I      |   My    | fork()---> Channel 2 }  channels, as they are
| Process |     P ---> | Process | fork()---> Channel 4 }  independtly running
-----------     E      ----------- fork()---> Channel 1 }  processes that die
                                   fork()---> Channel x }  when complete.

I followed the examples in the AT&T Programmers Guide and other books and they 
won't quite work.

   if (fork()) {
      if(child) {
         ...
         process_data_and_die();
         exit(1);
      }
      else
      if (parent) {
         wait()
      }
   }

If I do it basicly like this I only get one instantiation of my child process
because the parent sits there at the wait, losing data from it's input because
this child takes approx. 1-2 minutes to complete.

If I leave out the wait, I get hundreds of instantiations of my child process
which become zombie processes with a parent of (1 or init) until I can issue
a wait().  (When could I issue a wait()?)  At this point I loose data again from
my input process until all of these zombie processes get cleared up. (If I do 
not issue the wait or kill my process, I soon cannot fork() anymore, I guess I 
am exceeding the maxixmum number of processes) 

Any body got any ideas? This is the first time I have used fork(), is there
something basic I am missing?

At this point (until I figure out the fork()'s) I am just running my child 
process in the background and opening up an IPC FIFO between us and having to 
know in advance how many channels I will need so I can open up that many FIFOs.

This works, but there seems to be alot more overhead involved with the FIFOs
than would be with the fork()'s. The fork() would also be alot more flexible
since it would allow as many channels as needed, and I could regulate how many
would run as a maximum rather than having to run x number of background
processes and open x number of FIFOs.

Any insight to this problem will be greatly appreciated!
Thanks (ALOT) in advance,
R. Kirk Waterhouse

P.S.
Just in case this is important, I am running AT&T Sys V rel 3.2.2 on a 386 w/
4 meg of ram, and AT&T's C-compiler.

-- 
 kirk@ctms.gwinnett.com            |  R. Kirk Waterhouse    DoD# 0626    KD4NKD
                                   |     It was one of those nights
 if (opinions != employer)         |     when you turn out the lights  
    must_be(mine);                 |     and everything comes into view - AC/DC
Newsgroups: comp.unix.wizards