[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: fork
From:       "Tom Phoenix" <tom () stonehenge ! com>
Date:       2007-10-29 17:16:07
Message-ID: 31086b240710291016u695391d4rc58d18f03193d87b () mail ! gmail ! com
[Download RAW message or body]

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

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic