1

I am learning Linux internals. So I came across the poll system call. As far as I understand, it is used by drivers to provide notification when some data is ready to be read from device and when we have data ready to device.

If device do not have any data to read, process will get sleep and wake up when data become available and vice versa for write case.

Can someone provide me concrete understanding of poll system call with some real example?

Rohit
  • 11
  • 2
  • 1
    I'm not sure a simple example is possible here. W. Richard Stevens needed several dozen lines of code to create the core of a simple TCP "echo" server based on `poll(2)` in Chapter 6 of his *Unix Network Programming*, volume 1, and his code is a model of simplicity and clarity. That's my recommendation: get that book and read it. Although `poll(2)` can be used for any file handle, not just network sockets, that is where it's most often used, so it's where the best explanations are found. – Warren Young Dec 10 '12 at 13:49
  • Possible duplicate of [How to add poll function to the kernel module code?](https://stackoverflow.com/questions/30035776/how-to-add-poll-function-to-the-kernel-module-code) – Ciro Santilli OurBigBook.com Jun 20 '17 at 06:26

1 Answers1

2

poll and select (the latter is very similar to poll with these differences) sys calls are used in so called asynchronous event-driven approach for handling client's requests.

Basically, in network programming there are two major strategies for handling many connections from network clients by the server:

1) more traditional: threaded or process-oriented approach. In this situation network server has main proccess which listens on one specific network port (port 80 in case of web servers) for incomming connections and when connection arrives, it spawns new thread/process to handle this new connection. Apache HTTP server took this approch.

2) aforementioned asynchronous event-driven approach where (in simplest case) network server (for example web server) is application with only one process and it accepts connections (creating socket for each new client) and then it monitors those sockets with poll/select for incoming data. Nginx http web server took this approch.

mzet
  • 577
  • 2
  • 7
  • 1
    Re: Network programming, your answer implies that `select()` and `poll()` are only used with network sockets. In Unix systems, these calls can work on *any* file handle, not just sockets. – Warren Young Dec 10 '12 at 13:39
  • Re: Models of process structure, that is completely irrelevant here; the kernel doesn't care. It's just as well, since it's a hand-waving treatment of the possible range of complexity, ignoring so much that it's basically wrong. It's just as well removed. – Warren Young Dec 10 '12 at 13:40
  • Re: `poll(2)` vs. `select(2)`, the differences are irrelevant at the kernel level in Linux, since they're implemented using the same kernel-level mechanisms. – Warren Young Dec 10 '12 at 13:41
  • poll and select are for non-blocking IO, which is not to be confused with asynchronous io. – psusi Dec 10 '12 at 15:28
  • @ Warren Young: models of process structure illustrate practical use case scenario for poll/select – mzet Dec 10 '12 at 16:29
  • 1
    @mzet: Your answer paints a false dichotomy, where you have FD-per-child and blocking sockets in the one case, and monolithic processes checking for FD events in a single big loop with poll/select in the other case. I've written several programs where children are forked off and monitor FDs using `select()`. So, the fact that children are being forked off tells you nothing about how or whether you use `poll/select()`. In any case, the OP is talking about kernel internals, and a driver doesn't see anything about process structure when its poll handler function gets called. – Warren Young Dec 10 '12 at 17:25