I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 20061 CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.

Slides:



Advertisements
Similar presentations
Nonblocking I/O Blocking vs. non-blocking I/O
Advertisements

Programming with TCP – I
Out-of-Band Data© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
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
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.
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.
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;
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
TDC561 Network Programming Camelia Zlatea, PhD Week 3: Unix Asynchronous Events; Signals and Alarms API.
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 Nonblocking I/O Nonblocking reads and writes Buffers enabling overlapped nonblocking I/O Nonblocking connect.
Client Server Design Alternatives© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
1 I/O Multiplexing Computer Network Programming. 2 Input from multiple sources Process keyboard sockets file A process may have multiple sources of input.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Computer Network Architecture and Programming
Advanced UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
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.
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
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
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.
Chapter 2 Applications and Layered Architectures Sockets.
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.
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
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.
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.
Advanced Sockets API-II Vinayak Jagtap
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.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
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)
TCP Client-Server Example
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.
Ioctl Operations. ioctl Function Interface Configuration  Netstat, ifconfig command 에서 사용.
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.
More Project 1 and HW 1 stuff! Athula Balachandran Wolfgang Richter
I/O Multiplexing.
Socket Option.
Elementary UDP Sockets
CH5 TCP Client - Server Example:
Review: TCP Client-Server Interaction
Concurrent Servers Topics Limitations of iterative servers
Lecture 4 Socket Programming Issues
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Lecture 11 Overview.
Advanced Network Programming spring 2007
Socket Programming.
Elementary UDP Sockets connectionless, unreliable, datagram
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© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department Virginia Tech I/O Multiplexing

