Upload
gamindu-udayanga
View
218
Download
0
Embed Size (px)
Citation preview
8/13/2019 510_socket5
1/20
I/O Multiplexing
8/13/2019 510_socket5
2/20
What is I/O multiplexing?
When an application needs to handle
multiple I/O descriptors at the same time
E.g. file and socket descriptors, multiple socketdescriptors
When I/O on any one descriptor can resultin blocking
8/13/2019 510_socket5
3/20
Non-forking concurrent server
Concurrent Server
select()
C1 C2 C3 C4 C5
listenfd
fd1 fd2 fd3 fd4 fd5Sockets
Clients
8/13/2019 510_socket5
4/20
I/O Models
1. Blocking I/O
2. Non-blocking I/O
3. I/O multiplexingselect()
4. Signal driven I/O
5. Asynchronous I/O
8/13/2019 510_socket5
5/20
Blocking I/O
recvfrom() No datagram ready
datagram ready
Copy datagram
Copy completeProcess datagram
System call
Application Operating system
Wait for data
Copy data to user
Return ok
Process blocks
8/13/2019 510_socket5
6/20
Non-Blocking I/O
recvfrom() No datagram ready
datagram ready
Copy datagram
Copy completeProcess datagram
System call
Application Operating system
Copy data to user
Return ok
Process blocks
EWOULDBLOCK
recvfrom() No datagram readySystem call
EWOULDBLOCK
recvfrom() System call
8/13/2019 510_socket5
7/20
I/O Multiplexing
select() No datagram ready
datagram ready
Copy datagram
Copy completeProcess datagram
System call
Application Operating system
Wait for data
Copy data to user
Return ok
Process blocks
Return readable
Process blocks
recvfrom() System call
8/13/2019 510_socket5
8/20
8/13/2019 510_socket5
9/20
Asynchronous I/O
aio_read() No datagram ready
datagram ready
Copy datagram
Copy completeSignal handler
Process datagram
System call
Application Operating system
Wait for data
Copy data to user
Return ok
Process continues
return
8/13/2019 510_socket5
10/20
select()call
Allows a process to wait for an event to
occur on any one of its descriptors.
Types of event
ready for read
ready for write
Exception condition
8/13/2019 510_socket5
11/20
select() call
int select(
int maxfdp1, /* max. fd + 1 */
fd_set *readfds, /* read ready? */
fd_set *writefds, /* write ready? */fd_set*exceptfds, /* exceptions? */
struct timeval*timeout);
struct timeval{long tv_sec; /* seconds */
long tv_usec; /* microseconds */
}
8/13/2019 510_socket5
12/20
struct fd_set
Set of descriptors that we want to wait on forevents.
Typically holds 256 descriptor states.
Manipulation macros void FD_ZERO(fd_set*fds) void FD_SET (int fd, fd_set*fds)
void FD_CLR (int fd, fd_set*fds)
void FD_ISSET(int fd, fd_set*fds)
8/13/2019 510_socket5
13/20
Non-forking Concurrent Server
fdsetrdset, wrset;int listenfd, connfd1, connfd2;
int maxfdp1;
Connection establishment etc.
/* initialize */
FD_ZERO(&rdset);
FD_ZERO(&wrset);
f ( ) {
8/13/2019 510_socket5
14/20
for( ;; ) {
FD_SET(connfd1, &rdset);
FD_SET(connfd2, &wrset);
FD_SET(listenfd, &rdset);
maxfdp1 = max(connfd1, connfd2, listenfd) + 1;
/* wait for some event */
Select(maxfdp1, &rdset, &wrset, NULL, NULL);
if( FD_ISSET(connfd1, &rdset) ) {Read data from connfd1
}
if( FD_ISSET(connfd2, &wrset) ) {
Write data to connfd2
}
if( FD_ISSET(listenfd, &rdset) {Process a new connection
}
}
8/13/2019 510_socket5
15/20
High resolution timer!
Select can be used as a millisecond
resolution timer.
select( 0, NULL, NULL, NULL, &timeval)
Usual sleep() call has resolution of seconds.
8/13/2019 510_socket5
16/20
8/13/2019 510_socket5
17/20
Managing socket options3 ways
getsockopt() and setsockopt()
fcntl()
ioctl()
8/13/2019 510_socket5
18/20
getsockopt() and setsockopt()
int getsockopt(
int sockfd,
int level, /* SOL_SOCKET, IPPROTO_IP, IPPROTO_TCP etc */
int optname, /* option name */void *optval, /* option value */
socklen_t *optlen); /* data length of option*/
int setsockopt(
int sockfd,int level,
int optname,
const void *optval,
socklen_t optlen);
8/13/2019 510_socket5
19/20
Some socket options
SO_BROADCAST - enable socket for sending broadcast
SO_KEEPALIVEregularly probes on inactive TCPconnection.
SO_LINGERlinger till all data is sent before returningfrom close()on a TCP connection.
SO_RCVBUF/ SO_SNDBUFsize of receive/sendbuffers on socket.
SO_RCVTIMEO/SO_SNDTIMEOplace timeout onreceive/send operation.
8/13/2019 510_socket5
20/20
fcntl()
int fcntl(int fd, int cmd, long arg);
Miscellaneous file control operationsNon-blocking I/O (O_NONBLOCK, F_SETFL)
Signal-driven I/O (O_ASYNC, F_SETFL)
Set socket owner (F_SETOWN)