1,105,214 Community Members

Perl separate process

Member Avatar
neoanomally
Newbie Poster
13 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all. I have a process that checks the last time a file was updated. If the data is over 3 minutes a separate perl script is started. The problem is that second script takes 5-10 seconds to run. Is there a way to start the second script, but continue and finish the current script?

..... CODE .............

if ($time_difference > 180) {
    system("perl create_cache.pl");
}

...... MORE CODE .......

Thanks,

Member Avatar
kordaff
Newbie Poster
23 posts since Jun 2005
Reputation Points: 1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Since I just answered another question similar to this, I'd suggest using fork() depending on if it works on your OS or not (works fine on Linux/Unix and for me just now on Win7)

Change:

if ($time_difference > 180) {
system("perl create_cache.pl");
}

to:

if ($time_difference > 180) {
my $pid=fork();
if (undef $pid){die "fork failed: $!\n";}
if (!pid)
  {
  # May want to close any open sockets or filehandles
  # here in the child, before running the system 
  # command to avoid them hanging if the parent 
  # closes them before the system call completes in 
  # the child process.
  system("perl create_cache.pl");
  exit; # child exits
  }
else
  {
  # parent continues on...
  }
}
Member Avatar
neoanomally
Newbie Poster
13 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much. I've been looking specifically how to use fork(). I read through some documentation but wasn't sure how to actually use it.

In line 4, what is the if statement checking exactly? Because you are setting the $pid =fork()?

Thanks!

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

The $pid is the process ID created by the fork() if it is successfully created. The if(!pid) should be if($pid). I think it is just a typo. You should always use strict when you write Perl script.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article