Search the web
Sign In
New User? Sign Up
perl-beginner · Perl Beginners Mailing List
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Fork always dis's   Message List  
Reply | Forward Message #26195 of 26720 |
Re: [PBML] Fork always dies

From: John Francini <francini@...>
> The example is incorrect.
>
> The correct syntax should be
>
> $pid = fork() or die "Fork ERROR $!\n";)
>
> Using "or" instead of "||" is crucial.
>
> The || form of the or operator has higher precedence than the "or"
> version, and consequently causes both sides of the conditional to be
> evaluated -- that is, executed.

Nope. While sometimes the "or" versus "||" does matter it doesn't in
this case. The problem lies in something else. The fork() returns
three different things in three different cases.

- if it fails it returns an undef
- if it succeeds and you are in the parent process it returns the ID
of the created process
- if it succeeds and you are in the created process it returns 0

And the catch is that neither "or" nor "||" is able to distinguish
between the first and third case. Both 0 and undef are false.

So the code needs to do something like

my $pid = fork();
die "Fork ERROR $!\n" if !defined($pid);

if ($pid) {
print "I am the parent process.\n";
...
} else {
print "I am the new, created process.\n";
...
}

Jenda
===== Jenda@... === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery




Thu May 8, 2008 4:53 pm

jendaperl
Online Now Online Now
Send Email Send Email

Forward
Message #26195 of 26720 |
Expand Messages Author Sort by Date

Going through the book "Perl by Example" by Ellie Quigley, I was reading chapter 20 about sockets. The first server example is below from the book (example...
Dukelow, Don
dondukelow
Offline Send Email
May 8, 2008
1:32 pm

The example is incorrect. The correct syntax should be $pid = fork() or die "Fork ERROR $!\n";) Using "or" instead of "||" is crucial. The || form of the or...
John Francini
francini@...
Send Email
May 8, 2008
3:45 pm

From: John Francini <francini@...> ... Nope. While sometimes the "or" versus "||" does matter it doesn't in this case. The problem lies in something else....
Jenda Krynicky
jendaperl
Online Now Send Email
May 8, 2008
4:53 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help