For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2007 > fork









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fork
Ryan Dillinger

2007-10-29, 8:00 am


Hello,
I have this script here:

#!/usr/bin/perluse warnings;use strict;

print "PID=$$\n";
my $child = fork();die "Can't fork: $!" unless defined $child;
if ($child > 0) { # parent process print "Parent process: PID=$$, child=$child\n";} else { # child process my $ppid = getppid(); print "Child process: PID=$$, parent=$ppid\n";}

after I run it I get:
PID=3472
The getppid function is unimplemented at...
The getppid function is unimplemented at...
Can someone explain what is wrong here?
Thanks

________________________________________
_________________________
Boo!_Scare away worms, viruses and so much more! Try Windows Live OneCare!
http://onecare.live.com/standard/en...=wl_hotmailnews
Jeff Pang

2007-10-29, 7:00 pm

On 10/29/07, Ryan Dillinger <puppetmyst@hotmail.com> wrote:
>
> #!/usr/bin/perluse warnings;use strict;
>
> print "PID=$$\n";
> my $child = fork();die "Can't fork: $!" unless defined $child;
> if ($child > 0) { # parent process print "Parent process: PID=$$, child=$child\n";} else { # child process my $ppid = getppid(); print "Child process: PID=$$, parent=$ppid\n";}
>


Hi,

I think you want to see the parent id and child id when forking.
The modification version of your code is below,

#!/usr/bin/perl
use warnings;
use strict;

my $pid = $$;
print "process id before forking is ",$pid,"\n";

#print "PID=$$\n"; # don use something like this,what is it?

my $child = fork();
die "Can't fork: $!" unless defined $child;

if ($child > 0) { # parent process
print "parent id is ",$$,"\n";

}else { # child process
my $ppid = getppid();
print "Child id is $$, parent id is $ppid\n";
}

__END__

HTH.
Tom Phoenix

2007-10-29, 7:00 pm

On 10/29/07, Ryan Dillinger <puppetmyst@hotmail.com> wrote:

> #!/usr/bin/perluse warnings;use strict;
>
> print "PID=$$\n";
> my $child = fork();die "Can't fork: $!" unless defined $child;
> if ($child > 0) { # parent process print "Parent process: PID=$$, child=$child\n";} else { # child process my $ppid = getppid(); print "Child process: PID=$$, parent=$ppid\n";}


Yoicks! I don't know what happened to your program's formatting, so I
tried to untangle it:

#!/usr/bin/perl

use warnings;
use strict;

print "PID=$$\n";
my $child = fork();
die "Can't fork: $!" unless defined $child;

if ($child > 0) { # parent process
print "Parent process: PID=$$, child=$child\n";
} else { # child process
my $ppid = getppid();
print "Child process: PID=$$, parent=$ppid\n";
}

> after I run it I get:
> PID=3472
> The getppid function is unimplemented at...
> The getppid function is unimplemented at...
> Can someone explain what is wrong here?


It looks as if your perl executable doesn't understand getppid, which
surprises me because I had thought that getppid was implemented
everywhere that fork is. Seeing the message twice, but no error
message about the fork, implies that the fork succeeded, but both
processes (?!) got a return value of zero. (Or a negative number. Hm.
Does anybody's fork return negative PIDs? Why have you changed part of
the error message to "..."? Why aren't you showing us your program's
real output? Were there other error messages in the output? Is this
perhaps the output from a different version of your program?)

If your system does implement getppid (check your docs), that means
that your perl binary should be recompiled to use it. If your system
doesn't support getppid, which OS is it? Maybe people who use that OS
have a workaround.

In this case, because you're using fork, there is a simple workaround:
Save the PID before forking. But that doesn't obviate the need for
getppid in general.

For what it's worth, your code (after reformatting) works as expected
for me, on Mac OS X (Darwin) perl 5.8.6:

PID=1715
Child process: PID=1716, parent=1715
Parent process: PID=1715, child=1716

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com