in reply to Concurrent Processes

What do you mean by 'chokes', hangs, segfaults, etc? And some of the stuff(lots of it apparently, including what 'caller' happens to be) may be relevant to why it's 'choking'.

Also this bit I think is incorrect on its face:

if ($pid[$i]=fork()) { # parent process waitpid($pid[$i], 0); # wait for child close ($wh); ...
After each fork, the parent process is going to wait for the most recently forked child to complete BEFORE the next one is forked. All of your forked processes are going to run SERIALLY not in parallel.

What I think you might want to try is this:

## ## launch children ## for( $i = 0 ; $i < NUMPROCS ; $i++ ) { $pid[$i] = fork() ; next if( $pid[$i] != 0 ) ; # fork next process ## ## child stuff ## exit(0) ; # when finished } ## ## gather children ## foreach( @pid ) { waitpid($_, 0) ; # waits for each child if( $? != SOMETHINGGOOD ) { # bad exit status } }

Replies are listed 'Best First'.
Re: Re: Concurrent Processes
by mcogan1966 (Monk) on Oct 21, 2003 at 18:32 UTC
    I see what you are saying.
    Acutally, I think the first part of what you said may be the cause of the problem, I'm still ending up with this running in SERIAL with the waitpid() inside the for loop.

    But I'm unclear as to how your code can be used to do what I need. The parent needs to be able to see what the child puts out. Since I'm no longer doing the 2-way pipe that was set up, where does the child give it's data back to the parent process? You threw in comments about 'child stuff' and 'gather children', but your design changes the way it all works. Could you be more clear about that?
      I think you should look at the select() call to check for output from your various children.

      May I recommend going to a bookstore and purchasing Stevens' Unix Network Programming? This will have a lot of examples about how to do this sort of thing, coded in very clear C.

      Michael

        Ya know, I was trying to look at select() for that before, but I'm still having some difficulty finding any good examples that can clearly show me how it's used. I've seen a couple, but they mostly cover with use of sockets. I'd rather like to avoid that route if I can.

        Know of any good resources in Perl for that?
      That bit of code was only addressing the launching and gathering of child processes. I can't/won't/shouldn't work out everything for you.
        Contradicting everything in my previous post, I've 'slapped together' a process manager object and posted in the code section ProcessMgr