Np unit iv i
Upcoming SlideShare
Loading in...5
×
 

Np unit iv i

on

  • 423 views

 

Statistics

Views

Total Views
423
Views on SlideShare
416
Embed Views
7

Actions

Likes
0
Downloads
8
Comments
0

2 Embeds 7

http://vamsinp.blogspot.in 6
http://vamsinp.blogspot.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Np unit iv i Np unit iv i Presentation Transcript

  • I/O Multiplexing:select and poll function
  • abstractIntroductionI/O ModelsSynchronous I/O versus Asynchronous I/Oselect functionshutdown functionpselect functionpoll function
  • IntroductionTCP client is handling two inputs : standardinput 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)
  • 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 Blocking I/O Nonblocking I/O I/O multiplexing(select and poll) signal driven I/O(SIGIO) asynchronous I/O(posix.1 aio_ functions)Two distinct phases for an input operations 1. Waiting for the data to be ready 2. Copying the data from the kernel to the process
  • Blocking I/O application kernel System call recvfrom No datagram ready Wait for dataProcess blocksin a call to Datagram readyrecvfrom Copy data copy datagram from kernel to user Return OK Process Copy complete datagram
  • nonblocking I/O application kernel System call recvfrom No datagram ready EWOULDBLOCK System call recvfrom No datagram ready EWOULDBLOCK Wait for dataProcess System call repeatedly recvfrom datagram readycall recvfrom copy datagramwating for anOK return Copy data(polling) from Return OK kernel to user Process application datagram
  • I/O multiplexing(select and poll) application kernel System callProcess block select No datagram readyin a call toselect waiting Wait forfor one of datapossibly many Return readablesockets to Datagram readybecome readable System call recvfrom copy datagramProcess blocks Copy datawhile data from kernelcopied Return OK to userinto application Process Copy completebuffer datagram
  • We first enable the socket for signal-driven I/O and install a signal handlerusing the sigaction system call.The return from this system call is immediate and our process continues; it isnot blocked.When the datagram is ready to be read, the SIGIO signal is generated for ourprocess. We can either read the datagram from the signal handler by callingrecvfrom and then notify the main loop that the data is ready to be processedor we can notify the main loop and let it read the datagram.Advantage: we are not blocked while waiting for the datagram to arrive.The main loop can continue executing and just wait to be notified by thesignal handler that either the data is ready to process or the datagram is readyto be read.
  • signal driven I/O(SIGIO) application kernel Sigaction system call Establish SIGIOProcesscontinues Signal handlerexecuting Return Wait for data Deliver SIGIO Signal handler Datagram ready System call copy datagram recvfrom Copy dataProcess blockswhile data from kernelcopied Return OK to userinto application Process Copy completebuffer datagram
  • asynchronous I/OWe call aio_read and pass the kernel the descriptor, buffer pointer, buffer size ,fileoffset, and how to notify us when the entire operation is complete.This system call returns immediately and our process is not blocked while waitingfor the I/O to complete.We assume in this example that we ask the kernel to generate some signal whenthe operation is complete.This signal is not generated until the data has been copied into our applicationbuffer, which is different from the signal-driven I/O model.Signal-driven I/O , the kernel tells us when an I/O operation can be initiated, butwith asynchronous I/O, the kernel tells us when an I/O operation is complete.
  • asynchronous I/O application kernel System call aio_read No datagram ready Return Wait for dataProcesscontinues Datagram readyexecuting copy datagram Copy data from kernel to user Signal Deliver signal handler Copy complete Process Specified in aio_read datagram
  • Comparison of the I/O Models I/O signal-driven asynchronousblocking nonblocking multiplexing I/O I/Oinitiate check check initiate check check blocked wait for check data check check ready notification initiate initiate blocked blocked blocked copy data from kernelcomplete complete complete complete notification to user 1st phase handled differently, handles both phases 2nd phase handled the same
  • Synchronous I/O , Asynchronous I/O 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) Asynchronous I/O : does not cause the requesting process to be blocked (asynchronous I/O)
  • Select functionAllows the process to instruct the kernel to wait for any one ofmultiple events to occur and to wake up the process only whenone or more of these events occurs or when a specified amountof time has passed.(readable ,writable , expired time)
  • #include <sys/select.h> #include <sys/time.h> int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *);Returns: positive count of ready descriptors, 0 on timeout, –1 on error struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }
  • Condition of select function Wait forever : return only descriptor is ready(timeval = NULL) wait up to a fixed amount of time: 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
  • Readset => descriptor for checking readablewriteset => descriptor for checking writableexceptset => descriptor for checkingtwo exception conditions:arrival of out of band data for a socket:the presence of control status information to beread from the master side of a pseudo terminal
  • Descriptor setsArray of integers : each bit in each integercorrespond to a descriptor.fd_set: an array of integers, with each bit in each integer corresponding to adescriptor.Void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */Void FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fdset */Void FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in fdset*/int FD_ISSET(int fd, fd_set *fdset);/* is the bit for fd on in fdset ? */
  • Example of Descriptor sets functionfd_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*/FD_S
  • Maxfdp1 argumentspecifies the number of descriptors to betested.Its value is the maximum descriptor to betested, plus one.(hence our name ofmaxfdp1)(example:fd1,2,5 => maxfdp1: 6)constant FD_SETSIZE defined byincluding <sys/select.h>, is the number ofdescriptors in the fd_set datatype.(1024)
  • socket is ready for reading : any one conditions is true– The number of bytes of data in the socket receive buffer is greater than or equal to the current size of the low-water mark for the socket receive buffer. A read operation on the socket will not block and will return a value greater than 0. Set low-water mark : SO_RCVLOWAT socket option (defaults 1 :TCP and UDP)– The read half of the connection is closed.– The socket is a listening socket and the number of completed connections is nonzero.– A socket error is pending. A read operation on the socket will not block and will return an error (–1) with errno set to the specific error condition. These pending errors can also be fetched and cleared by calling getsockopt and specifying the SO_ERROR socket option.
  • socket is ready for writing : any one conditions is true – The number of bytes of available space in the socket send buffer is greater than or equal to the current size of the low-water mark for the socket send buffer and either: (i) the socket is connected (ii) the socket does not require a connection This means that if we set the socket to nonblocking, a write operation will not block and will return a positive value. We can set this low-water mark using the SO_SNDLOWAT socket option. low- water mark defaults to 2048 for TCP and UDP. – The write half of the connection is closed. – A socket using a non-blocking connect has completed the connection. – A socket error is pending. A write operation on the socket will not block and will return an error (–1) with errno set to the specific error condition. These pending errors can also be fetched and cleared by calling getsockopt with the SO_ERROR socket option.
  • A socket has an exception condition pending if there is out-of-band data forthe socket or the socket is still at the out-of-band mark.The purpose of the receive and send low-water marks is to give the applicationcontrol overhow much data must be available for readinghow much space must be available for writingFor example, if we know that our application has nothing productive to dounless at least 64 bytes of data are present, we can set the receive low-watermark to 64 to prevent select from waking us up if less than 64 bytes are readyfor reading.
  • Condition that cause a socket to be ready for select Condition Readable? 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 •
  • Condition handled by select in str_clicould be blocked in the call to fgets whensomething happened on the socketblocks in a call to select instead, waiting foreither standard input or the socket to bereadable.
  • Condition handled by select in str_cli clientData of EOF Select for readability • stdin on either standard Socket input or socket • error EOF TCP RST data FIN
  • Three conditions are handled with the socket Peer TCP send a data, the socket become readable and read returns greater than 0 Peer TCP send a FIN(peer process terminates), the socket become readable and read returns 0(end-of-file) 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 selectVoid str_cli(FILE *fp, int sockfd){ int maxfdp1; fd_set rset; char sendline[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 terminatedprematurely"); 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
  • client request time0 time1 requestStop and waitsends a line to the servertime2 requestand then waits for the reply time3 request server time4 reply server time5 reply time6 reply time7 reply
  • Batch inputTime 7: request8 request7 request6 request5 reply1 reply2 reply3 reply4Time 7: request9 request8 request7 request6 reply2 reply3 reply4 reply5
  • 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.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 Close one half of the TCP connection(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 client server data write write data Read returns > 0 shutdown FIN Read returns > 0 Read returns 0 Ack of data and FIN data write writeRead returns > 0 data closeRead returns > 0 FINRead returns 0 Ack of data and FIN
  • #include<sys/socket.h>int shutdown(int sockfd, int howto);/* return : 0 if OK, -1 on error */howto argumentSHUT_RD : read-half of the connection closedSHUT_WR : write-half of the connection closedSHUT_RDWR : both closed
  • Str_cli function using select and shutdown#include "unp.h"void str_cli(FILE *fp, int sockfd){ int maxfdp1, stdineof; fd_set rset; char sendline[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 serverRewrite the server as a single process that usesselect to handle any number of clients, insteadof forking one child per client.
  • Data structure TCP server(1) Before first client has established a connection Client[] fd0 fd1 fd2 fd3 [0] -1 rset: 0 0 0 1 [1] -1 [2] -1 Maxfd + 1 = 4 fd:0(stdin),1(stdout),2(stderr)[FD_SETSIZE -1] -1 fd:3 => listening socket fd
  • Data structure TCP server(2) After first client connection is established Client[] fd0 fd1 fd2 fd3 fd4 [0] 4 rset: 0 0 0 1 1 [1] -1 [2] -1 Maxfd + 1 = 5 * fd3 => listening socket fd[FD_SETSIZE -1] -1 *fd4 => client socket fd
  • Data structure TCP server(3) After second client connection is established Client[] fd0 fd1 fd2 fd3 fd4 fd5 [0] 4 rset: 0 0 0 1 1 1 [1] 5 [2] -1 Maxfd + 1 = 6 * fd3 => listening socket fd[FD_SETSIZE -1] -1 * fd4 => client1 socket fd * fd5 => client2 socket fd
  • Data structure TCP server(4) After first client terminates its connection Client[] fd0 fd1 fd2 fd3 fd4 fd5 [0] -1 rset: 0 0 0 1 0 1 [1] 5 [2] -1 Maxfd + 1 = 6 *Maxfd does not change * fd3 => listening socket fd[FD_SETSIZE -1] -1 * fd4 => client1 socket fd deleted * fd5 => client2 socket fd
  • TCP echo server using single process#include"unp.h"int main(int argc, char **argv){ int i, maxi, maxfd, listenfd, connfd, sockfd; int nready, client[FD_SETSIZE]; ssize_t n; fd_set rset, allset; char line[MAXLINE]; socklen_t clilen; struct sockaddr_in cliaddr, 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);
  • 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 = Read(sockfd, line, MAXLINE)) == 0) { /*4connection closed by client */ Close(sockfd); FD_CLR(sockfd, &allset); client[i] = -1; } else write(sockfd, line, n); if (--nready <= 0) break; /* no more readable descriptors */ } }}
  • Denial of service attacksIf 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.
  • Solutionuse nonblocking I/Ohave each client serviced by a separate thread ofcontrol (spawn a process or a thread to serviceeach client)place a timeout on the I/O operation
  • Poll function Similar to select, but provide additional information when dealing with streams devices #include <poll.h> int poll(struct pollfd *fdarray, unsigned long nfds, int timeout);/*return : 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*/}specifies the conditions to be tested for a givendescriptor fdevents: the conditions to be testedrevents:the status of that descriptor
  • Input events and returned revents for poll Input to Result from Constant Description events ? revents ?POLLIN • • Normal or priority band data can be readPOLLRDNORM • • normal data can be readPOLLRDBAND • • priority band data can be readPOLLPRI • • high-priority data can be readPOLLOUT • • normal data can be writtenPOLLWRNORM • • normal data can be writtenPOLLWRBAND • • priority band data can be writtenPOLLERR • An error has occurred hangup has occurredPOLLHUP •POLLNVAL descriptor is not an open file •
  • Timeout value for pollSpecifies how long the function is to wait before returning Timeout value Description INFTIM Wait forever 0 Return immediately, do not block >0 Wait specified number of milliseconds If we are no longer interested in particular descriptor, just set the fd member of the pollfd structure