Will read() ever block after select()?

前端 未结 5 1740
猫巷女王i
猫巷女王i 2020-12-03 03:14

I\'m reading a stream of data through TCP/IP socket. The stream load is very uneven. Sometimes large bulks of data arrive every second, sometimes no data come for an hour. I

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 03:53

    Actually it should not block (that is what select() is for!), but in fact, it might, exceptionally. Normally, read() should return up to the maximum number of bytes that you've specified, which possibly includes zero bytes (this is actually a valid thing to happen!), but it should never block after previously having reported readiness.

    Nevertheless, see the Linux select man page:

    Under Linux, select() may report a socket file descriptor as "ready for reading", while nevertheless a subsequent read blocks. This could for example happen when data has arrived but upon examination has wrong checksum and is discarded. There may be other circumstances in which a file descriptor is spuriously reported as ready. Thus it may be safer to use O_NONBLOCK on sockets that should not block.

提交回复
热议问题