sandeep.ses has asked for the wisdom of the Perl Monks concerning the following question:

hello, i am trying to open an application (chimera ) from my perl script . to have this application run seperately i am forking first and then opening it in the application. here is the code that i use
sub{ my $pid = fork(); if ($pid) { `chimera --send test.pdb` } }
whenever i want to view a stucture i call this subroutine. i am able to fork it the first time and view the structure in the application. when i try to view the second time it opens the applications but my perl programs hangs . what i am not able to understand is why is my main program hanging ?? i know for sure my concepts of fork() is wrong . it would be really great if anyone can point me in the rite direction

Replies are listed 'Best First'.
Re: problem with forking
by chromatic (Archbishop) on Jul 05, 2004 at 22:05 UTC

    See fork for more information. If the fork() succeeds, the parent process will receive the pid of the child process. The child process, which I assume you want to launch the other program, will receive zero.

    Also, I expect you want to use exec to launch the program, not backticks. Otherwise, you probably need to call exit in the child process so it won't continue to fork off programs.

    sub launch_chimera { my $pid = fork(); die "Unable to fork\n" unless defined $pid; return if $pid; exec(qw( chimera --send test.pdf )); }
      thanks a lot . i tried using exec it worked fine. Sandeep

        Another way to run applications instead of using exec or fork is to use expect; from the Expect module.
        This is very useful for unruly programs that cause your Perl to hang.
        I have also used open3 instead of fork / exec but am now currently converting open3 to expect. #Even though it only is available for linux it might be useful to you.

        Everything I know is here
        http://search.cpan.org/dist/Expect.pm/

        Some code that uses this

Re: problem with forking
by blue_cowdawg (Monsignor) on Jul 06, 2004 at 15:10 UTC

        to have this application run seperately i am forking first and then opening it in the application

    Perl forking 101:

    #!/usr/bin/perl -w use strict; my $pid=fork(); # there's a fork in the road... if ($pid){ wait(); # Parents are always waiting around for children } else { # This is the child process... # hand waving... do stuff here... exit(0); } # and on our merry way we go.

    There is a very simplified view of how forking works. The data space up until the time of the fork gets inherited by the child. In the child instance, $pid is going to be zero and in the parent process $pid will have the pid of the child process that is running. In the example I give above your parent process will wait until the child process exits (note the very implicit exit call, this prevents nasty things from happening if we fall through the if statement) before continuing execution.

    another mouse trap!

    There are ways of making your life easier when it comes to forking. Check out Proc::Fork and Schedule::Parallel::Fork just to name two.

Re: problem with forking
by mcogan1966 (Monk) on Jul 06, 2004 at 13:13 UTC
    A while back, I too has some issues with forking processes. Here's a link to my issue.

    Forking Stuff.

    In my case, I was having to run that forked process repeatedly. Maybe this can give you some more ideas to work with. Best of luck.

Re: problem with forking
by pearlie (Sexton) on Jul 07, 2004 at 14:53 UTC
    Try using Parallel::ForkManager perl module.