I/O Multiplexing
-
Upload
dara-burke -
Category
Documents
-
view
14 -
download
0
description
Transcript of I/O Multiplexing
![Page 1: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/1.jpg)
I/O MultiplexingRoad Map:1. Motivation2. Description of I/O multiplexing3. Scenarios to use I/O multiplexing4. I/O Models Blocking I/O Non-blocking I/O I/O multiplexing (select(..) and poll(..)
functions) Signal driven I/O Asynchronous I/O
5. Comparison of models
![Page 2: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/2.jpg)
Motivation Read/write from multiple descriptors?
What if data arrive from both fd1 & fd2?• If you do recv(fd1,..), then you blocked
until something arrives.• If something arrives at fd2, you have no
idea!• Ex: Web client, download accelerator,
proxies.
Solution : I/O multiplexing
![Page 3: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/3.jpg)
Description of I/O multiplexing
We need to check both fd1 & fd2 receive from the one which has
data.
This is called I/O multiplexing. Achieved by select and poll fcns.
![Page 4: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/4.jpg)
Scenarios to use I/O multiplexing1. Client handle multiple descriptors
Ex: interactive input & network socket
2. Client handle multiple sockets Web client, download accelerator,
proxies
3. Server handles both TCP & UDP DNS listens both TCP53 & UDP53
4. Server handles multiple services or protocols
![Page 5: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/5.jpg)
A note.. These techniques are not limited
to network programming.
I/O multiplexing is also used by File I/O Interactive I/O Network I/O
![Page 6: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/6.jpg)
Blocking I/O
Application Kernel
recvfrom
system call no ready datagram
datagram readycopy datagram
copy complete
wait for data here
copy data from kernel to user
return OK
process datagram
Application blocks here
Now let’s talk about this method. Do you think it is a good approach? What are pros and cons?
![Page 7: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/7.jpg)
Non-blocking I/O
Application Kernel
recvfromsystem call
no ready datagram
EWOULDBLOCK
recvfromsystem call no
ready datagram
EWOULDBLOCK
recvfromsystem call
datagram readycopy datagram
process datagram
return OK
copy complete
copy data from kernel to user
.
.
.
Pro
cess
repeate
dly
calls
re
cvfr
om
wait
ing f
or
an
O
K (
polli
ng)
wait for data here
![Page 8: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/8.jpg)
Non-blocking I/O cont.
Requires socket to be set non-blocking mode.
fcntl (stands for file control) sets a socket to non-blocking mode
Example: UdpNonblockingIO.c
#include <fcntl.h>int fcntl(int fd, int cmd,...) returns: depends on cmd if OK, -1
on error.
![Page 9: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/9.jpg)
I/O multiplexing model
Application Kernel
selectsystem call no
ready datagram
recvfromreturn readable
system call
process datagram
return OK
copy complete
datagram readycopy datagram
copy data from kernel to user
wait for data here
Pro
cess
blo
cks
while
data
is
copie
d f
rom
ke
rnel
Pro
cess
blo
cks
in
sele
ct w
ait
ing f
or
on
e o
f p
oss
ibly
m
any s
ock
ets
to
beco
me r
ead
able
![Page 10: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/10.jpg)
select System Call select allows the process to instruct
the kernel to wait for any of multiple events to occur and to wake up the process only when one or more of these events occurs or when a specified amount of time has passed. #include <sys/select.h>#include <sys/time.h>int select(int maxfd, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout) returns: positive count of descriptors, 0 on timeout, -1 on error.struct timeval{
long tv_sec; /* seconds */long tv_usec; /* milliseconds */
};
![Page 11: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/11.jpg)
select cont.
3 possibilities of timeout1. Wait forever: NULL timeout
argument.2. Wait up to a fixed amount of
time.3. No wait at all: Return immediately after
checking the descriptors. This is called “polling”.
both tv_sec & tv_usec are set to “0”.
![Page 12: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/12.jpg)
An example We can call select and tell the kernel to
return only when any of the descriptors in the set
{1,4,5} are ready for reading. {2,7} are ready for writing. {2,4,5} have an exception. 10.2 seconds have elapsed.
Tell the kernel what descriptors we are interested in (for reading, writing, or an exception condition) and how long to wait.
![Page 13: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/13.jpg)
How to specify descriptor set?
There are fuctions to set, clear, and query on a descriptor set.
Example:
selectEx.c
void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */void FD_SET(int fd, fd_set *fdset); /* turn on bits */void FD_CLR(int fd, fd_set *fdset); /* turn off bits */int FD_ISSET(int fd, fd_set *fdset); /* is the bit for fd on in fdset? */
fd_set rset;FD_ZERO(&rset);FD_SET(1, &rset);FD_SET(4, &rset);FD_CLR(5, &rset);
![Page 14: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/14.jpg)
poll System Call Very similar to select.
fdarray is a pointer to the first element of an array of structures. Each element in the array is a pollfd structure that specifies the conditions to be tested for a given descriptor, fd.
Most important events: POLLIN, POLLOUT.
#include <poll.h>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 interests on fd
*/short revents; /* events occured on fd */
};
![Page 15: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/15.jpg)
Signal driven I/O A signal is a notification to a
process that an event has occured. They are asynchronous.
Example: Pressing ^C while program running. Child process terminates and
kernel sends SIGCHLD signal to parent.
![Page 16: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/16.jpg)
Signal driven I/O cont. Every signal has a disposition. We set disposition by calling “sigaction”
with 3 options.1. Provide a function (signal handler) for
signal occurence. SIGKILL & SIGSTOP signals cannot be caught.
2. Ignore a signal by setting disposition to SIG_IGN. SIGKILL & SIGSTOP cannot be ignored.
3. Set default disposition by SIG_DFL. Example: signalEx1.c (handling ^C) signalEx2.c (using SIGALRM to perform I/O)
![Page 17: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/17.jpg)
Signal driven I/O cont. The idea is to set the I/O
descriptor for signal driven I/O and then set SIGIO handler calling sigaction.
Whenever the descriptor is ready for reading, the kernel will generate SIGIO signal.
The I/O can then be performed within signal handler.
![Page 18: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/18.jpg)
Signal driven I/O diagram
Application Kernel
Establish SIGIO signal handler
sigaction system call
initialize
return
signal handler
deliver SIGIO datagram ready
system callcopy datagram
process datagram
return OK
copy complete
Pro
cess
co
nti
nues
wait for data here
recvfrom
Pro
cess
wait
s
copy data from kernel to user
This is an advanced I/O model & we will not cover this in this class.
![Page 19: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/19.jpg)
Asynchronous I/OThe idea is to start the I/O operation
and let the kernel notify us when the entire process is complete (including the copy of the data from the kernel to our buffer).
Main difference between signal driven I/O is that with signal driven I/O the kernel tells when an I/O operation can be initiated.
But here kernel tells when I/O operation is complete.
![Page 20: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/20.jpg)
Asynchronous I/O diagramApplication Kernel
aio_read
no ready datagram
datagram readycopy datagram
copy complete
wait for data here
copy data from kernel to userSignal handler
process datagram
Process continues executing
system call
return
Deliver signal specified in aio_read
Few POSIX systems support asynchronous I/O
![Page 21: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/21.jpg)
Comparison of I/O Models1 2 3 4 5
1st phase: wait for data
Initiate CheckCheckCheckCheckCheck
Check
Ready Notification
Initiate
2nd phase: Copy data from kernel to user Complet
eComplet
eComplet
eComplete Complet
e
1st phase handled differently2nd phase handled the same
(i.e. blocked in call to recvfrom)
Handles both
phases
blocked
blocked
blocked
blocked
![Page 22: I/O Multiplexing](https://reader035.fdocuments.in/reader035/viewer/2022062721/5681386a550346895da01ae7/html5/thumbnails/22.jpg)
Last wordThe easiest to use among them
◦Blocked I/O◦I/O Multiplexing
If your system supports threads, you can emulate other I/O models using multiple threads: ◦One thread to wait for I/O for first and
third models & the other thread doing processing if something needs to be processed. Thus we overlap I/O & computation within a single application.