Monks,

I am writing a CGI script that creates a text file on the server and writes its PID to it. Then the script uploads a file to a server. I use a fork command and write the text file in one process and upload in the other process. The only problem is the text file isn't showing up until the upload finishes. I need the text file to show up well before the file finishes uploading. I have a feeling it's related to what I read on the perldoc site: "Note that if your forked child inherits system file descriptors like STDIN and STDOUT that are actually connected by a pipe or socket, even if you exit, then the remote server (such as, say, a CGI script or a backgrounded job launched from a remote shell) won't think you're done. You should reopen those to /dev/null if it's any issue."

I am using an open command for both the text file writing and file uploading, but the command is being called on two different files and the commands are called after the fork has been made, so I didn't know if they still shared file descriptors. I am still new to perl, so I dont know if STDIN and STDOUT are the same as using open() and I dont really understand what opening to dev/null looks like in code. I have tested this on an apache server running on windows and a lighttpd server running on cygwin and it happens in both cases. So how do I get the writing to text file to finish (and appear on the server) before the upload finishes??

my $fork_pid = fork(); if ($fork_pid == 0) #If its the chi +ld process { write_pid(); exit(0); } elsif ($fork_pid) #If its the paren +t process { wait(); if ($limit_file_size == 1) { checkFilesize(); } untaintInput(); uploadFile(); output(); dump_pid(); } else { die "Couldn't fork."; } sub uploadFile { my $upload_filehandle = $query->upload($form_input_name); + #grab handle of temporary file open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!"; + #open file for write or report errors binmode UPLOADFILE; #write the file +in binary mode while ( <$upload_filehandle>) { print UPLOADFILE; #write actual fi +le from the temporary file } close UPLOADFILE; } sub output { #Output page print $query->header(); print "{success: true, data: 'Your file was uploaded successfully!' +}"; } sub write_pid { open(MYID, ">" . $upload_dir . '/' . 'current_upload_pid.txt'); print MYID $$; close(MYID); }

In reply to Fork child process always finishes with parent by Rogue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.