Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Fork always dies

Expand Messages
  • John 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
    Message 1 of 3 , May 8, 2008
    • 0 Attachment
      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]
    • Jenda Krynicky
      From: John Francini ... Nope. While sometimes the or versus || does matter it doesn t in this case. The problem lies in something else.
      Message 2 of 3 , May 8, 2008
      • 0 Attachment
        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
      Your message has been successfully submitted and would be delivered to recipients shortly.