Fork issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 3srt
    New Member
    • Jan 2008
    • 2

    Fork issues

    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:

    Code:
    #!/usr/bin/perl
    
    # includes and dependencies....
    
    use strict;
    use warnings;
    
    my $max_clients = 10;
    my $port = 15100;
    
    while(1)
    {
    my $sock = new IO::Socket::INET (
            LocalHost => 'localhost',
            LocalPort => $port,
            Proto => 'tcp',
            Listen => $max_clients,
            Reuse => 1,
                                    );
        die "Could not create socket: $!\n" unless $sock;
    
        my $new_sock = $sock->accept();
    
        while(<$new_sock>)
        {
             # parse information
    
             close($sock);
    
             #send e-mail function here
    
             unless(fork)
             {
             # sleep for given time
             sleep(30);
    
             # perform function
    
             #2nd send e-mail function here
             exit(0); 
    
             }
     }
    }
    Thanks in advance!
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    the "unless(for k)" condition is evidently not being evaluated. Why, I do not know.

    Comment

    Working...