Chapter 6 I/O Multiplexing: select and poll function.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

I/O Models Satish Krishnan. I/O Models Blocking I/O Non-blocking I/O I/O Multiplexing Signal driven I/O Asynchronous I/O.
Мультиплексирование ввода/вывода Программирование с использованием POSIX thread library.
Returning to zombies and parents and children. –What happens to the children if the parent dies? /* ILLUSTRATING THAT UPON A PARENT KILL THE ZOMBIES /*
I/O Multiplexing: select and poll
I/O Multiplexing Road Map: 1. Motivation 2. Description of I/O multiplexing 3. Scenarios to use I/O multiplexing 4. I/O Models  Blocking I/O  Non-blocking.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Today’s topic Issues about sending structures with TCP. Server design alternatives: concurrent server and multiplexed server. I/O multiplexing.
Lecture 16 Overview. Creating a TCP socket int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); int mysock; struct sockaddr_in myaddr;
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
1 Data Communications and Networking Socket Programming Part II: Design of Server Software Reference: Internetworking with TCP/IP, Volume III Client-Server.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
1 Nonblocking I/O Nonblocking reads and writes Buffers enabling overlapped nonblocking I/O Nonblocking connect.
1 I/O Multiplexing Computer Network Programming. 2 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input.
Lecture 8 UDP Sockets & I/O Multiplexing
Select The select function determines the status of one or more sockets, waiting if necessary, to perform synchronous I/O. int select( int nfds, fd_set*
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
IP Multiplexing Ying Zhang EECS 489 W07.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Elementary TCP Sockets
1 Non-blocking I/O Computing Network Programming.
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Sirak Kaewjamnong Computer Network Systems
Elementary TCP Sockets
UNIX Network Programming1 Chapter 15. Nonblocking I/O.
Review: How to create a TCP end point? What is the right format for sin_port and sin_addr in the sockaddr_in data structure? How many different ways we.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
Chapter18 broadcasting. contents Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions.
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
1 I/O Multiplexing We often need to be able to monitor multiple descriptors:We often need to be able to monitor multiple descriptors: –a generic TCP client.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
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.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
I/O Multiplexing. 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,
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
Introduction A Simple Daytime Client A Simple Daytime Server
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
I/O Multiplexing Chap 6. I/O Models  Blocking I/O Model  Nonblocking I/O Model  I/O Multiplexing Model  Signal Driven I/O Model  Asynchronous I/O.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
I/O Multiplexing.
Socket Option.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
CH5 TCP Client - Server Example:
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Chapter 8 Elementary UDP Socket
Lecture 4 Socket Programming Issues
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Lecture 11 Overview.
TCP Sockets Programming
Advanced Network Programming spring 2007
Socket Programming.
Elementary UDP Sockets connectionless, unreliable, datagram
TCP Client-Server Example
I/O Multiplexing We often need to be able to monitor multiple descriptors: a generic TCP client (like telnet) need to be able to handle unexpected situations,
Presentation transcript:

Chapter 6 I/O Multiplexing: select and poll function

abstract n Introduction n I/O Models(5 종류 ) n Synchronous I/O versus Asynchronous I/O n select function n batch input n shutdown function n pselect function n poll function

Introduction n TCP client is handling two inputs at the same time: standard input and a TCP socket –when the client was blocked in a call to read, the server process was killed –server TCP sends FIN to the client TCP, but the client never see FIN since the client is blocked reading from standard input => the capability to tell the kernel that we want to be notified if one or more I/O conditions are ready. : I/O multiplexing (select and poll) : I/O multiplexing (select and poll)

n When: –client is handling multiple descriptors (interactive input and a network socket). –Client to handle multiple sockets(rare) –TCP server handles both a listening socket and its connected socket. –Server handle both TCP and UDP. –Server handles multiple services and multiple protocols

I/O Models n Blocking I/O n nonblocking I/O n I/O multiplexing(select and poll) n signal driven I/O(SIGIO) n asynchronous I/O(posix.1 aio_ functions) Two distinct phases for an input operations 1. Waiting for the data to be ready 1. Waiting for the data to be ready 2. Copying the data from the kernel to the process 2. Copying the data from the kernel to the process

Blocking I/O application recvfrom Process datagram System call Return OK No datagram ready Datagram ready copy datagram Copy complete kernel Process blocks in a call to recvfrom Wait for data Copy data from kernel to user

nonblocking I/O application recvfrom Process datagram System call Return OK No datagram ready copy datagram application kernel Wait for data EWOULDBLOCK recvfrom No datagram ready EWOULDBLOCK System call recvfromdatagram ready System call Copy data from kernel to user Process repeatedly call recvfrom wating for an OK return (polling)

I/O multiplexing(select and poll) application select Process datagram System call Return OK No datagram ready Datagram ready copy datagram Copy complete kernel Wait for data Return readable recvfrom Copy data from kernel to user Process block in a call to select waiting for one of possibly many sockets to become readable Process blocks while data copied into application buffer System call

signal driven I/O(SIGIO) application Establish SIGIO Process datagram System call Return OK Datagram ready copy datagram Copy complete kernel Wait for data Deliver SIGIO recvfrom Copy data from kernel to user Process continues executing Process blocks while data copied into application buffer Sigaction system call Return Signal handler

asynchronous I/O application aio_read Signal handler Process datagram System call Delever signal No datagram ready Datagram ready copy datagram Copy complete kernel Process continues executing Wait for data Copy data from kernel to user Return Specified in aio_read

Comparison of the I/O Models blocking nonblocking I/O multiplexing signal-driven I/O asynchronous I/O initiate complete check complete blocked check blocked ready initiate blocked complete notification initiate blocked complete initiate notification wait for data copy data from kernel to user ist phase handled differently, 2nd phase handled the same handles both phases

Synchronous I/O, Asynchronous I/O n Synchronous I/O : cause the requesting process to be blocked until that I/O operation (recvfrom) completes.( blocking, nonblocking, I/O multiplexing, signal-driven I/O ) n Asynchronous I/O : does not cause the requesting process to be blocked (asynchronous I/O) (asynchronous I/O)

Select function n Allows the process to instruct the kernel to wait for any one 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. n (readable,writable, expired time)

n #include n #include #include #include int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *); int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *); n struct timeval{ long tv_sec; /* seconds */ long tv_sec; /* seconds */ long tv_usec; /* microseconds */ long tv_usec; /* microseconds */ }

