Re: select() timeout question

Michał Nazarewicz <[email protected]>
Newsgroups org.kernel.vger.linux-c-programming
Message-ID <op.u3x2mqc07p4s8u@localhost>
On Wed, 25 Nov 2009 09:29:33 +0100, Randi Botse <[email protected]> wrote:

> I'm now learning the Linux's select() system call,
>
> int select(int fd, fd_set *rset, fd_set *wset, fd_set *excepfs, struct
> timeval *timeout);
>
> I want to receive notification when the given file descriptor is ready
> to read, I use TCP socket connection to demonstrate this, one for the
> sender and other for the receiver, with normal condition, when the
> sender sends data via write(), the select() returns and tells the
> receiver there is data to read.
>
> My question is: what happens when the receiver's select() is reaching
> its timeout while the sender sends data? Should the notification and
> it's data be lost (discarded)?

Data read from a socket is buffered somewhere in kernel (and the same
goes for any other file descriptors).  You may think of select(2) as
a way to check if there is any data ready in the buffer.  If select(2)
reaches timeout before data is written to buffer you get 0 return value
(if I recall correctly) but the buffer is still there and if the data
is written into the buffer the next select(2) will notify about it.

In fact, you can use select(2) with a zero timeout (as opposed to
giving NULL as timeout argument) to check if there is data without
waiting for any.

-- 
Best regards,                                           _     _
  .o. | Liege of Serenely Enlightened Majesty of       o' \,=./ `o
  ..o | Computer Science,  Michał "mina86" Nazarewicz     (o o)
  ooo +---<[email protected]>---<[email protected]>---ooO--(_)--Ooo--

--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.