Raymond Wan wrote:

Hi all,

Hello,

I'm trying to fork a process under modperl for a web server, but I've realized that the problem / misunderstanding that I'm having is unrelated to modperl...I get the same problem under Perl. What I want to do is to fork a child process (non-perl program), but also time how long it runs. As I want the "user time" and not the real time, taking

perldoc -f times

the time stamp when it starts and stops is possible, but not ideal. So, I think the only way to do this is to time it from the operating system rather than within the program. This is what I have:

-----
#!/usr/bin/perl
use diagnostics;
use warnings;
use strict;
use POSIX 'setsid';

my $cmd = qq (/usr/bin/time --output=time.txt ls &);

$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$SIG{CHLD} = 'IGNORE';

Your problem appears to be this line.  When you run:

exec '/usr/bin/time --output=time.txt ls &'

The '&' at the end causes the whole line to be run in a shell which probably sets a SIGCHLD handler of its own. If you run it without the '&' at the end then perl runs it directly through execve(2) and /usr/bin/time then inherits perl's ignored SIGCHLD status.

When I tested it and changed that line to:

$SIG{ CHLD } = 'DEFAULT';

Or just remove the line altogether it works fine.


defined (my $kid = fork) or die "Cannot fork: $!\n";
if ($kid) {
 print "in parent whose kid is $kid\n";
}
else {
 print "in child\n";

 setsid () or die "Can't start a new session: $!";

 open STDIN, '/dev/null'   or die "Can't read /dev/null: $!";
 open STDOUT, '>tmp.file' or die "Can't write to tmpfile: $!";
 open STDERR, '>&STDOUT'   or die "Can't write to STDOUT: $!";

 exec $cmd or die "Cannot execute exec: $!";
}
-----

When I run it, this is printed to stdout:

in child
in parent whose kid is 7122

time.txt is empty; and tmp.file has this:

test.pl
time.txt
tmp.file
/usr/bin/time: error waiting for child process: No child processes

I guess I have a misunderstanding of what fork does... Does anyone know how I can get around this? I don't quite mind whether the time is written to time.txt or to tmp.file...either will do... (Removing the "&" and/or replacing "exec" with "system" yields the same result.)



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to