A slightly simplified description of a remote login server:
- reads input from the remote user
- writes this input to a local application
- reads the response from the local application
- writes the response back to the remote user
Which of the 4 operations (2 reads, 2 writes) should be attempted first?
This problem can be handled sequentially using the select system call. (See the code on pages 40 - 42)
It is rather complex looking and involves these steps:
- Make each operation be non-blocking
- Create two file descriptor sets - one set for input (reads) and one for writes, initially empty.
- Keep track of which operations are wanted next (e.g., after a write to an application, next we want a read of the applications response)
- Add the descriptors for the wanted operations to the sets.
- call select
- Now check which wanted operations are actually ready
- It is safe to execution those ready operations (if any) without blocking. (The select call itself will block if NO wanted operations are ready.)
The code is rather good at obscuring what it does and is admittedly complicated.