views:

251

answers:

1

I want to ask what are the cases when do we need to use Non blocking flag on file/socket descriptors means instead we can always use select function call to determine the ready descriptor. This is reference to program in section 16.2 of Unix Network Programming V1. In that program, why does the author sets non blocking flag on the 3 descriptors stdin,stdout, socket. He also says that EWOULDBLOCK never occurs. But he also says that the time decreases from 12.3 to 6.9 seconds.

+1  A: 

Files:

Its good to open devices such as modems that need some time to initialize with a non blocking FD (aka O_NONBLOCK). This holds true not just for modems, but many kinds of character devices that need to construct themselves in order to be suitable for use, or where the device is likely to block prior to signaling itself as 'ready' for some other reason.

Various QRNGs (Quantum Random Number Generators) also need this flag, as well as various types of lighting controllers that must seek peers prior to signaling 'ready!'.

Additionally, as some user space file systems provide an ioctl() interface that is known to block for a few seconds .. you may or may not want to pass a non-blocking FD in that case, depending on how detrimental sleep may be for the timing that you allowed.

Socket:

When you wish to use non-blocking I/O while a single threaded 'butler' tends to many guests. A common misconception here is thinking that block free means lock free by some mystical means. It doesn't. In fact, either is usually exclusive of the other.

Tim Post
I am asking difference between using select / setting Non blocking flag.
avd
Is there some simple application? U have mentioned complex applications.
avd