Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

How to create a parallel (forked) process in Perl

Hi,
How can I create a parallel  (forked) process  in Perl?

Let's say I have system call in my Perl code. And I don't want the rest of the code to be interrupted due to a problem in my section of the entire code.

Note : The rest of the code does not rely on my code at all.

Here is my current code:

SOME OTHER CODE IS IN HERE

my $nb_sec = 60;
timeout $nb_secs => sub {
        system("java -jar myfile.java");
  } ;
  if ($@){

    Send e-mail from a1@domain.com to a2@domain.com with a subject of "Logging Timed out" and with a message of "System call interrupted due to time out"

     }
        if ($? == -1) {

           Send email from a1@domain.com to a2@domain.com with a subject line of "Logging Failed" and with a message of "failed to execute: $!\n"

        } elsif ($? & 127) {
             my $signal = ($? & 127);
            my $woCoredump = ($? & 128) ? 'with' : 'without';
            
         Send email from a1@domain.com to a2@domain.com with a subject line of Logging Failed and a message line of "Child died with signal $signal, $woCodedump coredump\n"

        }
    }

SOME OTHER CODE IS IN HERE

Open in new window


Thanks,
0
Tolgar
Asked:
Tolgar
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
wilcoxonCommented:
This should do it...
if (fork == 0) {
   ## your code goes here
   exit;
}

Open in new window

0
 
TolgarAuthor Commented:
So under which condition fork may not be equal to 0?

Can you please explain a little bit how this works?

Thanks,
0
 
point_pleasantCommented:
Here is a simpleexample.  put the process you don't want to wait for in the Child Process section

defined (my $pid = fork) or die "Cannot fork: $!\n";

   if ($pid)
   {
     # Parent Process...

     push(@children, $pid);
     wait();
     my $ret = $?;

     if ($ret)
     {
      warn("Abnormal child exit: $!\n");
      $daemon = undef;
      last;
     }
   }
   else
   {
    # Child Process...
    exit(0);
   }
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
> So under which condition fork may not be equal to 0?
perldoc -f fork
       fork    Does a fork(2) system call to create a new process running the
               same program at the same point.  It returns the child pid to
               the parent process, 0 to the child process, or "undef" if the
               fork is unsuccessful.
0
 
wilcoxonCommented:
As ozo said, fork returns 0 to the child and returns the pid to the parent so the code inside the if is only executed in the child and the parent happily goes on to doing whatever is after the if.
0
 
point_pleasantCommented:
tolgar,

be careful of using the above code.  Not using a wait statement in the parent process can cause unexpected results.  for example the below code with the wait commented out.

if (fork == 0) {
        print "in child\n";
        $actual = sleep(10);
        print "after sleep $actual\n";
        exit;
} else {
        print "in parent\n";
        #$waitis = wait;
        #print "$waitis\n";
}


when run perl forktest.pl the following is the output
in child
in parent
root@172.30.157.19 #  after sleep 10

the parent process has exited, the child is orphaned and completes to seconds later with the parent having know idea of success or failure.  with the wait uncommented the following is the output

in child
in parent  <-------- can't see it here but thete is a 10 second delay between this and the following line
after sleep 10
6630
root@172.30.157.19

0
 
TolgarAuthor Commented:
@ wilcoxon and ozo :

What do you think about this comment?


@ point_pleasant:

If I put wait then does parent wait until child completes the process? If this is the case, then this is not what I really want


Thanks,

0
 
wilcoxonCommented:
He is correct but it very rarely makes a difference in practice.  wait will cause the parent to wait for the child to finish which does allow doing error checking in the parent process.  However, you can always add code in the child process to catch any errors therein.
0
 
TolgarAuthor Commented:
ok .

Thanks everyone...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now