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.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Sop, Fan Reference: Daniel Spangenberger Computer Networks PPT-4 Socket Programming.
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.
I/O Multiplexing: select and poll
Chapter 6 I/O Multiplexing: select and poll function.
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.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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;
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.
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
1 I/O Multiplexing Computer Network Programming. 2 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Computer Network Architecture and Programming
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.
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)
Sirak Kaewjamnong Computer Network Systems
Concurrent Servers April 25, 2002 Topics Limitations of iterative servers Process-based concurrent servers Threads-based concurrent servers Event-based.
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.
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
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
CMPT 471 Networking II Network Programming © Janice Regan,
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
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.
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
@Yuan Xue CS 283Computer Networks Spring 2013 Instructor: Yuan Xue.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
I/O Multiplexing.
Concurrent Servers December 7, 2000
Socket Option.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
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
TCP/IP Socket Programming in C
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:

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. Select e poll

I/O Multiplexing When a client is handling multiple descriptors, interactive input When TCP server handles listening sockets and other connected sockets When the servers accepts requests from both TCP and UDP. When a server handles multiple service or multiple protocols

I/O Blocking I/O Non-blocking I/O I/O multiplexing Signal driven I/O Asynchronous I/O

I/O Operation Two steps: Waiting for data to be ready Copying the data from the kernel to the process

Blocking I/O

Nonblocking I/O Does not put process into the sleep state Returns code EWOULDBLOCK Polling

Nonblocking I/O

Select

Signal driven I/O Request the kernel to notify with the signal SEGIO when event occurs Not non-blocking Recvfrom or main loop

Signal driven I/O

Asynchronous I/O Tells the kernel when start the operation Kernell notifies when operation is completed including copy from the ketnel to the buffer

E/S assíncrona

I/O Type

select Tell the kernel to wake up the process either when a set of events happen or when a timeout happens The type of descriptor can be specified (reading, writing or exception)

select #include <sys/select.h> #include <sys/time.h> int select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout); Returns: positive count of ready descriptors, 0 on timeout, –1 on error O readset, writeset e exceptset sao arrys ade bits e designam os descritores que devem ser verificados para leitura escrita e condicoes, Maxpd1 da o numero maximo de desciptors a serem testados struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }; a

select Three Possibilities of wait: Wait until descriptor is ready for I/O– null pointer specifies the timeout value Wait up to a fixed amount of time Do not wait beyond the time specified in timeval structure

select Two options for test of execution: Arrival of out-of band data The presenc of control status information to be read from master side pf pseudo terminal

select descriptor set (array of integers) – each bit corresponds to a signal Four macros: void FD_ZERO(fd_set *fdset); void FD_SET(int fd, fd_set *fdset); void FD_CLR(int fd, fd_set *fdset); int FD_ISSET(int fd, fd_set *fdset);

select Select modifies the descriptor set of readset, writeset, excepetion (value-result arguments) Set all bits and check at return

Descriptors ready Conditions for a descriptor to be ready for reading if one of the following 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. The option SO_RCVLOWAT allows to set this parameter The read-half of the connection is closed (TCP connecton received a FIN). A read operation on the socket will not block, returns zero

Descriptors ready The socket is a listening socket and the number of completed connections is nonzero. An accept on the listening socket will not block A socket error is pending. A read operation on the socket will not block and will return an error. These pending errors can be fetched and cleared by calling getsockopt

Descriptors ready Conditions for a descriptor to be ready for writing if one of the following conditions is true: The number of bytes 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 the socket is connected or it does not require a connections. The low-water marking can be set by using SO_SNDLOWAT The write half of the connection is closed. A write operation will generate SIGPIPE

Descriptors ready The write-half of the connection is closed. A write operation on the socket will generate a SIGPIPE A socket error is pending. A write operation will not block and will return an error A socket error is pending. A write operation will not block and will return an error. Calling getsockops for the SO_ERROR socket option will clear the error

Ready for select A UDP socket is always writable since no connection is required There is a limited number of descriptors per sockets

str_cli 1 #include "unp.h" 2 void 3 str_cli(FILE *fp, int sockfd) 4 { 4 { 5 char sendline[MAXLINE], recvline[MAXLINE]; 6 while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen(sockfd, sendline, strlen (sendline)); 8 if (Readline(sockfd, recvline, MAXLINE) == 0) 9 err_quit("str_cli:server terminated prematurely"); 10 Fputs(recvline, stdout); 11 } 12 }

str_cli

