By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 2,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

Fork issues

P: 2
Hello,

I created a simple 'server' script that will accept input from a cgi script. The server will handle multiple connections on the same port, also. In a nut shell, this 'server' script will take in variables, and fork off a process to sleep for a given amount of time, and then send the user an email (and perform other functions).

Here is my problem: I receive a confirmation e-mail once the connection has been made, but i do not receive the other e-mail (which is supposed to be sent from the forked process once the sleep() is up). I receive one confirmation e-mail per client, but I will never receive the second e-mail. I don't know why the forked process is not working as I think it should?

Also, the problem ONLY happens when I daemonize the process. If i simply run the script inside a terminal 'perl server.pl', it will work perfectly fine and I will get all the desired results. However, when I daemonize it by 'perl server.pl &', the functions within the fork, including the second e-mail, will not be executed.

Shouldn't I be able to daemonize it and get the same results?

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. # includes and dependencies....
  4.  
  5. use strict;
  6. use warnings;
  7.  
  8. my $max_clients = 10;
  9. my $port = 15100;
  10.  
  11. while(1)
  12. {
  13. my $sock = new IO::Socket::INET (
  14.         LocalHost => 'localhost',
  15.         LocalPort => $port,
  16.         Proto => 'tcp',
  17.         Listen => $max_clients,
  18.         Reuse => 1,
  19.                                 );
  20.     die "Could not create socket: $!\n" unless $sock;
  21.  
  22.     my $new_sock = $sock->accept();
  23.  
  24.     while(<$new_sock>)
  25.     {
  26.          # parse information
  27.  
  28.          close($sock);
  29.  
  30.          #send e-mail function here
  31.  
  32.          unless(fork)
  33.          {
  34.          # sleep for given time
  35.          sleep(30);
  36.  
  37.          # perform function
  38.  
  39.          #2nd send e-mail function here
  40.          exit(0); 
  41.  
  42.          }
  43.  }
  44. }
Thanks in advance!
Feb 12 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
the "unless(fork)" condition is evidently not being evaluated. Why, I do not know.
Feb 13 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.