Reputation: 16435
The man page for select(2) says:
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform a corresponding I/O operation (e.g., read(2) without blocking, or a sufficiently small write(2)).
During this waiting time, does the kernel itself constantly poll the sockets to see if they're ready? How is watching a socket implemented?
Upvotes: 1
Views: 1024
Reputation: 16435
https://eklitzke.org/how-tcp-sockets-work answered my question
When a new data packet comes in on the network interface (NIC), the kernel is notified either by being interrupted by the NIC, or by polling the NIC for data. Typically whether the kernel is interrupt driven or in polling mode depends on how much network traffic is happening; when the NIC is very busy it’s more efficient for the kernel to poll, but if the NIC is not busy CPU cycles and power can be saved by using interrupts. Linux calls this technique NAPI, literally “New API”.
When the kernel gets a packet from the NIC it decodes the packet and figures out what TCP connection the packet is associated with based on the source IP, source port, destination IP, and destination port. This information is used to look up the struct sock in memory associated with that connection. Assuming the packet is in sequence, the data payload is then copied into the socket’s receive buffer. At this point the kernel will wake up any processes doing a blocking read(2), or that are using an I/O multiplexing system call like select(2) or epoll_wait(2) to wait on the socket.
Upvotes: 3
Reputation: 25895
No, it registers on the wait queue of each file descriptor, and goes to sleep (until signaled.). There is a pretty succinct answer on Quora with some more info, by Nelson Elhage:
https://www.quora.com/Network-Programming-How-is-select-implemented
and you can also delve into the source.
Upvotes: 2