in reply to Split process->check ->and run Parallel

I'm not really clear on what you want to do. The basic usage of fork is as follows:

if (my $child = fork()) { # in parent while (! -f "output.txt") { print "Waiting for output.txt\n"; sleep 10; }; print "Found output.txt, continuing\n"; } else { # In child, just run p2 exec 'p2'; # exit }; # do more stuff print "Waiting for child to finish\n"; wait $child;

Replies are listed 'Best First'.
Re^2: Split process->check ->and run Parallel
by matrixmadhan (Beadle) on Dec 24, 2008 at 05:54 UTC
    checking for the existence of file 'output.txt' independently by the parent process may not be the right idea and there is no guarantee that it is generated by the child process.

    There is a possibility that once p1 forks p2 and execs that; p2 can be signaled to terminate and any other process p3 can create a dummy file 'output.txt'; in this case, p1 will assume that everything ran well and output.txt is available.

    Also, there is one more problem with this approach - lets say output.txt is properly generated by child process p2 and 'if its in the process of being generated' ( file size being 1 GB ) - the moment parent process 'p1' sees the file it will assume its a successful completion and continue with its work and there is no guarantee that p2 will run to completion in creating the full file of size 1 GB ( this is just an example )

    so, its better that the child process signal the parent process after completion of its work or chunk of its work as per the requirements.