arodri7 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am fairly new to fork processes. I want to fork a process such that the parent process parses through each file I have in a certain directory and gets the required information and then in a child process submit that acquired information from the file into another module so that it will process that in the background. I am able to do it, however, the last child process is always repeated so I end up with two outputs for that parent file that are exactly the same. Here is a snippet of the code I use to fork the process. Let me know if there is an error in the code that makes it repeat the last process.
#Get list of files from request_directory @request_files = <REQUESTS>; #start submitting jobs for the not_yet_run requests while (( $available_machine) && ($request_file_exists){ #this should be the parent process #read request file ($bling, $file, $Tool) = split(/~/, $request_file); open(REQ,"<$home/Input_Files/$request_file"); while (<REQ>){ $line = $_; #process each $line in the file to extract info that I #want } #Once I have extracted the information from the file #I want that to be the input to a child process #fork process to run SubmitToChi $child_id = fork; if ($child_id == 0){ #this is the child process #print STDERR "run $cmd on $request_file on $mach\n"; #run ChiSub using a remote shell (should I use #someth +ing else?) `rsh $mach $cmd $path $file $family $sub_family $seq_size $Tool`; + #these variables are my input to the child process } }
Please Monks, let me know what you think is the problem. I am not familiar with the fork process, so I don't know why the last child process is being repeated twice. Thanks

Replies are listed 'Best First'.
Re: fork processes
by Aristotle (Chancellor) on Jun 05, 2002 at 10:03 UTC
    I don't know if it's just an error in pasting, but while (( $available_machine) && ($request_file_exists){is not valid Perl - you forgot a closing round bracket. I'm not entirely sure there there's no circumstance under which this might mistakenly parse. Assuming that's not the problem, where do $available_machine and $request_file_exists get changed? I don't see them altered in the loop. If they're being changed in a part of the code you left out, you may have a fencepost error (ie your loop conditions do not get changed as early as you're expecting).

    Makeshifts last the longest.

      Sorry Screamer, I left the part where $available machines and $request_file_exists get updated. Before the code I had written, I check to see how many machines are available and put them in an array, as well as the $request_file_exists variable and as long as any of the two arrays are not empty (I left out the part where I use pop(@arrays) to update the variables) I keep going through the loop. But when I get to the last machine, it repeats its process twice. I update those variables in the "while" line. The code that I use to change the $variables is:
      #start submitting jobs for the not_yet_run requests while (( $usable_machine = pop(@$usable) ) && ( $request_file = pop(@a +ll_requests) )){ #use the code I had before }
      Let me know what ya think Thanks
        I figured out why it was repeating the previous fork porcesses each time it looped. I had not closed the child process and it was creating zombies (I guess that is what is called). Thanks for the help
Re: fork processes
by Aristotle (Chancellor) on Jun 05, 2002 at 16:46 UTC

    Ok, after your clarifications, it doesn't seem to be a fencepost error.

    And since I've not really used the fork()s yet *grin*, I was a bit slow in spotting what I think is your reason; in the if ($child_id == 0) block, there's no exit(); so you fall through the end of the block and, the way I see it, execute the loop once more from within the child.

    Oh and, why're you using backticks in void context? I think system() would be a better choice there. :-)
    ____________

    Makeshifts last the longest.