I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c:...

Post on 05-Jan-2016

230 views 0 download

Transcript of I/O Multiplexing. TCP Echo Client: I/O operation is sequential !! tcpcliserv/tcpcli01.c:...

I/O MultiplexingI/O Multiplexing

TCP Echo Client: I/O operation is TCP Echo Client: I/O operation is sequential !!sequential !!tcpcliserv/tcpcli01.c: lib/str_cli.c:

TCPClient

TCPServer

stdin

stdout

fgets

fputs

writen

readline

I/O ModelsI/O Models

Blocking I/O ModelBlocking I/O Model

Nonblocking I/O ModelNonblocking I/O Model

I/O Multiplexing ModelI/O Multiplexing Model

Signal Driven I/O ModelSignal Driven I/O Model

Asynchronous I/O ModelAsynchronous 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.

PerformancePerformance

select() Ready!

read()Ready!

read()

read()

read()

read()

Blocking I/O I/O Multiplexing

Usage of I/O MultiplexingUsage 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 FunctionsSelect 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 ?

Readset

Writeset

Exceptionset

readwrite exceptionhandling

#include <sys/time.h> /* UNIX */#include <sys/select.h> /* UNIX */#include <unistd.h> /* UNIX */#include <winsock2.h> /* 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 SetsHow 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 ReadyConditions 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/SFilling 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

TCPClient

TCPServer

stdin

stdout

fgets

fputs

writen

readline

str_clistr_cli Function - using I/O Multiplexing Function - using I/O Multiplexing

EoF on input close socket 남은 reply 를 socket 에서 read 불가능

str_clistr_cli Function (Revisited) Function (Revisited)

TCP Echo Iterative Server TCP Echo Iterative Server

TCP Echo Iterative Server ProgramTCP Echo Iterative Server Program