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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Spawning Child Process in Multiple Environments


User / Moderator

Aug 20, 2002, 8:12 AM

Post #1 of 6 (3721 views)
Spawning Child Process in Multiple Environments Can't Post

I have a web based application that does some extensive searches and data
manipulation that can take a long time to process. Most web servers timeout if
I try to complete the whole process in one chunk.

What I would like to do is pass the search criteria to the CGI program from the
web based form and then do the following:

If the process is simple (single record search or changing user preferences only)

The application will change the preferences or start search program that
creates a results file for the single record search. The search program
will be the same program that is used for multiple record searches. The
main program will sleep for 10 seconds to allow the time for a single record search to complete. If the file is created in that ten seconds, the main program will immediately post the results. Otherwise, the main program will display a 'Search in Progress' message and recall the main program after 6 seconds. If the file is done, It will display the results. Otherwise, the process repeats until the file is done or more than $x number of minutes have elapsed. If $x minutes is passed, an message will be displayed to contact the system admin to check for errors.

If the process is an intensive search

The application will behave exactly as above, but will never have the
processing complete after the initial 10 second sleep.

The problem is this. If I use exec() or system() to call the program that will
do the search, the main application still waits to display output until the called
child process completes.

What I need is complete disassociation of the called process. I need to start
the search from the main program and display whatever output is required without
waiting for that search to complete.

Does anyone have an idea how to do this? I really need help getting this done.
I have to demo this program to it's users tomorrow afternoon. Any assistance
or contribution of ideas would be greatly appreciated. Please post any
suggestions that you think could viably solve the problem, no matter how far
fetched you think they may be. I am DESPERATE!!!!! Crazy

Thanks In Advance,

print unpack"u*",pack "h*",$a,"\n\n";

(This post was edited by fashimpaur on Aug 20, 2002, 8:14 AM)


Aug 20, 2002, 11:06 PM

Post #2 of 6 (3713 views)
Re: [fashimpaur] Spawning Child Process in Multiple Environments [In reply to] Can't Post

just do a open( FH, 'subproc|' ) and then do select loop on that
handle and output whatever you read to the web browser. you can pass the query arguments on the command line to the process or even echo them in the shell like:

open( FH, "echo @args | myproc |" ) || die "flaming death $!"

the select loop can be done with IO::Select which can poll and sleep for you. the rest i leave as an exercise to you.

there are many other ways to do this as well.


User / Moderator

Aug 21, 2002, 4:20 AM

Post #3 of 6 (3711 views)
Re: [uri] Spawning Child Process in Multiple Environments [In reply to] Can't Post

just do a open( FH, 'subproc|' ) ...

Does this let the child process go on in it's own task and allow the main program
to display something to the user and exit? If not, this will be a problem. The
long process has to be started and have no ties back to the main program that
will keep the main program from finishing it's current state handling.

More suggestions are welcome.

Thanks, Uri

print unpack"u*",pack "h*",$a,"\n\n";

(This post was edited by fashimpaur on Aug 21, 2002, 4:20 AM)


Aug 21, 2002, 10:30 AM

Post #4 of 6 (3707 views)
Re: [fashimpaur] Spawning Child Process in Multiple Environments [In reply to] Can't Post

i am unclear about your real goal. if the child process is forked and the main one can print results to the user and exit, how do the results of the forked process ever get sent to the user?

this is assuming a web server/cgi environment but my question is valid for any client/server setup.


User / Moderator

Aug 21, 2002, 10:55 AM

Post #5 of 6 (3706 views)
Re: [uri] Spawning Child Process in Multiple Environments [In reply to] Can't Post


That is the problem with trying to keep the gory details out of the
post. This is what happens:


User requests search by filling out web based form.
Main CGI processes/validates request.
If no errors:
Prepares HTML to state search in progress
Puts hidden variable in HTML form for timeout if process takes
too long
Sends HTML to User
Forks after HTML output to start search.
Form auto submits every 6 seconds to see if search is done
or process is taking longer than timeout setting in config

Forked Search does this with passed parameters:
Queries LDAP server
Finds all Records matching search criteria
Parses each entry's x509 certificate entry (.cer format)
Evaluates if certificate will expire in month specified
in search criteria
Writes a .csv file on success
Any fatal error (can't open file, etc.) is written to
error file.

When Main CGI finds the existence of either the .csv or error
files, it processes them for display back to the browser.

Process is finished.

Now, I could get into more detail that there are cookies involved
and the different search criteria and user options available, but
this is the main idea of the program. The problem is the search
to LDAP is not so long as is the parsing and handling of each 785
byte certificate for determination of it's expiration. This is
especially true when a generic date search of all Organization Numbers known to have been issued certificates for a given month
in the next year (i.e. any month from Aug 2002 through
Aug 2003) is requested. There are several months where there are
more than 700 certificates that will expire.

I hope this kind of explains the problem better. I have been
pointed to a potential solution that I am trying now. I have not
been able to test it's viability yet. If it works I will post the solution here. If not, I will be looking for more solutions.

Thanks in advance,

print unpack"u*",pack "h*",$a,"\n\n";

User / Moderator

Sep 5, 2002, 6:07 AM

Post #6 of 6 (3586 views)
Re: [fashimpaur] Spawning Child Process in Multiple Environments [In reply to] Can't Post

Hey all,

For the sake of documenting solutions, I wanted to let you know how to do this
process. Please read my previous post for more information about what is being
done here and how I am getting the results from the forked process.

Here was my solution:

# main thread would execute

# Following is some pseudo code ...
if (search is not in progress){
get search criteria;
print 'waiting' html with flag set to indicate search has started
startSearch(); # once this runs, new thread is spawned and this one exits 12 lines down

else {
if (process output does not exist){
redisplay some waiting html to user
that auto submits form again to check after 6 second wait

else {
process search results based on process output


startSearch {
local $SIG{CHLD} = 'IGNORE';
open( STDIN, "</dev/null");
open( STDOUT, ">>/dev/null");
open( STDERR, ">>../logs/daemon_logfile");

fork and exit; # main thread stops here and new thread resumes from this point

my @args = ("perl", "");
exec @args or print STDERR "[ ".localtime()." ] PID: $$ Can't perform search. Error: $!\n";


Note that there is a lot of missing code to explain all the logic conditions handled
and what to do if the application is running the search too long ( a timeout built
in). But, this is for the general informational use of anyone trying to fork that
process. I did not think fork worked on Windows platform, but found that it is
somewhat functional in the newer releases. This worked with no problems for me
to develop the application but since the final platform would be a UNIX box, I
knew that it should work there completely as expected.

If anyone is trying to do something like this and they have problems doing it,
I would be glad to assist you. This is the second most unusual thing I have ever
done with perl. The first was to parse x509 digital certificates. Which, if you
have the desire to discuss those, I can also help there too.

Thanks to all who helped,

print unpack"u*",pack "h*",$a,"\n\n";


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

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