Condition of select function n Wait forever : return only descriptor is ready(timeval = NULL) n wait up to a fixed amount of time: n Do not wait at all : return immediately after checking the descriptors(timeval = 0) wait: normally interrupt if the process catches a signal and returns from the signal handler

n Readset => descriptor for checking readable n writeset => descriptor for checking writable n exceptset => descriptor for checking two exception conditions two exception conditions :arrival of out of band data for a socket :arrival of out of band data for a socket :the presence of control status information to be read from the master side of a pseudo terminal :the presence of control status information to be read from the master side of a pseudo terminal

Descriptor sets n Array of integers : each bit in each integer correspond to a descriptor. n fd_set: an array of integers, with each bit in each integer corresponding to a descriptor. n Void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */ n Void FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fdset */ n Void FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in fdset*/ n int FD_ISSET(int fd, fd_set *fdset);/* is the bit for fd on in fdset ? */

Example of Descriptor sets function 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_ST(5, &rset); /*turn on bit fd 5*/ FD_SET(5, &rset); /*turn on bit fd 5*/

n specifies the number of descriptors to be tested. n Its value is the maximum descriptor to be tested, plus one.(hence our name of maxfdp1)(example:fd1,2,5 => maxfdp1: 6) n constant FD_SETSIZE defined by including, is the number of descriptors in the fd_set datatype.(1024) Maxfdp1 argument

Condition that cause a socket to be ready for select ConditionReadable?writable? Exception? Data to read read-half of the connection closed new connection ready for listening socket Space available for writing write-half of the connection closed Pending error TCP out-of-band data

n could be blocked in the call to fgets when something happened on the socket n blocks in a call to select instead, waiting for either standard input or the socket to be readable. Condition handled by select in str_cli(section5.5)

Data of EOF client stdin Socket errorEOF RST TCP dataFIN Select for readability on either standard input or socket