© Dr. Ayman Abdel-Hamid, CS4254 Spring Outline I/O Multiplexing (Chapter 6)  Introduction  I/O Models  Synchronous I/O versus Asynchronous I/O  select function  TCP echo client using select  Shutdown function  TCP Echo Server  TCP and UDP Echo Server using select (section 8.15)

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Introduction 1/2 TCP echo 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 sees FIN since the client is blocked reading from standard input We need 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, poll, or newer pselect functions)

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Introduction 2/2 Scenarios for I/O Multiplexing  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 Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring I/O Models Models  Blocking I/O  Nonblocking I/O  I/O multiplexing( select and poll )  Signal driven I/O ( SIGIO )  Asynchronous I/O Two distinct phases for an input operation  Waiting for the data to be ready (for a socket, wait for the data to arrive on the network, then copy into a buffer within the kernel)  Copying the data from the kernel to the process (from kernel buffer into application buffer)

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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 Blocking I/O

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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 recvfrom datagram ready System call Copy data from kernel to user Process repeatedly calls recvfrom waiting for an OK return (polling)

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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 blocks 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Asynchronous I/O application aio_read Signal handler Process datagram System call Deliver 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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 1st phase handled differently, 2nd phase handled the same handles both phases

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Synchronous I/O, Asynchronous I/O Synchronous I/O  causes 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring select function 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. What descriptors we are interested in (readable,writable, or exception condition) and how long to wait? #include int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *); //Returns: +ve count of ready descriptors, 0 on timeout, -1 on error struct timeval{ long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Possibilities for select function Wait forever : return only when descriptor (s) is ready (specify timeout argument as NULL) wait up to a fixed amount of time Do not wait at all : return immediately after checking the descriptors. Polling (specify timeout argument as pointing to a timeval structure where the timer value is 0) The wait is normally interrupted if the process catches a signal and returns from the signal handler  select might return an error of EINTR  Actual return value from function = -1

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring readset  descriptors for checking readable writeset  descriptors for checking writable exceptset  descriptors for checking exception conditions (2 exception conditions) 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 (Ignore) If you pass the 3 arguments as NULL, you have a high precision timer than the sleep function select function Descriptor Arguments

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Descriptor Sets Array of integers : each bit in each integer correspond to a descriptor ( fd_set ) 4 macros  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 ? */

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Example of Descriptor sets Macros 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_SET(5, &rset); /*turn on bit fd 5*/

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring specifies the number of descriptors to be tested. Its value is the maximum descriptor to be tested, plus one. (hence maxfdp1)  Descriptors 0, 1, 2, up through and including maxfdp1 -1 are tested  example: interested in fds 1,2, and 5  maxfdp1 = 6  Your code has to calculate the maxfdp1 value constant FD_SETSIZE defined by including  is the number of descriptors in the fd_set datatype. (often = 1024) maxfdp1 argument to select function

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Select modifies descriptor sets pointed to by readset, writeset, and exceptset pointers On function call  Specify value of descriptors that we are interested in On function return  Result indicates which descriptors are ready Use FD_ISSET macro on return to test a specific descriptor in an fd_set structure  Any descriptor not ready will have its bit cleared  You need to turn on all the bits in which you are interested on all the descriptor sets each time you call select Value-Result arguments in select function

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Condition for 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Recall section 5.5 (source code is lib/str_cli.c ) Problems with earlier version  could be blocked in the call to fgets when something happened on the socket  We need to be notified as soon s the server process terminates Alternatively  block in a call to select instead, waiting for either standard input or the socket to be readable. str_cli Function revisited

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Condition handled by select in str_cli Data of EOF client stdin Socket errorEOF RST TCP dataFIN Select for readability on either standard input or socket

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Conditions handled with the socket Peer TCP sends data  the socket becomes readable and read returns greater than 0 (number of bytes of data) Peer TCP sends a FIN (peer process terminates)  the socket become readable and read returns 0 (EOF) Peer TCP sends a RST (peer host has crashed and rebooted)  the socket become readable and returns -1  errno contains the specific error code  Source code in select/strcliselect01.c tested by select/tcpcli01.c  This version is OK for stop-an-wait mode (interactive input), will modify later for batch input and buffering

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring select -based str_cli function 1/2 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…..

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring if (FD_ISSET(sockfd, &rset)) {/* socket is readable */ if (Readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prem"); 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 select-based str_cli function 2/2

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring str_cli operates in a stop-and-wait mode  sends a line to the server and then waits for the reply Assume RTT of 8 units of time  only using one-eighth of the pipe capacity Ignore TCP ACKsrequestrequest serverrequest request reply reply reply reply client time1 time2 time3 time4 time5 time6 time7 time0 Batch Input and Buffering 1/2

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring With batch input, can send as fast as we can The problem with the 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. We need 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 Batch Input and Buffering 2/2

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Shutdown function 1/3 Close one half of the TCP connection  send FIN to server, but leave the socket descriptor open for reading Limitations with close function  decrements the descriptor’s reference count and closes the socket only if the count reaches 0 With shutdown, can initiate TCP normal connection termination regardless of the reference count  terminates both directions (reading and writing) With shutdown, we can tell other end that we are done sending, although that end might have more data to send us

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring Shutdown function 2/3 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

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring #include int shutdown ( int sockfd, int howto ); /* return : 0 if OK, -1 on error */ howto argument  SHUT_RD read-half of the connection closed Any data in receive buffer is discarded Any data received after this call is ACKed and then discarded  SHUT_WR write-half of the connection closed (half-close) Data in socket send buffer sent, followed by connection termination  SHUT_RDWR both closed Shutdown function 3/3

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring str_cli using select and shutdown 1/2 //Source code in select/ strcliselect02.c, test with select/tcpcli02.c #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…..

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring 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)); } } } str_cli using select and shutdown 2/2

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP echo server using select 1/5 Rewrite the server as a single process that uses select to handle any number of clients, instead of forking one child per client. Before first client has established a connection Client[] [0] [1] [2] [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 4 fd:0(stdin), 1(stdout), 2(stderr) fd:3  listening socket fd

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP echo server using select 2/5 After first client connection is established (assuming connected descriptor returned by accept is 4) Client[] [0] [1] [2] 4 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 5 fd:0(stdin), 1(stdout), 2(stderr) fd:3  listening socket fd fd:4  first connected fd 1 fd4

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP echo server using select 3/5 After second client connection is established (assuming connected descriptor returned by accept is 5) Client[] [0] [1] [2] 4 5 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 6 fd:0(stdin), 1(stdout), 2(stderr) fd:3  listening socket fd fd:4  first connected socket fd fd:5  second connected socket fd 1 fd4fd5 1

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP echo server using select 4/5 First client terminates its connection ( fd 4 readable and read returns 0  client TCP sent a FIN) Client[] [0] [1] [2] 5 [FD_SETSIZE -1] rset: fd0fd1fd2fd Maxfd + 1 = 6 fd:0(stdin), 1(stdout), 2(stderr) fd:3  listening socket fd fd:5  second connected socket fd 0 fd4fd5 1

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP echo server using select 5/5 As clients arrive, record connected socket descriptor in first available entry in client array (first entry = -1) Add connected socket to read descriptor set Keep track of  Highest index in client array that is currently in use  Maxfd +1 The limit on number of clients to be served Min ( FD_SETSIZE, Max ( Number of descriptors allowed for this process by the kernel)) Source code in tcpcliserv/tcpservselect01.c

I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring TCP and UDP echo server using select Section 8.15 Combine concurrent TCP echo server with iterative UDP server into a single server that uses select to multiplex a TCP and UDP socket Source code in udpcliserv/udpservselect01.c Source code for sig_chld function (signal handler) is in udpcliserv/sigchldpidwait.c  Handles termination of a child TCP server  See sections 5.8, 5.9, and 5.10