[HRPM] fork and exec

Troy E. Webster twebster at pcs.cnu.edu
Sat Nov 4 10:27:15 CST 2000


Ok guys...I'm having issues here.

I pretty much understand fork and exec and the relationships between
parent and child processes. Or I thought I did.

Basically I need to fork a child process and have the parent process wait
on the child to finish up before continuing on. I think I've got a working
piece of code to do this, but it's not working as expected. Here's the
code (it's short) and then the output. After that is the output that I
think is supposed to happen. They are different.

<snippet>

if (!defined($kidpid = fork()))
{
    #fork returned undef, so failed
    die "cannot fork: $!";
}
elsif ($kidpid == 0)
{
   
    #fork returned 0, so this branch is the child
    
    print "here in child before exec\n";

    # this next command takes a word document and 
    # writes an ANSII text version of it to disk
    # using antiword
    exec("./antiword ./uploads/$ARGV[0] > uploads/text.txt");
       #if exec fails, fall thru to the next statement
    die "can't exec antiword properly: $!";
    exit;
}
else
{
    #fork returned neither 0 nor undef, so this branch is parent
    #wait for kid to finish up file write
    
    print "here before wait.\n";

    waitpid($kippid, 0);

    print "here after wait.\n";
    
    #now its safe to open up text.txt....

    $cnt = chmod(0777, './uploads/text.txt');

    if ($cnt == 0)
    {
	print "unable to chmod the text file.";
	warn "can't chmod the text file";
    }

    open(FH, "./uploads/text.tx |") 
	or print "Unable to open up the text.txt file: $!";
 
    close(FH);   
}



</snippet>

<output>

web4>perl thingy.cgi Cafeteria.doc
here before wait.
here after wait.
here in child before exec
can't chmod the text file at thingy.cgi line 52.
unable to chmod the text file

</output>

<output_I_think_should_be>

web4>perl thingy.cgi Cafeteria.doc
here before wait.
here in child before exec
here after wait.

</output_I_think_should_be>


any ideas?? Am I missing something here?

troy

___________________________________________________________________________
``Beware of bugs in the above code; I have only proved it correct, not
tried it.''  -Don Knuth
---------------------------------------------------------------------------





More information about the Norfolk-pm mailing list