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...
Message search is now enhanced, find messages faster. Take it for a spin.

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 #26194 of 26720 |
Re: [PBML] Fork always dies

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.

John



On 8 May 2008, at 9:29, Dukelow, Don wrote:

> 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 20.17) with a few minor changes. As it is
> written from the book the fork command always die's when the client
> script is started. ($pid = fork() || die "Fork ERROR $!\n";) But
> when I take out the "|| die" part it works.
> My question is why does the die command always get executed when
> part of the fork line.
>
> #! /usr/local/bin/perl
>
> use warnings;
> use Socket;
>
> print "\n\tServer startrd\n\n";
>
> $AF_UNIX = 1;
> $SOCK_STREAM = 1;
> $PROTOCOL = 0;
>
> socket (SERVERSOCKET, $AF_UNIX, $SOCK_STREAM, $PROTOCOL) ||
> die "Socket $!\n";
> print "Socket OK!\n";
>
> $name = "./greetings";
> unlink "./greetings" || warn "$name: $!\n";
>
> bind (SERVERSOCKET, $name) || die "Bind $!\n";
> print "Bind OK!\n";
>
> listen(SERVERSOCKET, 5) || die "Listem $!\n";
> print "Listren OK!\n";
>
> while (1) {
> accept (NEWSOCKET, SERVERSOCKET);
> sleep 1;
> # $pid = fork() || die "Fork ERROR $!\n"; # Always dies
> $pid = fork(); # Works fine
> if ($pid == 0) {
> print NEWSOCKET "Greetings from your server!!\n";
> close (NEWSOCKET);
> exit (0);
> }
> else {
> close (NEWSOCKET);
> }
> }
> -----
> Don Dukelow
> HP License Team
> Hewlett-Packard Company
> Tel: 810-728-3388
> e-mail: dukelow@...
>
>



[Non-text portions of this message have been removed]




Thu May 8, 2008 3:38 pm

francini@...
Send Email Send Email

Forward
Message #26194 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
Offline Send Email
May 8, 2008
4:53 pm
Advanced

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