简体   繁体   中英

Is it necessary to use non-blocking file descriptors with IO multiplexing?

Posix supports blocking and non-blcoking file descriptors. Second ones may be opened with O_NONBLOCK flag. I have a main loop in my app, which polls some set ( poll sys call) of file descriptors for POLLIN and POLLOUT events. May I still use blocking file descriptors, cause I write only when POLLOUT is set and read only when POLLIN is set?

Accroding to poll(2) man page:

POLLOUT Writing is now possible, though a write larger that the available space in a socket or pipe will still block (unless O_NONBLOCK is set).

In other words: if there is not enough space in kernel buffer associated with this fd, writing a chunk of data, larger than space available in buffer would block. If there is space available they behave identically.

So you must set all your file descriptors to be non-blocking, especially TCP sockets, cause if the process on the other side has slow connection you may face blocking write call, until client won't send you back all ACKs for every IP package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM