Slide 1 : The starting point for a better world is the belief that it is possible." - Norman Cousins IO Multiplexing V. Vasantha M.E.,
Senior Lecturer,
Dept. Of Information Technology,
National Engineering College,
K. R. Nagar, Kovilpatti.
vasanthavivek@gmail.com
Overview : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 2 Overview Intro IO Models Select() function poll() function Summary IO Multiplexing
Input from multiple sources : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 3 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input and may be sending output to multiple destinations.
I/O multiplexing is used to multiplex the input from multiple sources into a single process.
I/O multiplexing: to be notified, by kernel, if one or more I/O conditions are ready. screen other terminal
devices
Where do we use : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 4 Where do we use When a client handles multiple descriptors
stdin, a network socket…
When a client handles multiple sockets at the same time
web clients
TCP server handles listening socket and connected socket at the same time
Server handles both TCP and UDP
Server handles multiple servers
I/O Models : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 5 I/O Models Blocking I/O Model - blocked all the way
Nonblocking I/O Model - if no data, immediately returns EWOULDBLOCK
I/O Multiplexing Model (select() and poll()) - blocked separately in wait and copy
Signal Driven I/O Model (SIGIO signal) - nonblocked in wait but blocked in copy (signaled when I/O can be initiated)
Asynchronous I/O Model (aio_functions) - nonblocked all the way (signaled when I/O is complete)
Two phases for an input operation:
waiting for the data to be ready in the kernel
copying the data from the kernel to the process
Blocking I/O Model : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 6 Blocking I/O Model recvfrom no datagram ready datagram ready
copy datagram copy complete process
datagram Application Kernel Wait for data Copy data
from kernel
to user Process
blocks
in a call
to
recvfrom system call return OK
Non-blocking I/O Model : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 7 Non-blocking I/O Model recvfrom no datagram ready datagram ready
copy datagram copy complete process
datagram Application Kernel Wait for
data Copy data
from kernel
to user EWOULDBLOCK system call system call EWOULDBLOCK recvfrom recvfrom recvfrom system call system call EWOULDBLOCK return OK Process
repeatedly
calls
recvfrom
waiting
for an OK
return
(polling)
I/O Multiplexing Model : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 8 I/O Multiplexing Model select no datagram ready datagram ready
copy datagram copy complete process
datagram Application Kernel Process blocks
in a call to select
waiting for
one of possibly
many sockets to
become readable system call return readable recvfrom system call return OK Copy data
from kernel
to user Process
blocks while
data copied
into application
buffer Wait for
data
Blocking I/O vs I/O Multiplexing : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 9 Blocking I/O vs I/O Multiplexing select() Ready!
read() Ready!
read() read() read() read() Blocking I/O I/O Multiplexing
Signal driven I/O : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 10 Signal driven I/O Establish
SIGIO signal
handler datagram ready
copy datagram copy complete process
datagram Application Kernel sigaction system call deliver SIGIO recvfrom system call return OK Process
blocks while
data copied
into
application
buffer Wait for
data return signal handler Process
continues
executing Copy data
from kernel
to user
Asynchronous I/O Model : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 11 Asynchronous I/O Model datagram ready
copy datagram copy complete signal handler
process
datagram Application Kernel system call deliver signal wait for
data return Copy data
from kernel
to user aio_read no datagram ready specified in aio_read Process
continues
executing
Comparison of I/O Models : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 12 Comparison of I/O Models Blocking
initiate
complete Nonblocking
check
check
check
check
check
check
check
check
complete I/O Multiplexing
check
ready
initiate
complete Signal-driven I/O
notification
initiate
complete Asynchronous I/O
initate
notification Wait
for data Copy
data
from
kernel
to user blocked blocked blocked blocked
Synchronous vs Asynchronous I/O : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 13 Synchronous vs Asynchronous I/O A synchronous I/O operation causes the requesting process to be
blocked until that I/O operation completes
Blocking
Nonblocking
I/O multiplexing
Signal driven I/O
An asynchronous I/O operation does not cause the requesting process
to be blocked.
Asynchronous I/O
Usage of I/O Multiplexing : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 14 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
select Function (1) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 15 select Function (1) Allows the process to instruct the kernel to wait for any one of multiple IO events to occur
Return (wake up) if one or more of the IO events occur or a specified amount of time has passed
Example: select can be called and tell the kernel to return only when
Any of the descriptors in the set [1,4,5] are ready for reading
Any of the descriptors in the set {2,7} are ready for writing
Any of the descriptors in the set {1,4} have an exception condition pending
T seconds have passed
Select Function (2) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 16 Select Function (2) Returns: positive count of ready descriptors
0 on timeout
-1 on error
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
} #include
#include
int select (int maxfd1,fd_set *readset,
fd_set *writeset,fd_set *exceptset
const struct timeval *timeout);
select Function (3) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 17 select Function (3) maxfd1 - the max number of descriptors in the three sets
three independent sets of descriptors are watched.
readfds - if a read is ready (one or more fd)
characters become available for reading
end of file reached
writefds - if a write is ready
exceptfds – if there are exceptions. int select (int maxfd1,
fd_set *readset,
fd_set *writeset,
fd_set *exceptset
const struct timeval *timeout);
select Function (4) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 18 select Function (4) If timeout is set to NULL, then select will wait until one of the event occurs
If timeout is set to 0, then timeout returns immediately after checking. This is called polling
Wait for a specified amount of time struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
}
struct fd_set : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 19 struct fd_set Set of descriptors that we want to wait on for events.
Typically holds 256 descriptor states.
Manipulation macros
void FD_ZERO(fd_set *fds) /* clear all bits in fdsets */
void FD_SET (int fd, fd_set *fds) /* turn on the bit for fd in fdset */
void FD_CLR (int fd, fd_set *fds) /* turn off the bit for fd in fdset */
void FD_ISSET(int fd, fd_set *fds) /* is the bit for fd on in fdset? */
Example of Descriptor sets : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 20 Example of Descriptor sets fd_set rset;
FD_ZERO(&rset);/*all bits off : initiate*/
FD_SET(1, &rset);/*turn on bit fd 1*/
FD_SET(4, &rset); /*turn on bit fd 4*/
FD_SET(5, &rset); /*turn on bit fd 5*/
Descriptor Ready Conditions : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 21 Descriptor Ready Conditions
TCP echo server : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 22 TCP echo server Rewrite the TCP Echo server as a single process that uses select to handle any number of clients, instead of forking one child per client.
When select function is not used : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 23 When select function is not used
When select function is used : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 24 When select function is used
Slide 25 : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 25 for ( ; ; )
{
select(maxfd1 , &rset , NULL , NULL , NULL);
if(FD_ISSET(sockfd , &rset))
{
if(readline(sockfd , recvline , MAXLINE)= = 0)
err_quit(“servet terminate”)
fputs (recvline , stdout);
}
if(FD_ISSET(fileno(fp), &rset))
{
if(fgets(sendline , MAXLINE , fp) = = NULL)
return;
write(sockfd , sendline ,strlen (sendline));
}
}
Data structure TCP server(1) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 26 Data structure TCP server(1)
Data structure TCP server(2) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 27 Data structure TCP server(2)
poll Function (1) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 28 poll Function (1) #include
int poll(struct pollfd *fdarray,
unsigned long nfds, int timeout);
Returns: count of ready descriptors,
0 on timeout, -1 on error
struct pollfd {
int fd; /* descriptor to check *.
short events; /* events of interest on fd */
short revents; /* events that occurred on fd */
};
poll Function (2) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 29 poll Function (2) Similar to select but provides additional information when dealing with streams devices
timeout(in msec)
INFTIM: wait forever
0 : return immediately, do not block
>0: wait specified number of miliseconds
poll Function (3) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 30 poll Function (3) Constant events revents Description
POLLIN x x normal or priority band to read
POLLRDNORM x x normal data to read
POLLRDBAND x x priority band data to read
POLLPRI x x high-priority data to read
POLLOUT x x normal data to write
POLLWRNORM x x normal data to write
POLLWRBAND x x priority band data to write
POLLERR x error occurred
POLLHUP x hangup occurred
POLLNVAL x descriptor is not an open file
Three Kinds of Data : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 31 Three Kinds of Data Normal
Priority band
High priority
TCP echo server ( revisited ) : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 32 TCP echo server ( revisited ) Rewrite the TCP Echo server as a single process that uses poll to handle any number of clients, instead of forking one child per client.
Summary : June 9, 2010 AICTE Sponsored SDP on NPM V. Vasantha, NEC 33 Summary IO Models
Select () function
Poll () function
TCP echo program using select & poll
Slide 34 : http://www.wiziq.com/vasantha Thank You ! It is not the answers that show us the way, but the questions.“
Rainer Maria Rilke, Czech poet Queries ???