in reply to Re: creating a fork process
in thread creating a fork process

sorry, i did read the manual before i asked my question but i dont seem to understand, i do try but im not that smart. i used your code in conjunction with what im doing...
my $pid = fork; if ($pid) { # give thank u message. } elsif (defined $pid) { # close STDIN # close STDOUT # send emails. # exit. } else { # Error message. }
but im not convinced a fork was created, because the browser seems to still be loading from the perl script the whole time while the emails are being sent and then it times out after about 5 minutes, i have closed both STDIN and STDOUT yet the perl script still seems to be communicating with the browser?

Replies are listed 'Best First'.
Re: Re: Re: creating a fork process
by ColtsFoot (Chaplain) on Feb 10, 2002 at 11:48 UTC
    This seems to word for me. Replace your "mail code" where I exec ('sleep 60')
    #!/usr/bin/perl -w use strict; use CGI; use Errno qw(EAGAIN); my $pid; my $page = new CGI; print $page->header(), $page->start_html(); FORK: { if ($pid = fork()) { print <<HTML; <CENTER> In the parent process<BR> HTML print $page->end_html(); exit; } elsif (defined $pid) { print <<HTML; <CENTER> In the child process<BR> HTML print $page->end_html(); close STDIN; close STDOUT; close STDERR; exec ('sleep 60'); exit; } elsif ($! == EAGAIN) { print qq(Re-doing fork\n); sleep 5; redo FORK; } else { die qq(Can't fork: $!'n); } }
    Hope this is of more help than the first posters comment. We all had to start once
      ok thanks for the code, i appreciate it! i have a couple of things...the last line which says

      die qq(Can't fork: $!'n); is the 'n suppose to be \n?

      i did run your code in conjunction with what im doing but i still had the same problem, the browser was still communicating with the perl script the whole time the emails were being sent, almost as if the fork wasnt actually sending the emails and the perl script was? i did overcome this by placing close STDIN, STDOUT, STDERR all inside the parent process, that completely cut the parent process from from everything and seems to have done the trick :) i suspect it may cause some other problem im unaware of?

      now that it seesm ive created a fork is there anyway to cancel a fork already in process?

      thanks rich

Re: Re: Re: creating a fork process
by ehdonhon (Curate) on Feb 12, 2002 at 10:42 UTC

    You might want to take a look at Proc::Daemon. I think the init() function might be a better approach then the one you are taking with closing your filehandles.