select on stdin at EOF

select on stdin at EOF

Post by nonam » Sun, 03 Nov 2002 02:41:34



Hi All,

When you run select() on stdin and when the end of file for stdin is
reached (either by typing Ctrl+D at the console or when EOF of a file
redirected as stdin is reached), select signals that stdin is ready for
reading. Doing a read() then returns 0. If you run select() again on
stdin, it still signals that stdin is ready for reading. Why is that so?

Thanks,
Magesh

 
 
 

select on stdin at EOF

Post by Joe Halpi » Sun, 03 Nov 2002 02:55:37



> When you run select() on stdin and when the end of file for stdin is
> reached (either by typing Ctrl+D at the console or when EOF of a
> file redirected as stdin is reached), select signals that stdin is
> ready for reading. Doing a read() then returns 0. If you run
> select() again on stdin, it still signals that stdin is ready for
> reading. Why is that so?

It's not signalling that it's ready for reading. It's signalling that
if you do a read() on the descriptor, the call will not block. The
descriptor could be some random number, which has never been
associated with any I/O device, and select() would say the same thing,
because if you tried to do a read() on it, it won't block.

See, for example

http://www.opengroup.org/onlinepubs/007904975/functions/select.html

  "A descriptor shall be considered ready for reading when a call to
   an input function with O_NONBLOCK clear would not block, whether or
   not the function would transfer data successfully. (The function
   might return data, an end-of-file indication, or an error other
   than one indicating that it is blocked, and in each of these cases
   the descriptor shall be considered ready for reading.)

   A descriptor shall be considered ready for writing when a call to
   an output function with O_NONBLOCK clear would not block, whether
   or not the function would transfer data successfully."

Joe

 
 
 

select on stdin at EOF

Post by Joe Halpi » Sun, 03 Nov 2002 02:57:37




> > When you run select() on stdin and when the end of file for stdin is
> > reached (either by typing Ctrl+D at the console or when EOF of a
> > file redirected as stdin is reached), select signals that stdin is
> > ready for reading. Doing a read() then returns 0. If you run
> > select() again on stdin, it still signals that stdin is ready for
> > reading. Why is that so?

> It's not signalling that it's ready for reading. It's signalling that

Sorry, I should have said that it's not signalling that data is
available.

Joe

 
 
 

select on stdin at EOF

Post by Barry Margoli » Sun, 03 Nov 2002 03:01:42



>When you run select() on stdin and when the end of file for stdin is
>reached (either by typing Ctrl+D at the console or when EOF of a file
>redirected as stdin is reached), select signals that stdin is ready for
>reading. Doing a read() then returns 0. If you run select() again on
>stdin, it still signals that stdin is ready for reading. Why is that so?

Select is telling you that read() won't block.  It didn't, did it?

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

select on stdin at EOF

Post by Dan Merc » Sun, 03 Nov 2002 03:04:07




> Hi All,

> When you run select() on stdin and when the end of file for stdin is
> reached (either by typing Ctrl+D at the console or when EOF of a file
> redirected as stdin is reached), select signals that stdin is ready for
> reading. Doing a read() then returns 0. If you run select() again on
> stdin, it still signals that stdin is ready for reading. Why is that so?

> Thanks,
> Magesh

Because stdin points to a device that is always ready,  like a filesystem
file.  It will only block for a pipe, fifo, socket or tty. Even
for those it may unblock with no chars (for instance,  by
sending EOF by hitting Control D).

--
Dan Mercer

If responding by email, include the phrase 'from usenet'
in the subject line to avoid spam filtering.

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

select on stdin at EOF

Post by Michael Kerris » Sun, 03 Nov 2002 18:53:25





>> When you run select() on stdin and when the end of file for stdin is
>> reached (either by typing Ctrl+D at the console or when EOF of a
>> file redirected as stdin is reached), select signals that stdin is
>> ready for reading. Doing a read() then returns 0. If you run
>> select() again on stdin, it still signals that stdin is ready for
>> reading. Why is that so?

>It's not signalling that it's ready for reading. It's signalling that
>if you do a read() on the descriptor, the call will not block. The
>descriptor could be some random number, which has never been
>associated with any I/O device, and select() would say the same thing,
>because if you tried to do a read() on it, it won't block.

Small correction here - it looks as though you are saying that select() on a
closed file descrptor will return "ready" - not so, it will return the error
EBADF.

Cheers

Michael

 
 
 

1. How can csh detect EOF on stdin?

What's the csh equivalent of the /bin/sh:

        while :
        do
                read INPUT_LINE
                if test $? -ne 0; then break; fi        # EOF, exit loop

                # not EOF, process the line ...
        done

After "set input_line = $<", $status is always 0.  My system doesn't have
the line(1) program, so that's not an acceptable alternative.  Also note
that the input may contain blank (zero length) lines, so testing $input_line
== '' doesn't work right.

Thanks,
Mike Khaw
--

uucp:     {uunet|sun|ucbvax|decwrl|uw-beaver}!mkhaw%teknowledge-vaxc.arpa
hardcopy: Teknowledge Inc, 1850 Embarcadero Rd, POB 10119, Palo Alto, CA 94303

2. $$$ GET CASH $$$

3. child process gets EOF on stdin when parent exits?

4. RH 6.0 and a Eicon Diehl Diva 2.0 ISDN T/A

5. Additional STDIN and detection of EOF?

6. Announce: kdb v3.0 is available for kernel 2.4.20

7. Why would EOF (Ctl-D) characters be queued for stdin?

8. kppp

9. *NIX/C: ^D(EOF) prob-- need to ignore from stdin

10. select: data or eof?

11. select() not always detecting EOF on Solaris 2.3

12. Berkeley Sockets, select() and EOF detection.

13. Detecting socket EOF w/o select