xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

Hi gurus,
I'm new to perl. and now ,I'm learning network programming with perl.This is a example code"
#!/usr/bin/perl use strict; use warnings; print "pid=$$\n"; my $child= fork(); die "Can't fork: $!" unless defined $child; if ($child > 0) { #parent process print "Parent process:Pid=$$,parentid=$child\n"; } else { # child process my $ppid = getppid(); # 14 line print "Child process: PID=$$,parent=$ppid\n"; } __OUTPUT__ pid=1588 The getppid function is unimplemented at fork_test.pl line 14. The getppid function is unimplemented at fork_test.pl line 14.
I realized that my OS is win and value of $child is negative. So I change if ($child>0) to if ($child != 0),but error message is still there!perlfunc and Supper research don't give me any useful clue.

What should I do? Any advice would be really appreciated!





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: getppid is broken in Win?
by quester (Vicar) on Jan 18, 2007 at 08:43 UTC
    The reason getppid isn't usually implemented under Windows is that it is trying to find what process id the operating system thinks is the parent of your process. Windows doesn't have a parent-child relationship between processes, so there is no real meaning to the result of getppid.

    There is a rather technical discussion of finding the process that started the current process here but note that Window's conception of the process that started a child process does not change when the parent process dies. In Unix systems, child processes whose parent processes die become children of process id 1, the "init" process. In Windows, they don't.

    What you probably need to do is to back up a step and think about what you are going to use the parent process id for. You may be able to just save the value of $$ before the fork. The child process can then use the saved id to find its parent.

    By the way, in the message "Parent process..." you should put child=$child. Putting parentid=$child is confusing.

    Try this:

    #!/usr/bin/perl use strict; use warnings; my $parent_pid=$$; 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 print "Child process: Pid=$$,parent=$parent_pid\n"; }
Re: getppid is broken in Win?
by BrowserUk (Patriarch) on Jan 18, 2007 at 13:45 UTC

    As questor has pointed out, getppid() is not implemented on Win32 because it is one (of many) built-ins in Perl that display Perl's origins on unix and unix-like systems that do not translate directly or easily onto Win32.

    It is usually possible on modern (NT 5 and later) to obtain the information, parent process id, that you are seeking, but it would not help you in this case as when using fork on Win32, as there is only one process.

    This is because fork on Win32 is only an emulation that uses a thread to simulate that *nix concept. Therefore, there is only one process containing two threads, with the "process ID" of the child actually being it's negated thread id. This, and several other fundamental differences in the concepts and implementation between Win32 and teh unix-like systems upon which Perl evolved point to a much deeper problem in your stated goal.

    Attempting to learn "network programming" using Perl on Win32 is really not a practical idea. This is especially true if you are using one or more of the more common "Perl & Network programming" reference texts which are mostly based firmly upon unix/POSIX programming concepts and techniques. There are too many common programming practices used within these texts--forking, signals, zombies et al--that do not translate to Win32 for this to be a productive learning exercise.

    If your primary goal is to learn Perl on Win32, avoid network programming until you are much more familiar with Perl.

    If your aim is to become familiar with Network programming in Perl, if you have the choice, look to using a unix-like OS for your learning.

    If you want to learn network programming on Win32, avoid reference texts for "Perl & Network programming" and seek out something like Win32 Network Programming: Windows(R) 95 and Windows NT Network Programming Using MFC. This is C++ based, but will tell you much more what is possible and what techniques make sense on Win32.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: getppid is broken in Win?
by xiaoyafeng (Deacon) on Jan 19, 2007 at 03:13 UTC
    Thanks for reply! In fact,I want to learn network programming in perl. OS is not important,but my notebook is Win XP ;)
    Now I'd like to resume my learning although the course may be hard.




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction