110 likes | 230 Views
4061 Session 24 (4/12). Today. Finish up File Locking Select and Poll Asynchronous I/O. Today’s Objectives. Write single-threaded code that monitors multiple file descriptors concurrently or asynchronously. Admin. Readings: Today we’re going backwards: see Robbins 8.8 and 4.4-4.5.
E N D
Today • Finish up File Locking • Select and Poll • Asynchronous I/O
Today’s Objectives • Write single-threaded code that monitors multiple file descriptors concurrently or asynchronously
Admin • Readings: • Today we’re going backwards: see Robbins 8.8 and 4.4-4.5
Reading Multiple Sources • The problem: what if we want to read from more than one FD at once? • FTP Client • Read input from terminal • Read input from network • Which one is ready? • Multiple threads? Multiple Processes? • Two more options that we’ll talk about today • Non-blocking and asynchronous I/O • Select and Poll
Non-Blocking I/O • Issue a read/write call, but don’t block if the fd isn’t ready • Keep retrying (polling) the fds • Specify O_NONBLOCK flag when you call open() • Then, if you make a read/write call that would block, it instead immediately returns with errno EAGAIN
Asynchronous I/O • Robbins 8.8 • One solution to the multiple I/O problem is to queue up a number of I/O requests, and have them fulfilled in the background
Using Posix aio • Define an asynchronous I/O control block (aiocb) • open() a file • Register a signal handler • Call the aio_read or aio_write function to request asynchronous I/O • Continue execution until the signal arrives • After completion, call the aio_return function to retrieve completion value • close() the file
Select and Poll • Robbins 4.4-4.5 • Two functions for multiplexing I/O • Simultaneously monitor multiple files • unblock when one of them is ready (for reading or for writing) • Similar in functionality and implementation, somewhat different in programmer’s interface • One is often a wrapper function for the other
Select/Poll Interface • Select • Organized by event type • Poll • Organized by file
Performance • Multiplexing I/O is central to the performance of networked servers • Thus, select and poll are seen as reliable standbys, but somewhat out of fashion • They don’t scale particularly well to very large n • Newer, faster, etc., include • /dev/poll • kqueue • epoll • real-time signals • Plenty of benchmarking studies out there, if you’re interested in server performance