Select
Select
What was the worst part about A3?
What was the worst part about A3?
For me it was dealing with reads blocking.
It probably was way worse for you though!
Blocking and Networks
These headaches get even worse
when using sockets over networks
Clients can disconnect at any time.
Including in a read() call
This causes the read to block
New connections cannot be accepted
when blocking
Multiplexed I/O
Multiplexed I/O provides a solution to the problem of read blocking.
The idea is to keep track of a set of file descriptors and read only when the descriptor is ready to be read from.
The Select operation determines when it is safe to read.
Select
Select uses a set of file descriptors to monitor.
Timeout information is given to set
nfds: Largest fd from 3 sets allowed +1
readfds: fds to check for read
writerds: same but for write.
exceptfds: listens for “exceptional events”
Modification of FD Sets
FD_ZERO: Clears file descriptor set
FD_SET: Adds fd to the fdset
FD_CLR: Removes fd
FD_ISSET: Checks to see if fd is in set
These functions help read and modify the bit array
which stores the file descriptors we want to monitor.
Example
Uses multiplexed IO to take stdin
and return if there is something
to read from stdin and stop listening
after 5 seconds.
/docProps/thumbnail.jpeg