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

studious monks, why does this code work? it does what I want, but doesn't exactly look like it should. the root of the problem i think is that i don't really *get* fork()
#!/usr/bin/perl use warnings; use strict; $|++; # running in Apache 1.3.34 print "Content-Type: text/html\n\n"; print "OK"; fork(); close STDOUT; qx/ some_cmd /;
What this apparently does, which I want it to do, is print OK and then execute some_cmd, and the browser recieves the string "OK" and a closed HTTP connection BEFORE (or AS) that qx occurs. I want those 2 events to be as close as possible as I can get it, connection closure and qx/ some_cmd / - but why

Update: I guess I wasn't very clear on what I was asking, a lot of people are telling me that fork will make some_cmd execute twice, which on the command line is true. But when run by apache, close STDOUT apparently does something magic, some_cmd is executed once. I'd expect it to execute either 0 times or twice, but once? wth?

It's not what you look like, when you're doin' what you’re doin'.
It's what you’re doin' when you’re doin' what you look like you’re doin'!
     - Charles Wright & the Watts 103rd Street Rhythm Band, Express yourself

Replies are listed 'Best First'.
Re: explain my code
by Joost (Canon) on Jun 22, 2006 at 19:10 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: explain my fork
by sgifford (Prior) on Jun 23, 2006 at 01:30 UTC
    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.

      An easy way to do what you want without using fork and exec is to do system('some_cmd &')
      Actually, this only works when you use a shell that understands the "&". That means, it doesn't work for most Windows users.


      daniel.