I/O Multiplexing Chap 6
I/O Models Blocking I/O Model Nonblocking I/O Model I/O Multiplexing Model Signal Driven I/O Model Asynchronous I/O Model
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.
Comparison of I/O Model
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 possibilities wait forever: timeout = NULL wait up to a fixed amount of time polling: do not wait at all: timer value = 0
Select Function(Cont’d) Exception conditions arrival of out-of-band data for a socket presence of control status information to be read from the master side of pseudo terminal in packet mode 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 (1) A socket is ready for reading (readable) Connected socket: read() returns > 0 : data in socket receive buffer >= low-water mark( SO_RCVLOWAT ) by default, SO_RCVLOWAT == 1 (for TCP, UDP) 0 (end-of-file) : read-half of connection is closed(TCP has received a FIN) -1 : socket error is pending listening socket: # of completed connections > 0 A socket is ready for writing (writable): write returns >= 0 : available space in socket send buffer >= low-water mark( SO_SNDLOWAT ) only if connected TCP socket or UDP socket by default, SO_SNDLOWAT == 2048 (TCP, UDP) -1 socket error is pending write-half connection is closed: write() will generate SIGPIPE and return error EPIPE
Conditions for Descriptor Ready (2) 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
str_cli Function - using I/O Multiplexing Eof on input close socket 남은 reply 를 socket 에서 read 불가능
close versus shutdown Close decrease reference count close the socket only if count == 0 terminates both directions of data transfer: reading and writing Cannot read after close Shutdown initiate TCP’s normal termination regardless of reference count terminates one direction (half of connection) SHUT_RD: read-half of the connection is closed Any data currently in socket receive buffer is discarded Any data received after this call is acknowledged and then silently discarded SHUT_WR: write-half of the connection is closed(half-close) Any data currently in socket send buffer will be sent SHUT_RDWR: read-half and write-half of connection are both closed SHUT_RD + SHUT_WR
str_cli Function (Revisited)
TCP Echo Iterative Server
TCP Echo Iterative Server Program
Yet Another Problems 한 client 가 1byte data 보내고 계속 sleep 한다면 Readline() 에서 newline 도착할 때까지 blocked 됨 다른 client 도 서비스 불가 Denial of Service Attack Possible Solutions Use nonblocking I/O Have each client serviced by a separate thread (or a process) Place a timeout on the I/O operation