Three conditions are handled with the socket n Peer TCP send a data,the socket becomr readable and read returns greater than 0 n Peer TCP send a FIN(peer process terminates), the socket become readable and read returns 0(end-of-file) n Peer TCP send a RST(peer host has crashed and rebooted), the socket become readable and returns -1 and errno contains the specific error code

Implimentation of str_cli function using select Void str_cli(FILE *fp, int sockfd) { intmaxfdp1; fd_setrset; charsendline[MAXLINE], recvline[MAXLINE]; FD_ZERO(&rset); for ( ; ; ) { FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockfd) + 1; Select(maxfdp1, &rset, NULL, NULL, NULL); Continue…..

if (FD_ISSET(sockfd, &rset)) {/* socket is readable */ if (Readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prematurely"); Fputs(recvline, stdout); } if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ if (Fgets(sendline, MAXLINE, fp) == NULL) return;/* all done */ Writen(sockfd, sendline, strlen(sendline)); } }//for }//str_cli

Stop and wait sends a line to the server and then waits for the reply request serverrequest serverreply client time1 time2 time3 time4 time5 time6 time7 time0

Batch input request8request7request6request5 reply1reply2reply3reply4 Time 7: request9request8request7request6 reply2reply3reply4reply5 Time 7:

n The problem with our revised str_cli function –After the handling of an end-of-file on input, the send function returns to the main function, that is, the program is terminated. –However, in batch mode, there are still other requests and replies in the pipe. n A way to close one-half of the TCP connection –send a FIN to the server, telling it we have finished sending data, but leave the socket descriptor open for reading <= shutdown function

Shutdown function n Close one half of the TCP connection (example:send FIN to server, but leave the socket descriptor open for reading) (example:send FIN to server, but leave the socket descriptor open for reading) Close function : decrements the descriptor’s reference count and closes the socket only if the count reaches 0, terminate both direction(reading and writing) Shutdown function : just one of them(reading or writing)

Calling shutdown to close half of a TCP connection clientserver data FIN Ack of data and FIN data FIN Ack of data and FIN Read returns > 0 Read returns 0 write close write shutdown Read returns > 0 Read returns 0

n #include n #include int shutdown(int sockfd, int howto); int shutdown(int sockfd, int howto); /* return : 0 if OK, -1 on error */ /* return : 0 if OK, -1 on error */ n howto argument SHUT_RD : read-half of the connection closed SHUT_RD : read-half of the connection closed SHUT_WR : write-half of the connection closed SHUT_WR : write-half of the connection closed SHUT_RDWR : both closed SHUT_RDWR : both closed

Str_cli function using select and shutdown #include"unp.h" void str_cli(FILE *fp, int sockfd) { intmaxfdp1, stdineof; fd_setrset; charsendline[MAXLINE], recvline[MAXLINE]; stdineof = 0; FD_ZERO(&rset); for ( ; ; ) { if (stdineof == 0) // select on standard input for readability FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockfd) + 1; Select(maxfdp1, &rset, NULL, NULL, NULL); Continue…..

if (FD_ISSET(sockfd, &rset)) {/* socket is readable */ if (Readline(sockfd, recvline, MAXLINE) == 0) { if (stdineof == 1) return;/* normal termination */ else err_quit("str_cli: server terminated prematurely"); } Fputs(recvline, stdout); } if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ if (Fgets(sendline, MAXLINE, fp) == NULL) { stdineof = 1; Shutdown(sockfd, SHUT_WR);/* send FIN */ FD_CLR(fileno(fp), &rset); continue; } Writen(sockfd, sendline, strlen(sendline)); }

TCP echo server n Rewrite the server as a single process that uses select to handle any number of clients, instead of forking one child per client.

Data structure TCP server(1) Client[] [0] [1] [2] [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 4 fd:0(stdin),1(stdout),2(stderr) fd:3 => listening socket fd Before first client has established a connection

Data structure TCP server(2) Client[] [0] [1] [2] 4 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 5 * fd3 => listening socket fd fd4 1 *fd4 => client socket fd After first client connection is established

Client[] [0] [1] [2] 4 5 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 6 * fd3 => listening socket fd fd4 1 * fd4 => client1 socket fd fd5 1 * fd5 => client2 socket fd Data structure TCP server(3) After second client connection is established

Data structure TCP server(4) Client[] [0] [1] [2] 5 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 6 * fd3 => listening socket fd fd4 0 * fd4 => client1 socket fd deleted fd5 1 * fd5 => client2 socket fd *Maxfd does not change After first client terminates its connection

TCP echo server using single process # include"unp.h" int main(int argc, char **argv) { inti, maxi, maxfd, listenfd, connfd, sockfd; intnready, client[FD_SETSIZE]; ssize_tn; fd_setrset, allset; charline[MAXLINE]; socklen_tclilen; struct sockaddr_incliaddr, servaddr; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ);

maxfd = listenfd;/* initialize */ maxi = -1;/* index into client[] array */ for (i = 0; i < FD_SETSIZE; i++) client[i] = -1;/* -1 indicates available entry */ FD_ZERO(&allset); FD_SET(listenfd, &allset); for ( ; ; ) { rset = allset;/* structure assignment */ nready = Select(maxfd+1, &rset, NULL, NULL, NULL); if (FD_ISSET(listenfd, &rset)) {/* new client connection */ clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); #ifdefNOTDEF printf("new client: %s, port %d\n", Inet_ntop(AF_INET, &cliaddr.sin_addr, 4, NULL), ntohs(cliaddr.sin_port)); #endif

for (i = 0; i < FD_SETSIZE; i++) if (client[i] < 0) { client[i] = connfd; /* save descriptor */ break; } if (i == FD_SETSIZE) err_quit("too many clients"); FD_SET(connfd, &allset);/* add new descriptor to set */ if (connfd > maxfd) maxfd = connfd;/* for select */ if (i > maxi) maxi = i;/* max index in client[] array */ if (--nready <= 0) continue;/* no more readable descriptors */ }

for (i = 0; i <= maxi; i++) {/* check all clients for data */ if ( (sockfd = client[i]) < 0) continue; if (FD_ISSET(sockfd, &rset)) { if ( (n = Readline(sockfd, line, MAXLINE)) == 0) { /*4connection closed by client */ Close(sockfd); FD_CLR(sockfd, &allset); client[i] = -1; } else Writen(sockfd, line, n); if (--nready <= 0) break;/* no more readable descriptors */ }

Denial of service attacks n If Malicious client connect to the server, send 1 byte of data(other than a newline), and then goes to sleep. =>call readline, server is blocked. =>call readline, server is blocked.

n Solution Þ use nonblocking I/O Þ have each client serviced by a separate thread of control (spawn a process or a thread to service each client) Þ place a timeout on the I/O operation

pselect function #include int pselect(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timespec *timeout, const sigset_t *sigmask) pselect function was invented by Posix.1g.

pselect function n struct timespec{ time_t tv_sec; /*seconds*/ time_t tv_sec; /*seconds*/ long tv_nsec; /* nanoseconds */ long tv_nsec; /* nanoseconds */ n sigmask => pointer to a signal mask.

Poll function n Similar to select, but provide additional information when dealing with streams devices n #include n #include int poll(struct pollfd *fdarray, unsigned long nfds, int timeout); int poll(struct pollfd *fdarray, unsigned long nfds, int timeout); /*return : count of ready descriptors, 0 on timeout, -1 on error*/

n Struct pollfd{ int fd; /* descriptor to check */ int fd; /* descriptor to check */ short events; /* events of interest on fd */ short events; /* events of interest on fd */ short revents;/*events that occurred on fd*/ short revents;/*events that occurred on fd*/ } specifies the conditions to be tested for a given descriptor fd specifies the conditions to be tested for a given descriptor fd events: the conditions to be tested events: the conditions to be tested revents:the status of that descriptor revents:the status of that descriptor

Input events and returned revents for poll Constant Input to events ? Result from revents ? Description POLLIN POLLRDNORM POLLRDBAND POLLPRI Normal or priority band data can be read normal data can be read priority band data can be read high-priority data can be read POLLOUT POLLWRNORM POLLWRBAND POLLERR POLLHUP POLLNVAL normal data can be written priority band data can be written An error has occurred hangup has occurred descriptor is not an open file

Timeout value for poll Timeout valueDescription INFTIM 0 >0 Wait forever Return immediately, do not block Wait specified number of milliseconds If we are no longer interested in particular descriptor, just set the fd member of the pollfd structure Specifies how long the function is to wait before returning