----- Original Message -----
From: "Paul D. Kraus" <[EMAIL PROTECTED]>
Date: Monday, May 9, 2005 10:18 am
Subject: Fork - Process ID

> Sceniro.
> I have many cron jobs that run perl scripts. These perl scripts launch
> TbredBasic Applications. The problem I keep finding these processes
> hung but I have noway of finding out which program is actually causing
> the problem. When the tbredbasic application launches the processes
> name is always BASIC.
> 
> I wanted to write a program that would write out the process id and
> the perl program that spawned it to a file then destroy that file when
> the program fails. This way I can just look at the "log" and see that
> program X is what keeps hanging.
> 
> So I have been experminting with the fork command but I am 
> confused as
> to the out put I get. Below is the code and its output follows. First
> please explain why the pid prints twice and second let me know if
> there is a better way to achive what I am trying to do.
Hi Paul,
The reason your code prints pid twice, is in the placement of your print 
statment. Since you placed your print code after your fork statment, both 
parent and child read that statment. Inside that block Parent will see pid of ' 
0 ' for child, which is normaly a condition used to split the two apart. You 
can use <C> getpid, to get the read PID of process. Your code should look 
something like this:

#!/bin/perl
use strict;
use warnings;
$|=1;

defined( my $pid = fork ) or die "Cannot fork: $!";

if( $pid ) {
print "I am the parent with pid: ",getpid(),"\n";

}
else{
print "I am the child with pid: ",getpid(),"\n";
print STDERR "Spawning off Basic: ",getpid(),"\n";
#  exec "date" or die "cannot exec date: $!"; # here the error will be in $@, 
needs to be further sifted

}

HTH,
Mark G.  


> 
> TIA
> Paul
> 
> #!/bin/perl
> 
> use strict;
> use warnings;
> 
> defined( my $pid = fork ) or die "Cannot fork: $!";
> print "pid:$pid\n";
> unless( $pid ) {
>  exec "date";
>  die "cannot exec date: $!";
> }
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
> 


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


Reply via email to