#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    38
    Rep Power
    3

    Perl fork and child exit status


    HI, I need some help with something I am trying to complete. I have a script that runs a while(1), yep, like a daemon, so my question is, how do I know is, when it runs OK, it will go to the infinite loop and I have no way of letting the OS know exit(0). So I tought, I will use perl and fork that while(1) on a child process. On that child process, I will try to open a conection to some server. If the conection fails, I do exit(1), however if it is OK, how the parent process will know it was OK? How do I tell the parent process if the exit is 0 when the child process will enter an infinite loop?? There must be a way if the connection was OK, tell parent process, hey it seems I am OK. When it fails it is easier, just exit(1). I am really having a difficult time with this, and I would appreciate some more experienced user than me, to tell me how is the correct way to handle this??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    In general, the parent will receive a status about the child only when the child exits. This also means that if the child does into a sort of daemon mode, the parent will know indirectly because it will not receive informaztion that the child died. f this is not sufficient, you have to establish explicit inter-process communication.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by Laurent_R
    In general, the parent will receive a status about the child only when the child exits. This also means that if the child does into a sort of daemon mode, the parent will know indirectly because it will not receive informaztion that the child died. f this is not sufficient, you have to establish explicit inter-process communication.
    Thank you, Laurent. I will need to do a sleep for lets say 3 secs, and then check for the pid of the child to check if it is running, if not then I would do the exit(1), else exit(0) from the parent. Now, all these questions are because I am trying to put a perl daemon on the Solaris Service Management Facility SMF, but I am having a hard time trying to put the perl daemon to run and the SMF know that it is running OK. Will need to run a couple of tests and follow your suggestion, I believe I can work with that. Regards
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    I don't know what constraints the system you're working under imposes, but it sounds like you're forking too soon. Most resources (such as sockets) opened in the parent are inherited by the child, so you can do a lot of preperation work in the parent (while you still have the opportunity to make an error return) then fork and let the child continue with the verified good configuration. Here's psuedo-code for what I'd implement:

    Code:
    fh = connect_to_server();
    exit(1) unless valid(fh);
    fork()
    
    if( in_parent ) {
       exit(0);
    }
    elsif( in_child ) {
       daemonize();
       daemon_loop(fh);
    }
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    38
    Rep Power
    3
    Originally Posted by OmegaZero
    I don't know what constraints the system you're working under imposes, but it sounds like you're forking too soon. Most resources (such as sockets) opened in the parent are inherited by the child, so you can do a lot of preperation work in the parent (while you still have the opportunity to make an error return) then fork and let the child continue with the verified good configuration. Here's psuedo-code for what I'd implement:

    Code:
    fh = connect_to_server();
    exit(1) unless valid(fh);
    fork()
    
    if( in_parent ) {
       exit(0);
    }
    elsif( in_child ) {
       daemonize();
       daemon_loop(fh);
    }
    Ok, I will give the details, I am using Solaris Service Management Facility, which is a type of application framework, where solaris will monitor your scripts or services. Now, It asks you for 3 methods, start, stop, restart. In Solaris, you will run the command in this way:

    Code:
    svcadm start|stop|restart yourservice
    Each method expects and exit code, so Solaris know if it went OK, or is something went wrong, so, when you do a svcs command, it will show that service with its status, if it is online, maintenance, offline, etc. Now, for the stop, I am OK, I just do a kill of the PID and exit 0, no prob, however, When starting, I need to do a SMPP connection to a server. I use the Net::SMPP, and then will start the connection like this:

    Code:
    $smpp = Net::SMPP->new_receiver($host,system_id => $sysid, password => $pw, system_type => 'SMPP', port => $port,) or die("Can't create server: $!"); #or exit(1);
    After that I will go into a while(1) loop, or while($smpp)

    Now, the thing is when going into the while loop, I have no way of telling Solaris if everything it is OK, and it timeouts waiting for the script to give an exit code. So I imagined, I must use a child process for the while loop, and use the parent to return the exit code to Solaris, now... my question is about "CONTRACTS". CONTRACTS are some type of way the OS will interrelate child processes to a main process, so, I need to test if the child process stops or get killed, the Solaris SMF will know and will change the status to maintenance or offline mode. So, thank very much for your replies.. this is what I am going to do, the parent process will check the connectivity, if it is OK, it will close the connection and launch the child process and will exit(0); I just hope if I kill the child process or if it stops for any reason, it will be part of the contract and the OS will know and the SMF will change the status of the service. I am sorry if this is mixed topic Perl/Solaris ... Regards Laurent and Omega

IMN logo majestic logo threadwatch logo seochat tools logo