str_cli 1 #include "unp.h" 2 void 3 str_cli(FILE *fp, int sockfd) 4 { 5 int maxfdp1; 6 fd_set rset; 7 char sendline[MAXLINE], recvline[MAXLINE]; 8 FD_ZERO(&rset); 9 for ( ; ; ) { 10 FD_SET(fileno(fp), &rset); 11 FD_SET(sockfd, &rset); 12 maxfdp1 = max(fileno(fp), sockfd) + 1; 13 Select(maxfdp1, &rset, NULL, NULL, NULL); 14 if (FD_ISSET(sockfd, &rset)) { /* socket is readable */ 15 if (Readline(sockfd, recvline, MAXLINE) == 0) 16 err_quit("str_cli: server terminated prematurely"); 17 Fputs(recvline, stdout); 18 } 19 if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ 20 if (Fgets(sendline, MAXLINE, fp) == NULL) 21 return; /* all done */ 22 Writen(sockfd, sendline, strlen(sendline)); 23 } 24 } 25 } A versao anterior poderia ficar bloqueada no fget De 8 a 13 seta o selecet – so precisa verificar readability , fileno converte converte um ponteiro para um arquivo de i/O, calcula o maxino dos dois desciptors para argumento 14-18 se o socket estiver ready a linha echaoada e mostrada no output pr fputs 19-23 – se o sstandard input estiver readable, a linha e lida e escrita no socket

str_cli Function fileno converts a pointer to na I/O file into a pointer to descritors. Both select and poll work only on descriptors Descriptors for writing and for exceptions are set null Flow is driven by select and not by fget anymore

Stop and wait mode

Batch mode

Close Limitations of the close operation: close decrements the descriptor’s reference count and closes the socket only if the count reaches; close terminates both directions of data transfer, reading and writing. shutdown send a FIN segment Shutdown is useful quando se quer fechar em uma direcao paraum tipo de operacao nelson

shutdown

shutdown shutdown howto: #include <sys/socket.h> int shutdown(int sockfd, int howto); Returns: 0 if OK, –1 on error shutdown howto: SHUT_RD – the read-half of the connection is closed; no more read data and socket receive buffer content is discarded. SHUT_WR – the write-half is closed. Any data currently in the socket send buffer will be sent , followed by TCP’s normal termination sequence SHUT_RDWR both read and write are closed.

str_cli (final version) 3 void str_cli(FILE *fp, int sockfd) 4 { 5 int maxfdp1, stdineof; 6 fd_set rset; 7 char buf[MAXLINE]; 8 int n; 9 stdineof = 0; 10 FD_ZERO(&rset); 11 for ( ; ; ) { 12 if (stdineof == 0) 13 FD_SET(fileno(fp), &rset); 14 FD_SET(sockfd, &rset); 15 maxfdp1 = max(fileno(fp), sockfd) + 1; 16 Select(maxfdp1, &rset, NULL, NULL, NULL); 17 if (FD_ISSET(sockfd, &rset)) { /* socket is readable */ 18 if ( (n = Read(sockfd, buf, MAXLINE)) == 0) { 19 if (stdineof == 1) 20 return; /* normal termination */ else err_quit("str_cli: server terminated prematurely"); 23 } 24 Write(fileno(stdout), buf, n); 25 } 26 if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ 27 if ( (n = Read(fileno(fp), buf, MAXLINE)) == 0) { 28 stdineof = 1; 29 Shutdown(sockfd, SHUT_WR); /* send FIN */ 30 FD_CLR(fileno(fp), &rset); 31 continue; 32 } 33 Writen(sockfd, buf, n); 34 } 35 } 36 } str_cli (final version) A flag stdineof e uma nova flag inicializada em 0, testa readability on standard input sempre que esta variavel for zero (5 a 8) 16-24 se eof e encontrado esta e a terminmacao ormal. Caso contraio o servido\r terminou prematuramente 25-33 quando eof e encontrado a variavel flag e setada e shutdown e enviado comn argumento shut_wr

TCP Echo server (1) Tem-se comop read descriptor set , com os standard input, standard output e erro e um array de integer que contem os descritores dos sockets conectados

TCP Echo server (2) Um cliente fez uma conexao – descritor 4

TCP Echo server (3) Um segundo cliente fe uma conexao – descriptor 5

TCP echo server (4) O cliente com descriptor 4 fez um close

