Introductory concepts An IO-driven loop Register callbacks on IO events The callback is called when the registered IO event completes void init(void) { registeriocallback(fd, READ, handleread, handlereaddata); enter_io_loop(); } void enter_io_loop(void) { /* Construct list of read/write fds */ /* Perform select() to find which are ready */ numready = select(numfds, &rfds, &wfds, &exfds, NULL); /* Go through each that are ready, and call their callback */ for (i = 0; i < numfds; i++) { if (FD_ISSET(i, &rfds)) { rhandle(i, rdata); } if (FD_ISSET(i, &wfds)) { whandle(i, rdata); } if (FD_ISSET(i, &exfds)) { exhandle(i, rdata); } } } void handleread(int fd, void *data) { /* Handle reading data from fd */ }