When do read() return 0 despite that select() returned 1

When do read() return 0 despite that select() returned 1

Post by Henrik Johansso » Tue, 24 Mar 1998 04:00:00



I have a somewhat unstable program that causes strange crashes.
The code is multi-threaded using sproc(). Sometimes one of the
threads starts to loop calling a select() with a filedescriptor
of a readble pipe, gets an indication of readability, does a
read() call wich returns 0, and so on.

do {
   noOfRead = select(...);
   if (noOfRead==1) {
     howMany = read(...);
     if (howMany > 0) {
        // Do something useful
     } else if (howMany==-1) {
       // Error output
       abort();
     }
   }

Quote:} while (1);

I am not asking about why the program crashes. I am just
curious why select() indicates bytes to be read, but read()
indicates no bytes and returns 0. If I can solve that, this
part of the code can do a graceful exit. As it is today,
this part of the codes get a lot of interest from people
trying to locate the cause of error, since it loops.
I would like a way to stop looping and produce a correct
diagnostic message. Today I get contradicting messages:
select says "go ahead and read" and read() says "nothing to read".
 
 
 

When do read() return 0 despite that select() returned 1

Post by Emmanuel Garci » Tue, 24 Mar 1998 04:00:00



> I am not asking about why the program crashes. I am just
> curious why select() indicates bytes to be read, but read()
> indicates no bytes and returns 0. If I can solve that, this
> part of the code can do a graceful exit. As it is today,

read() returns 0 when you've reached EOF on the fd. Not an error.
That's that simple.

Emmanuel Garcia


 
 
 

When do read() return 0 despite that select() returned 1

Post by Brian McCaule » Tue, 24 Mar 1998 04:00:00



> Today I get contradicting messages: select says "go ahead and read"
> and read() says "nothing to read".

read()=0 does not mean "nothing to read" it means "end of file".  As
far as select() is concerned and end of file contition *is* something to
read.

read()=-1, errno=EWOULDBLOCK means "nothing to read".  Of course you
must be in non-blocking mode to get this.

--

 .  _\\__[oo       from       | Phones: +44 121 471 3789 (home)

.  l___\\    /~~) /~~[  /   [ | PGP-fp: D7 03 2A 4B D8 3A 05 37...
 # ll  l\\  ~~~~ ~   ~ ~    ~ | http://wcl-l.bham.ac.uk/~bam/

 
 
 

When do read() return 0 despite that select() returned 1

Post by Elisabeth Soderstro » Wed, 25 Mar 1998 04:00:00


I got another problem... quite the opposite. I expect select() to
return when a socket connection is broken, so I can read() 0, close
the connection and try to reestablish it.

But nothing happens when the other process dies/is killed!
netstat even says that the connection is still established. How is
this possible? It has worked before, I'm pretty sure of that...

I'm running Solaris 2.5.1 btw. Anyone who can help me?

        Elisabeth

--
*--------------------------------------------------------------------*
|     ____                        _                                  |
|    ( /_  / ' ,  _  /  _ _/)/   (_  ..  _/)_  _  , _/)_ ..          |
|    _/___/_/_/)_(|_/)_(/_/_/)_ (__)_()_(/_(/_/(_/)_/_/(_()_/))      |
|                                                                    |
| tel: 0920-120 88, 202059   snail: Krong?rdsringen 10, 974 35 Lule? |

|                                                                    |  
|     These opinions are my own, not those of Ericsson Erisoft       |
*--------------------------------------------------------------------*

 
 
 

When do read() return 0 despite that select() returned 1

Post by Bjorn Ree » Wed, 25 Mar 1998 04:00:00



> I got another problem... quite the opposite. I expect select() to
> return when a socket connection is broken, so I can read() 0, close
> the connection and try to reestablish it.

> But nothing happens when the other process dies/is killed!
> netstat even says that the connection is still established. How is
> this possible? It has worked before, I'm pretty sure of that...

select() only returns when data is available, including a notification
from the OS of the remote host that the client died. If there is no
traffic between the two processes then there is no way [1] to determine
if a connection is down. If the remote end is disconnected due to
network problems, or the host dies (or some lame Win client dies)
you will not receive any notification thereof, and you will not
notice anything until you attempt to write()/send() anything to the
socket.

The usual way to circumvent this problem is to add your own heartbeat
or keepalive message to your protocol, which is automatically sent
after some time of inactivity (just make sure that you don't start
congesting the net with your heartbeat messages.)

[1] You could use TCP KEEPALIVE messages, but they are only send
after 2 hours of inactivity, and if you are using non-blocking
sockets you will not notice until you attempt communication on
the connection anyways.

 
 
 

When do read() return 0 despite that select() returned 1

Post by Henrik Bergstr? » Wed, 25 Mar 1998 04:00:00



>I got another problem... quite the opposite. I expect select() to
>return when a socket connection is broken, so I can read() 0, close
>the connection and try to reestablish it.

>But nothing happens when the other process dies/is killed!
>netstat even says that the connection is still established. How is
>this possible? It has worked before, I'm pretty sure of that...

This does indeed sound strange. When a process exits all
open file descriptors are automatically closed. This includes
sockets. Are you sure that no other process has the socket's
fd open? A common situation is to accept() connections in
one process, fork() to handle the connection, and close() the
fd in the parent process.

If the parent process does not close() the socket fd, it will
still be open when the child process terminates and this
will keep the connection open as well. It's hard to supply
any more precise information unless you describe your
problem furhter.

/Henrik

 
 
 

1. Select returns data available read returns no data

Hello All,
I have a problem I hope someone here can answer. I have multiple
sockets that I am watching with select. I get an indication from
select that there is data to be read from a socket I am interested in.
When I go to retrieve that data, read returns 0 bytes. When is it
possible that select can return that there is data available from a
particular socket but read dosen't have data available for that
socket? My OS is AIX.

2. FWB HDT and Linux volumes?

3. Does select return "read ready" or "won't block" status?

4. Installing Redhat with LS120 floptical

5. select return OK, but read EAGAIN

6. snprintf() for Solaris? (for ntalk port)

7. Select return ok, but nothing to read ??

8. BOOT EPROM for 3C509 & 3c503 Network cards

9. Does select() return if the FD changed before select() was called?

10. select(read) on an empty file returns 1

11. read() after successful select() returns 0 !!!

12. The return of the return of crunch time (2.5 merge candidate list 1.6)

13. Problem: Command substitution adds an extra carriage return character on return.