Solved

How to create a parallel  (forked) process  in Perl

Posted on 2011-03-24
9
586 Views
Last Modified: 2012-05-11
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
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 475 total points
ID: 35208935
This should do it...
if (fork == 0) {
   ## your code goes here
   exit;
}

Open in new window

0
 

Author Comment

by:Tolgar
ID: 35208987
So under which condition fork may not be equal to 0?

Can you please explain a little bit how this works?

Thanks,
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35208990
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 35209037
> 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
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 475 total points
ID: 35209104
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
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35210315
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
 

Author Comment

by:Tolgar
ID: 35210548
@ 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
 
LVL 26

Expert Comment

by:wilcoxon
ID: 35211311
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
 

Author Comment

by:Tolgar
ID: 35211440
ok .

Thanks everyone...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question