Fundamental of asynchronous programming: Non Blocking Sockets
Why async socket programming
- An application can create thousands of sockets but creating a thread per socket becomes infeasible for a large number (e.g. 100k sockets)
- An async framework consists non-blocking sockets and an event loop
* The kernel allows applications to seek for an event * Application registers a callback to be called when the event occur * Callback runs by the same application thread after kernel intimates that an event happened on a socket
- This model uses a single thread to poll many sockets for various events and calls the callback.
- It is concurrent that is runs many tasks but not parallel execution of tasks.
How do a socket turn into non-blocking?
- A socket is a file, read and written with standard POSIX calls.
- A system call for read operation uses the kernel BIO (Block I/O). A BIO provides an interface to access a block device.
ioctlallows to set a fd non-blocking for read/write calls
- Learn all about BIO: https://lwn.net/Articles/736534/
Written with StackEdit.