home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / programm / 4270 < prev    next >
Encoding:
Internet Message Format  |  1992-08-13  |  3.8 KB

  1. Path: sparky!uunet!mcsun!corton!cenaath.cena.dgac.fr!stna.dgac.fr!melennec
  2. From: melennec@stna.dgac.fr (Ronan Melennec STNA 7C p8240 BD22)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: non-blocking input
  5. Message-ID: <1992Aug13.121758.12403@cenaath.cena.dgac.fr>
  6. Date: 13 Aug 92 12:17:58 GMT
  7. References: <8eTJ5su00VpDQUk0Ml@andrew.cmu.edu> <9208066999@umunk.GUN.de> <1992Aug11.092112.8339@cenaath.cena.dgac.fr> <93345@bu.edu>
  8. Sender: news@cenaath.cena.dgac.fr
  9. Organization: STNA 95 rue Henri Rochefort 91025 Evry Cedex 33(1)60.79.80.00
  10. Lines: 72
  11.  
  12. In article <93345@bu.edu>, tasos@cs.bu.edu (Anastasios Kotsikonas) writes:
  13. > In article <1992Aug11.092112.8339@cenaath.cena.dgac.fr> melennec@stna.dgac.fr (Ronan Melennec STNA 7C p8240 BD22) writes:
  14. > >In article <9208066999@umunk.GUN.de>, udo@umunk.GUN.de (Udo Munk) writes:
  15. > >> zg03+@andrew.cmu.edu (Zafrir E. Gan) writes:
  16. > >> : 
  17. > >> :     I'm writing an interactive server that needs to check for input from
  18. > >> : sockets as well as keyboard commands.  The socket part is already done
  19. > >> : so what I need is a way to check for input on the keyboard, read it if
  20. > >> : it's there but continue with the cycle if it isn't.  Any suggestions?
  21. > >> 
  22. > >> Use fcntl(0, F_SETFL, O_NDELAY). A read from stdin now returns with 0
  23. > >> bytes readed, if there is no input and you can continue the cycle.
  24. > >
  25. > >Using O_NDELAY leads to the following inconvenient : you can't
  26. > >distinguish an IDLE input (no byte available) from a CLOSED input
  27. > >(such as peer socket closed by peer process, or user hangup).
  28. > >Both return 0.
  29. > >
  30. > I would disagree. The following code works fine for me:
  31. >
  32. > [example of non blocking read on a socket fd deleted] 
  33.  
  34. My apologies: I should have written `you can't IN GENERAL distinguish...'
  35.  
  36. While it is true that O_NDELAY will put a socket file descriptor
  37. in non blocking mode, it is in general not true for other kinds of file
  38. descriptors.
  39.  
  40. Quote from SunOS 4.1.1 man(2V) man page:
  41.  
  42.      When attempting to read from a descriptor associated with an
  43.      empty pipe, socket, FIFO, or stream:
  44.  
  45.      +  If the object the descriptor is associated with is marked
  46.         for  4.2BSD-style  non-blocking  I/O  (with  the  FIONBIO
  47.         ioctl() request or a call to fcntl(2V) using the  FNDELAY
  48.         flag   from   <sys/file.h>  or  the  O_NDELAY  flag  from
  49.         <fcntl.h> in  the  4.2BSD  environment),  the  read  will
  50.         return -1 and errno will be set to EWOULDBLOCK.
  51.  
  52.      +  If the descriptor is marked for POSIX-style  non-blocking
  53.         I/O   (using   fcntl()  with  the  O_NONBLOCK  flag  from
  54.         <fcntl.h>) and refers to a stream, the read  will  return
  55.         -1 and errno will be set to EAGAIN.
  56.  
  57. [according to my experience O_NONBLOCK works for any kind of file
  58.  descriptor, not only streams. - RM ]
  59.  
  60.      +  If the descriptor  is  marked  for  System  V-style  non-
  61.         blocking  I/O  (using  fcntl()  with  the FNBIO flag from
  62.         <sys/file.h> or the O_NDELAY flag from <fcntl.h>  in  the
  63.         System  V  environment),  and does not refer to a stream,
  64.         the read will return 0.
  65.     Note: this is  indistinguishable from EOF.
  66.     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  67.  
  68. (In SunOS 5.0, O_NDELAY and O_NONBLOCK cause the same behavior
  69. when applied to a socket fd  : both cause read(2) to set errno to EAGAIN
  70. when no byte can be transferred. In SunOS 5.0 sockets are implemented
  71. on top of streams.)
  72.  
  73. My opinion is that you should use O_NONBLOCK (if your system supports it)
  74. in the interest of portability and perennity and functionality (it
  75. work the same with pipes, sockets, ttys and pseudo-ttys).
  76. Also it is a POSIX thing :-)
  77.  
  78. Regards,
  79. -- 
  80. Ronan Melennec              | Service Technique de la Navigation Aerienne
  81. +33 1 60 79 82 40           | Departement 7
  82. melennec@stna7.stna.dgac.fr | 95, rue Henri Rochefort, 91025 EVRY CEDEX, FRANCE
  83.