ioctl Operations
ioctl Function
Interface Configuration Netstat, ifconfig command 에서 사용
ARP Caching and Routing Table Operations ARP(Address Resolution Protocol) cache operations ARP: protocol(IP) address hardware address Routing table operations
fcntl Function Set socket for nonblocking I/O: F_SETFL, O_NONBLOCK Set socket for signal-driven I/O: F_SETFL, O_ASYNC Set socket owner: F_SETOWN Get socket owner: F_GETOWN
Fcntl, ioctl, and routing socket
Nonblocking I/O
Blocking and Nonblocking Sockets OperationBlockingNonblocking Input TCP no data available in socket receive buffer Returns immediately with error of EWOULDBLOCK UDP socket receive buffer is empty Output TCP no room in socket send buffer immediate error return if no room in socket send buffer, or returns # of bytes written if some room in socket send buffer UDP never blocks (no actual UDP socket send buffer) never blocks (Returns # of bytes written) accept() new connection is not available Returns immediately with error of EWOULDBLOCK connec t() TCP blocks for at least RTT Returns immediately with error of EINPROGRESS if connection not established, or returns OK(==0) if connection established immediately UDP never blocks (just store peer’s IP addr/port #)
Buffering for Nonblocking Read/Write Send buffer Receive buffer
Nonblocking str_cli nonblock/strclinonb.c:
Time Line of Nonblocking Example
A Simpler Version of str_cli using concurrent processes
A Simpler Version of str_cli using Threads
Clock Time Comparison of str_cli Sample case input file of 2000 lines 175msec RTT Stop-and wait:354.0 sec select and blocking I/O: 12.3 sec Nonblocking I/O: 6.9 sec Two processes with fork: 8.7 sec Threaded version: 8.5 sec 1 Simpler is better
Nonblocking connect Usage overlap other processing with three-way handshake(for RTT) establish multiple connections: popular with Web browsers shorten timeout for connection establishment(typically 75 sec) by using select with time limit specification Non blocking connect Returns immediately with an error of EINPROGRESS, but TCP three-way handshake continues check for either successful or unsuccessful completion of connection establishment using select If server is on the same host, connection establishment normally takes place immediately(returns 0) Many portability problems
Nonblocking Connect: Daytime Client lib/connect_nonb.c Shorten timeout
Nonblocking Multiple Connections: Web Client Establish a connection for reading a page Then, establish multiple connections for reading references in the page Using nonblocking connections Or using thread per connection
Nonblocking accept Use nonblocking accept Or, select for listening socket(may blocked) then, accept when listening socket is ready
Signal-Driven I/O
2 different ways to build a UDP server
Signal-Driven I/O for Sockets Steps for Signal-driven I/O establish SIGIO signal handler set socket owner for receiving signals fcntl(sockfd, F_SETOWN, getpid()); enable socket to signal-driven I/O ioctl(sockfd, FIOASYNC, &on); /* signal-driven I/O */ ioctl(sockfd, FIONBIO, &on); /* non-blocking I/O */ SIGIO signal is generated whenever for UDP socket a datagram arrives an asynchronous error occurs for TCP sockets Signal is generated too often not useful too many cases
UDP Echo Server using SIGIO (I) sigio/dgecho01.c
UDP Echo Server using SIGIO (II)