Hi Rob,
Rob Dixon wrote:
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.
Oh, alright then. Since most mail readers thread mails very well, I
tend to not pay enough attention to how I reply... Sorry!
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";
}
Yes, I am running this on a Linux system. So, as I said, this is
running under modperl/mason for a web server. What I want to do is to
start off a running C++ program and have the main process immediately
come back and show something to the user. That is the reason why I have
that line there in the first place. Yes, without it, the child would be
reaped, but the downside is that the main process is (I think) going to
wait for the C++ program to run...and I don't want that. Especially
since in a web server setting, I guess the client will just see that a
web page is trying to load and almost certainly will click "Reload"
uncontrollably... :-)
That is why I want the child process to be detached. Wanting that
causes them to be zombies, so then the problem gets bigger and bigger.
To be honest, I started by searching for "modperl" and "starting a
process" on the web and found some lines of code resembling what I first
posted and have since been modifying that. I haven't taken a long, hard
look at each individual line to see if it is correct or not applicable
for my problem. (No code I have found on the Net has addressed the
issue of timing that process, though.)
Thanks for the source code! I'll play with it; someone I am working
with also proposed something similar just yesterday...obviously, my
double-forking is raising eyebrows, so I should take a good look at what
I am doing.
Actually, the double-forking does work...I got it working a couple of
days ago. But, I'm new to modperl, and I think a fork doesn't copy the
perl interpreter, but a copy of the whole apache process...so, yes, a
single fork is going to be a performance hit, so one is better than 2,
if I can get away with it... Thanks a lot!
Ray
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/