TCP echo server (5) 2 int 3 main(int argc, char **argv) 4 { 5 int i, maxi, maxfd, listenfd, connfd, sockfd; 6 int nready, client[FD_SETSIZE]; 7 ssize_t n; 8 fd_set rset, allset; 9 char buf[MAXLINE]; 10 socklen_t clilen; 11 struct sockaddr_in cliaddr, servaddr; 12 listenfd = Socket(AF_INET, SOCK_STREAM, 0); 13 bzero(&servaddr, sizeof(servaddr)); 14 servaddr.sin_family = AF_INET; 15 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 16 servaddr.sin_port = htons(SERV_PORT); 17 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 18 Listen(listenfd, LISTENQ); 19 maxfd = listenfd; /* initialize */ 20 maxi = -1; /* index into client[] array */ 21 for (i = 0; i < FD_SETSIZE; i++) 22 client[i] = -1; /* -1 indicates available entry */ 23 FD_ZERO(&allset); 24 FD_SET(listenfd, &allset); ... Parte ususla do codigo

TCP echo server (6) ... 25 for ( ; ; ) { 26 rset = allset; /* structure assignment */ 27 nready = Select(maxfd + 1, &rset, NULL, NULL, NULL); 28 if (FD_ISSET(listenfd, &rset)) { /* new client conn */ 29 clilen = sizeof(cliaddr); 30 connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); 31 for (i = 0; i < FD_SETSIZE; i++) 32 if (client[i] < 0) { 33 client[i] = connfd; /* save descriptor */ 34 break; 35 } 36 if (i == FD_SETSIZE) 37 err_quit("too many clients"); 38 FD_SET(connfd, &allset); /* add new descriptor to set */ 39 if (connfd > maxfd) 40 maxfd = connfd; /* for select */ 41 if (i > maxi) 42 maxi = i; /* max index in client[] array */ 43 if (--nready <= 0) 44 continue; /* no more readable descriptors */ 45 }*/ if */ 26-27 – select waits for something to happen; eithe the establishment of a new client connection of the arrival rate, a FIN or an RST onan existing connection 28-45 – if th elistening sock is readable, a new connection has been established. We call accept and update our data structure accordingly. We use the first unused entry in the client array to record the connected socket. The number of ready descriptors is decremented and if it is zero we can avoid the nbext for looop. Let us use the return value from select to avoid checking descriptors that are not ready.

TCP echo server (7) ... 46 for (i = 0; i <= maxi; i++) { /* check all clients for data */ 47 if ( (sockfd = client[i]) < 0) 48 continue; 49 if (FD_ISSET(sockfd, &rset)) { 50 if ( (n = Read(sockfd, buf, MAXLINE)) == 0) { 51 /* connection closed by client */ 52 Close(sockfd); 53 FD_CLR(sockfd, &allset); 54 client[i] = -1; 55 } else 56 Writen(sockfd, buf, n); 57 if (--nready <= 0) 58 break; /* no more readable descriptors */ 59 } 60 } 61 } /* end for */ 62 }/* end main */ 46-60 – a test is made for each existing client connection as to wheather or not its descriptor is in the descriptor set returned by select. If so, a line is read from the client and echoed back to the client. If the client closes the connection, readline returns 0 and the data structure is updated.

Denial of service Client sends a byte and go into sleep mode, server becomes blocked A server that accepts request from multiple server can not block given a request of a single client Potential solution: Use of non-blocking I/O Each client treated as individual thread of control Set timer to I/O operation

poll Similar to select but provide additional information #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 interest on fd */ short revents; /* events that occurred on fd */ }; Utiliza um array de agregados ao inves de arrays debits como no selsct Event envia a condicao e recebe responsta em revent Timeout diz quanto temo a funcao deve esperarantes de retornar

poll

poll Three types of data: TCP and UDP data are considered normal priority band High priority TCP and UDP data are considered normal TCP out-of-band is considered priority band New connection at listenning socket can be considered either as normal or as priority band 20-24 – we use the

TCP echo server (with poll) (1) 1 #include "unp.h" 2 #include <limits.h> /* for OPEN_MAX */ 3 int 4 main(int argc, char **argv) 5 { 6 int i, maxi, listenfd, connfd, sockfd; 7 int nready; 8 ssize_t n; 9 char buf[MAXLINE]; 10 socklen_t clilen; 11 struct pollfd client[OPEN_MAX]; 12 struct sockaddr_in cliaddr, servaddr; 13 listenfd = Socket(AF_INET, SOCK_STREAM, 0); 14 bzero(&servaddr, sizeof(servaddr)); 15 servaddr.sin_family = AF_INET; 16 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 17 servaddr.sin_port = htons(SERV_PORT); 18 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 19 Listen(listenfd, LISTENQ); 20 client[0].fd = listenfd; 21 client[0].events = POLLRDNORM; 22 for (i = 1; i < OPEN_MAX; i++) 23 client[i].fd = -1; /* -1 indicates available entry */ 24 maxi = 0; /* max index into client[] array */

TCP echo server (withpoll) (2) ... 25 for ( ; ; ) { 26 nready = Poll(client, maxi + 1, INFTIM); 27 if (client[0].revents & POLLRDNORM) { /* new client conn */ 28 clilen = sizeof(cliaddr); 29 connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); 30 for (i = 1; i < OPEN_MAX; i++) 31 if (client[i].fd < 0) { 32 client[i].fd = connfd; /* save descriptor */ 33 break; 34 } 35 if (i == OPEN_MAX) 36 err_quit("too many clients"); 37 client[i].events = POLLRDNORM; 38 if (i > maxi) 39 maxi = i; /* max index in client[] array */ 40 if (--nready <= 0) 41 continue; /* no more readable descriptors */ 42 }

TCP echo server (with poll) (3) 43 for (i = 1; i <= maxi; i++) { /* check all clients for data */ 44 if ( (sockfd = client[i].fd) < 0) 45 continue; 46 if (client[i].revents & (POLLRDNORM | POLLERR)) { 47 if ( (n = read(sockfd, buf, MAXLINE)) < 0) { 48 if (errno == ECONNRESET) { 49 /* connection reset by client */ 50 Close(sockfd); 51 client[i].fd = -1; 52 } else 53 err_sys("read error"); 54 } else if (n == 0) { 55 /* connection closed by client */ 56 Close(sockfd); 57 client[i].fd = -1; 58 } else 59 Writen(sockfd, buf, n); 60 if (--nready <= 0) 61 break; /* no more readable descriptors */ 62 } 63 } 64 } 65 }