chapter6
- 6.1 Overview
- I/O multiplexing is typically used in the following network applications.
- (1): When the client processes multiple descriptors, IO multiplexing must be used
- (2): It is possible, but not uncommon, for a client to handle multiple sockets at the same time.
- (3): If a TCP server handles both listening sockets and connected sockets.
- (4): If a server has to handle both TCP and UDP
- (5): If a server has to handle multiple services or multiple protocols
- IO multiplexing is not limited to the network, many important applications also need to use this technology.
- 6.2 I/O Model
- Basic differences between the 5 I/O models available under Unix:
- (1) Blocking I/O
- (2) Non-blocking I/O
- (3) I/O multiplexing (select and poll)
- (4) Signal-driven I/O (SIGIO)
- (5) Asynchronous I/O (POSIX aio_ series functions)
- 6.2.1 Blocking I/O
- 6.2.2 Non-blocking I/O Model
- 6.2.3 I/O multiplexing model
- With I/O multiplexing, we can call select or poll and block on one of these two system calls instead of blocking on the real I/O system call.
- 6.2.4 Signal-driven I/O model
- We can also use signals to have the kernel send a SIGIO signal to notify us when the descriptor is ready. We call this model signal-driven I/O
- 6.2.5 Asynchronous I/O Model
- 6.3 select function
- This function allows a process to instruct the kernel to wait for any one of several events to occur, and to wake it up after one or more events have occurred or a specified period of time has elapsed.
- We call select to tell the kernel which descriptors (read or write or exception) are interested in and how long to wait.
- #include <sys/select.h>
- #include <sys/time.h>
- int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, cosnt struct timeval *timeout);
- struct timeval
- {
- long tv_sec; //seconds
- long tv_usec;//Subtle
- };
- void FD_ZERO(fd_set *fdset);
- void FD_SET(int fd, fd_set *fdset);
- void FD_CLR(int fd, fd_set *fdset);
- int FD_ISSET(int fd, fd_set *fdset);