fork returns twice, once in the parent and once in the child, with zero in the child and the child's PID in the parent. Both processes continue running at the same time (or as close to the same time as your hardware and OS allow). To do what you're describing, normally the CGI script will fork a child process; the child will often fork again, to fully dissociate itself from the parent process, close any open file descriptors, then do its thing in the background, perhaps with exec. In the meantime, the parent is finishing up sending output to the client, then exiting or accepting another request.

An easy way to do what you want without using fork and exec is to do system('some_cmd &').

Update: Thanks, wirrwarr, I didn't realize that using an ampersand to start a process in the background was nonportable. Apparently the Windows equivalent is system(1,'some_cmd'), so that might be worth a try.


In reply to Re: explain my fork by sgifford
in thread explain my fork by qbxk

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.