se7en
se7en

Reputation: 21

How linux select() works?

Could someone explain how select() works to me? I have a wrong mental model and do not understand it from the man page.

If I have clients with multiple sockets to different servers and I am reading some info periodically from server, how does the kernel knows which socket mark to read? How does he know which socket read() will not be blocking? I think it is not predictable if it actually does not read data from server.

Upvotes: 1

Views: 607

Answers (1)

user2404501
user2404501

Reputation:

The kernel isn't predicting anything. It's telling you the current status of the socket's receive buffer. If the buffer is not empty, the socket is readable. If the buffer is empty, select() waits. When a packet arrives from the server, the kernel matches it to the correct socket using the IP address, protocol, and port numbers. The packet is put into the socket's receive queue and select() is notified that the status has changed.

Upvotes: 4

Related Questions