Raymond Wan wrote:
> Rob Dixon wrote:
>>
>> What do you need to accomplish that something as simple as the code below 
>> won't do?
>>
>> use strict;
>> use warnings;
>>
>> my $kid = fork;
>>
>> if ($kid) {
>>   print "in parent whose kid is $kid\n";
>> }
>> elsif ($kid == 0) {
>>   print "In child\n";
>>   my ($usertime) = times;
>>   print "$usertime seconds\n";
>> }
>> else {
>>   die "Cannot fork: $!\n";
>> }
> 
> Hmmmm, true -- I may be adding and adding code unnecessarily... 
> 
> What the forked process does is run a C++ program and it is that program 
> that needs to be timed.  Would the code below accomplish that?  I mean, 
> having "times" in the Perl script that calls that C++ program will give 
> the user time of that (spawned) Perl process.  But that is a close 
> enough estimation of the C++ program's user time provided all I do is 
> run that program?
> 
> Also, this is within modperl/mason and I would like the parent process 
> to be completely detached from the child (so that a web page can be 
> shown) and not care when the child completes...

Please bottom-post your replies to this list, so that extended threads can
remain comprehensible. Thanks.

I have tried to do some experimentation with calls to fork and times, but I
suspect I am failing because they are not implemented fully on my Windows
systems. But I suggest that you do the same yourself, and also remember that a
call to times returns four values including the user time for both the current
process and its children.

I also believe there is no need to fully detach your child process, as setting

  $SIG{CHLD} = 'IGNORE';

should cause the child processes to be reaped automatically and the parent
process need have nothing more to do with them. Presumably their mere existence
as child processes is of no concern?

So I suggest you experiment with something like the slight modification below.
As stated this will not work for me but I believe that is because of the
imperfect emulation of fork on a Windows platform.

HTH,

Rob



use strict;
use warnings;

$|++; # autoflush

$SIG{CHLD} = 'IGNORE';

my $kid = fork;

if ($kid) {
  print "in parent whose kid is $kid\n";
  sleep 10;
  my $running = kill(0, $kid);
  print "Child process ", $running ? "still running\n" : "terminated\n";
}
elsif ($kid == 0) {

  print "In child\n";

  my ($user, $system, $cuser, $csystem);

  ($user, $system, $cuser, $csystem) = times;
  printf "user: %f cuser: %f\n", $user, $cuser;

  system('myprogram') == 0 or die "myprog failed: $?";

  ($user, $system, $cuser, $csystem) = times;
  printf "user: %f cuser: %f\n", $user, $cuser;

  exit;
}
else {
  die "Cannot fork: $!\n";
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to