Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

assistance needed with fork and multiple child processes

mims1979mims1979 Posts: 4Member
First of all, I'm on a unix box, running apache with perl 5.6.1.

I've been banging my head for three days trying to figure this out and can't manage to. What I have is a cgi that basically asks for usernames, devices names, passwords, etc and then passes those items to other pl files that actually do a lot of ssh connections to pull data from remote routers and switches. This is somewhat time consuming and if I just try to do it straight from the script it always times out and never finishes. So I heard a rumor that I could fork some child processes, the browser would just tell the users "Ok, its processing, it'll be done soon" and be done with it, close the browser if they want or whatever because the other scripts produce no output, they actually email the results to the user after it has been processed. But I can't seem to figure out how to fork 7-8 processes at the same time. My code is similar to this:

[code]if (defined(param('subbed'))){
$pid = fork();
if(! $pid){
exec ("./gw1.cgi userid=" . param('userid') . " gw1=" . param('gw1'));
exec ("./gw2.pl userid=" . param('userid') . " gw2=" . param('gw2') . " pswd=" . param('pswd') . " enable=" . param('enable'));
}
} else {
PrintIndex();
}[/code]

When I launch off the page in browser, input the required data in the form and hit submit it just sits at a white page waiting until the script is completely done, which 9 times out of 10 times out. When I try to run it straight off the server command line through ssh it seems to work but doesn't launch off the second exec command. I only have two exec commands right now but when all is said and done I should have 6-7 different scripts that need to be launched off.

Can someone please help me with this as I just don't know how to proceed, if forking isn't my best option, let me know another, but I'm just stuck in a rut and can't get out.

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    A couple of things.

    First, if you want to do background processing without leaving the browser waiting (that is, just return a message saying "your request was submitted and we are processing it"), you need to tell the webserver there is no more output coming. That is handled with a:

    close STDOUT;

    After you've sent all the output you care to to the web browser.

    Second, the code looks wrong. You're only forking one process so far as I can see - to fork more, you need to call fork a few times. When fork returns, you have two processes. It returns 0 to the child process, and the ID of the new process to the parent (which you have taken advantage of in your code - that looks correct).

    However, exec (if I remember correctly) replaces the current process with the one you invoke. So if I'm right about that, the second call to exec will never actually be reached.

    Hope this helps a little,

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • mims1979mims1979 Posts: 4Member
    Thanks, I was actually able to finally get some hints and came up with:

    [code]if (defined(param('subbed'))){
    # child1
    $pid = fork();
    if (not defined $pid) {
    die "Couldn't fork"
    }
    elsif ($pid == 0) {
    exec "perl gw1.pl " . param('userid') . " " . param('gw1');
    exit(0) #just to be sure
    }

    # child2
    $pid = fork();
    if (not defined $pid) {
    die "Couldn't fork"
    }
    elsif ($pid == 0) {
    exec ("perl gw2.pl " . param('userid') . " " . param('gw2') . " " . param('pswd') . " " . param('enable'));
    exit(0)
    }
    } else {
    PrintIndex();
    }[/code]

    Both pl files launch off and process at the same time so that is working great. But I have the problem you mentioned about it just waiting in the browser. I'll try closing out the STDOUT handle and hopefully that will resolve that problem.

    Exec seems to work correctly for me for some reason. During my research of system vs. exec it appears like system causes the script to wait until that system command is complete which I don't want to do which is why I chose to use exec.


    Thanks,
    J
  • mims1979mims1979 Posts: 4Member
    Perfect, just added close STDOUT; right before lauching off exec and it works like a charm. Thanks for all the help!

    J
Sign In or Register to comment.