> -----Original Message----- > From: Alex Read [mailto:[EMAIL PROTECTED]] > Sent: Friday, April 26, 2002 12:01 PM > To: [EMAIL PROTECTED] > Subject: Probably a stupid question > > > Hello, > > OK, I have a nice a simple test case, but I just can't get it > to do what > I want it to. How do I get this so that when I hit submit it comes > straight back with my "hi" message instead of waiting until the sleep > command has finished? I've tried playing around with fork and how I > execute the command, but whatever I do I just can't get it to > go to the > return message without waiting for the child process to finish. Any > help gratefully recieved. > Many thanks > > > Alex. > > > #!/usr/bin/perl > use strict; > > my $pid = fork && exit; #$pid should return 0 if fork is
No. fork() is called once, but returns *twice*; In the parent, $pid is the child process. In the child, $pid is zero. Here you are exiting when $pid returns non-zero, so the parent is immediately exiting, leaving the following code to be run only by the parent. You technically need to check for undef on the return from fork, which will happen if you run out of process table space, for example. This is typically coded as: defined(my $pid = fork) or die "Couldn't fork: $!" > successful. > > if ($pid == 0 ) { > exec "sleep 100" or die "Cannot sleep: $!" ; > } Assuming you check for undef, $pid will always be zero here, since the child is the only process running. But now you call exec(). exec() does not return. So now you have the child sleeping for 100 seconds, but the following code will never run, as far as I can tell. > > print "Content-type: text/html\n\n<html>"; > print "\nhi\n\n</html>\n\n"; > The way to code this to do something like: defined(my $pid = fork) or die "Couldn't fork: $!"; # parent prints message and exits if ($pid) { print "Content-type: text/plain\r\n\r\n"; print "hi\n"; exit; } # child now running, so do your long-running process here ... -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]