i/o multiplexing. tcp echo client: i/o operation is sequential !! tcpcliserv/tcpcli01.c:...

18
I/O Multiplexing I/O Multiplexing

Upload: brenda-hardy

Post on 05-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

I/O MultiplexingI/O Multiplexing

Page 2: 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

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

Page 3: 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

I/O ModelsI/O Models

Page 4: 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

Blocking I/O ModelBlocking I/O Model

Page 5: 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

Nonblocking I/O ModelNonblocking I/O Model

Page 6: 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

I/O Multiplexing ModelI/O Multiplexing Model

Page 7: 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

Signal Driven I/O ModelSignal Driven I/O Model

Page 8: 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

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.

Page 9: 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

PerformancePerformance

select() Ready!

read()Ready!

read()

read()

read()

read()

Blocking I/O I/O Multiplexing

Page 10: 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

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)

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

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

Page 15: 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

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

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

Page 16: 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

str_clistr_cli Function (Revisited) Function (Revisited)

Page 17: 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

TCP Echo Iterative Server TCP Echo Iterative Server

Page 18: 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

TCP Echo Iterative Server ProgramTCP Echo Iterative Server Program