I/O Multiplexing
TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client TCP Server stdin stdout fgets fputs writen readline
I/O Models
Blocking I/O Model
Nonblocking I/O Model
I/O Multiplexing Model
Signal Driven I/O Model
Asynchronous I/O Model Synchronous vs. Asynchronous I/O A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes. An asynchronous I/O operation does not cause the requesting process to be blocked.
Performance select() Ready! read() Ready! read() Blocking I/O I/O Multiplexing
Usage of I/O Multiplexing Client handles an interactive input and a socket handles multiple sockets at the same time Server handles both a listening socket and its connected socket handles both TCP and UDP handles multiple services and perhaps multiple protocols (e.g., inetd daemon)
Select Functions Wait for any one of multiple events to occur and wake up the process only when one or more of these events occurs or, a specified amount of time has passed wait forever: timeout = NULL wait up to a fixed amount of time polling: do not wait at all: timer value = 0 readset, writeset, exceptset after select returns may be changed Need to set them again for testing file descriptors ready ready ? Read set Write set Exception set readwrite exception handling #include /* UNIX */ #include /* Windows */ int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout); Returns: count of read desciptors if positive, 0 on timeout, -1 on error
How to Manipulate Descriptor Sets Descriptor sets(fd_set): array of integers(FD_SETSIZE) if fdset == NULL, no interest on the condition caution: value result arguments Macros
Conditions for Descriptor Ready A socket is ready for reading (readable) data in socket receive buffer >= low-water mark SO_RCVLOWAT (==1, default) read-half of connection is closed (TCP has received a FIN) listening socket and # of completed connections > 0 socket error is pending A socket is ready for writing (writable) available space in socket send buffer >= low-water mark SO_SNDLOWAT ( == 2048, default (TCP, UDP)) write-half connection is closed: write() will generate SIGPIPE and return error EPIPE A socket using nonblocking connect has completed the connection, or the connect has failed socket error is pending Socket has an exception condition pending if there exists out-of-band data still at out-of-band mark
Filling the Pipe: Echo C/S In stop-and-wait mode response time = RTT(round-trip time) + server’s processing time(=0) batch mode 로 (file 을 stdin 으로 redirection 해서 ) 1000 line 을 보내면 1000 x response time Continuous Tx Fill the pipe TCP Client TCP Server stdin stdout fgets fputs writen readline
str_cli Function - using I/O Multiplexing EoF on input close socket 남은 reply 를 socket 에서 read 불가능
str_cli Function (Revisited)
TCP Echo Iterative Server
TCP Echo Iterative Server Program