Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple beginners problem

Status
Not open for further replies.

neal83

Programmer
Mar 29, 2007
5
GB
i am trying to teach myself perl and network programming with perl and i am unsure of what the problem is with the following code:

#!/usr/local/bin/perl
print "PID=$$\n";
my $child = fork();
die "cant 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";
}

i know its not much but the code is copied fom a book, the thing is it should work and its giving me a headache. i have windows XP pro and running perl 5.6

thanx in advance
 
It would be helpful if you posted what specific error you are getting.

The code as posted works so I'm guessing your path-to-perl line is wrong-->#!/usr/local/bin/perl
Sorry, I'm not sure what it is for windows, but a quick google should help-
 
Perl forking in windows is a bit of a pain. I find it easier to use the Parallel Fork Manager module. I can fork in unix no problem but windows always gave me fits.
 
The code as posted works so I'm guessing your path-to-perl line is wrong-->#!/usr/local/bin/perl

For future reference, Windows ignores the shebang line. But you are correct it is wrong for a Windows environmnt, but unless this is a CGI script, it won't matter.

Windows shebang line is generally:

#!c:\perl\bin\perl.exe

or:

#!perl

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
sorry should be more specific, when i run the program in dos i get this:
PID=3067 #the id of the current process
the get getppid function is unimplemented at c:\perl programs\fork.pl line 9.

what i find a mystery (even when i use #!c:\perl\bin\perl.exe) is that it recognises the current process id in $$ yet it fails to fork() or recognise the getppid function, i think that since its working fine on your OS i might want to up date my version of perl to 5.8 but i thought that there would be support for fork() in windows, which their probably is but i will get it anyway.

thanx to those who tried it out, i hope someone can figure out why that wont run in windows though.
 
Any reason you're running 5.6 and not 5.8.n ? 5.8 is hardly bleeding edge these days, and it might be fixed in the latest release...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I am running 5.8 on XP

Code:
print "PID=$$\n";
my $pid = fork();

if ($pid){ 
   print "parent process: $pid\n";
}
else{ 
     print "child process: $pid\n";
}

This seems to work.. but $$ never matches the parent pid.. but I don't think it will in windows. Read the perlfork documentation in activestate.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top