CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Assistance with fork and multiple child processes

 



mims1979
New User

Jan 10, 2008, 12:43 PM

Post #1 of 6 (612 views)
Assistance with fork and multiple child processes Can't Post

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();
}



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.


KevinR
Veteran


Jan 10, 2008, 1:12 PM

Post #2 of 6 (610 views)
Re: [mims1979] Assistance with fork and multiple child processes [In reply to] Can't Post

apparently you didn't read the exec() function documentation. exec() never returns. Once you call it in your program, thats it, your perl program stops running. You probaly mean to use system() instead of exec(). Perl 5.6 is considered ancient at this time, hopefully you can get upgraded to 5.8.5 or higher, currently perl is at 5.10.
-------------------------------------------------


mims1979
New User

Jan 10, 2008, 1:20 PM

Post #3 of 6 (609 views)
Re: [KevinR] Assistance with fork and multiple child processes [In reply to] Can't Post

Yes, I'd love to upgrade, but its not my server and can't do anything about that. So switch it to system instead of exec. But is that the process to launch off multiple processes and let them all run at the same time? each system() will launch off another child so that they all run at the same time?


mims1979
New User

Jan 11, 2008, 11:15 AM

Post #4 of 6 (606 views)
Re: [mims1979] Assistance with fork and multiple child processes [In reply to] Can't Post

I did a little more research on system vs. exec and it would appear that exec is what I actually do want to use. If I use system then the script actually waits for the processing to complete, which is not what I want. I want it to fork out the processes and print to the browser "Processing now..." I have the forking working correctly now but can't seem to make the browser receive any text until all the child processes are complete. Any Ideas?


KevinR
Veteran


Jan 11, 2008, 11:45 AM

Post #5 of 6 (603 views)
Re: [mims1979] Assistance with fork and multiple child processes [In reply to] Can't Post

As far as I know, once you call an exec() function, your perl script stops running. So you can't call exec() more than once in a perl program. Threads is probably what you need, but I am not sure how well supported they are for perl 5.6, and since I never use Threads I can't really give you any advise other than to read the documentation.
-------------------------------------------------


mims1979
New User

Jan 14, 2008, 6:37 AM

Post #6 of 6 (599 views)
Re: [KevinR] Assistance with fork and multiple child processes [In reply to] Can't Post

Well, maybe its different on 5.6 because I successfully got it working, and so far I'm calling exec 4 times in the script and each one of them fire off and work flawlessly. I had to add "close STDOUT;" before each exec call though so that the browser thinks the script is finished output and everything works as I wanted it to.

Thanks for all you guy's help.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives