Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 6 I/O Multiplexing: select and poll function."— Presentation transcript:

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

2 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

3 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)

4 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

5 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

6 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

7 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)

8 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

9 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

10 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

11 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

12 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)

13 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)

14 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 */ }

15 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

16 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

17 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 ? */

18 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*/

19 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

20 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

21 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)

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

23 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

24 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…..

25 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

26 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

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

28 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

29 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)

30 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

31 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

32 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…..

33 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)); }

34 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.

35 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

36 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

37 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

38 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

39 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);

40 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

41 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 */ }

42 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 */ }

43 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.

44 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

45 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.

46 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.

47 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*/

48 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

49 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

50 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

51

52


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

Similar presentations


Ads by Google