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.

Slides:



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

Nonblocking I/O Blocking vs. non-blocking I/O
Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
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.
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.
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.
TDC561 Network Programming Camelia Zlatea, PhD Week 3: Unix Asynchronous Events; Signals and Alarms API.
Advanced Sockets Amit Mondal TA, Intro to Networking Jan 22, 2009 Recital 3 Introduction to Networking Instructor: Prof. Aleksandar Kuzmanovic.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
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.
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 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*
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.
Elementary TCP Sockets
1 Non-blocking I/O Computing Network Programming.
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Socket Models Different ways to manage your connections.
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 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.
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.
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.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
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,
TELE 402 Lecture 12: Signal-Driven I/O & Raw Socket 1 Overview Last Lecture –Advanced UDP sockets and threads –Source: Chapters 22&26 of Stevens’ book.
CMPT 471 Networking II Network Programming © Janice Regan,
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
回到第一頁 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.
Socket Option.
Elementary UDP Sockets
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Concurrent Servers Topics Limitations of iterative servers
Advanced Sockets Introduction to Networking
Lecture 4 Socket Programming Issues
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
Lecture 11 Overview.
TCP Sockets Programming
Operating Systems Chapter 5: Input/Output Management
Socket Programming.
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:

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 multiplexing –Source: Chapter 6 of Stevens’ book Next Lecture –Socket options and UDP socket –Source: Chapters 7&8 of Stevens’ book

TELE 402 Lecture 4: I/O multi … 2 Problems from Last Time Client could be blocked in fgets and miss data from readline. Sending and receiving data should be independent.

TELE 402 Lecture 4: I/O multi … 3 I/O Multiplexing 1 What is I/O multiplexing? –The capacity to tell the kernel that we want to be notified if one or more I/O conditions are ready (e.g. input is ready to be read, or the buffer is capable of taking more output) –Provided by select and poll functions

TELE 402 Lecture 4: I/O multi … 4 I/O Multiplexing 2 Scenarios for I/O multiplexing in C/S –A client handles multiple descriptors, or sockets –A server handles both a listening socket and its connected sockets –A server handles both TCP and UDP –A server handles multiple services and protocols (e.g. the inetd daemon) –It is possible, but rare, for a client to handle multiple sockets at the same time.

TELE 402 Lecture 4: I/O multi … 5 I/O Models There are five I/O models under Unix –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 –Copying the data from the kernel to the process Definition of “data being ready” –For UDP, an entire datagram has been received –For TCP, data received passed the low-water mark

TELE 402 Lecture 4: I/O multi … 6 Blocking I/O Process is put to sleep if blocked

TELE 402 Lecture 4: I/O multi … 7 Nonblocking I/O When an I/O cannot be completed, the process is not put to sleep, but returns with an error (EWOULDBLOCK) Waste of CPU time

TELE 402 Lecture 4: I/O multi … 8 I/O Multiplexing Use select or poll to report if some descriptor is readable or writable. select may be blocked if no descriptor is readable or writable.

TELE 402 Lecture 4: I/O multi … 9 Signal driven I/O If a descriptor is ready, notify the process with the SIGIO signal

TELE 402 Lecture 4: I/O multi … 10 Asynchronous I/O The process initiates an I/O operation. When it is complete, the process is notified.

TELE 402 Lecture 4: I/O multi … 11 Comparison of I/O models The first four are synchronous I/O.

TELE 402 Lecture 4: I/O multi … 12 Synchronous I/O vs Asynchronous I/O POSIX definition: –A synchronous I/O operation causes the requesting process to be blocked until that I/O operation completes. –An asynchronous I/O operation does not cause the requesting process to be blocked.

TELE 402 Lecture 4: I/O multi … 13 select 1 select function –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 –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

TELE 402 Lecture 4: I/O multi … 14 select 2 Examples of when to use select –Wait for descriptors {1,4,5} are ready for reading –Wait for descriptors {2,7} are ready for writing –Wait for descriptors {1,4} have an exception condition pending –Wait for 10.2 seconds

TELE 402 Lecture 4: I/O multi … 15 select 3 struct timeval { long tv_sec; long tv_usec; } Three ways for timeout –Wait forever: return only when one of the specified descriptors is ready. The timeout argument is specified as NULL –Wait up to a fixed time: return when one of the specified descriptors is ready, but don’t wait beyond the time specified by timeout. –Don’t wait at all: return immediately after checking the descriptors. The two elements of timeout is specified as both 0.

TELE 402 Lecture 4: I/O multi … 16 select 4 The wait during select can be interrupted by signals (first two ways) Exception conditions –The arrival of out-of-band data

TELE 402 Lecture 4: I/O multi … 17 select 5 The middle three arguments specify the descriptors we want the kernel to test. They are: –readset –writeset –exceptset They are value-result arguments. (most common error) On return, the result indicates the descriptors that are ready.

TELE 402 Lecture 4: I/O multi … 18 select 6 Macros for fd_set datatype –FD_ZERO(fd_set *fdset); // clear all bits in fdset –FD_SET(int fd, fd_set *fdset); // turn on the bit for fd in fdset –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?

TELE 402 Lecture 4: I/O multi … 19 select 7 maxfdp1 specifies the number of descriptors to be tested. Its value is the maximum descriptor to be tested, plus 1. (most common error) Maximum number of descriptors: 256?, 1024 (Linux)?

TELE 402 Lecture 4: I/O multi … 20 Conditions for Readiness 1 A socket is ready for reading if any of the following conditions is true: –Data received in buffer greater than or equal to the low- water mark –Read-half of the connection is closed –A listening socket with nonzero number of connections –A socket error is pending

TELE 402 Lecture 4: I/O multi … 21 Conditions for Readiness 2 A socket is ready for writing if any of the following conditions is true: –Available space in the socket send buffer is greater than the low-water mark and the socket is connected or does not require a connection (UDP) –The write-half of the connection is closed (SIGPIPE) –A socket using a non-blocking connect has completed the connection, or the connect has failed –A socket error is pending A socket has an exception condition pending if there exists out-of-band data for the socket.

TELE 402 Lecture 4: I/O multi … 22 Summary of conditions

TELE 402 Lecture 4: I/O multi … 23 Revised str_cli 1 Three conditions for socket –If peer TCP sends data, socket becomes readable and read returns greater than 0 –If peer TCP sends a FIN, the socket becomes readable and read returns 0 (EOF) –If peer TCP sends RST, socket becomes readable and read returns -1, and errno contains specific error code.

TELE 402 Lecture 4: I/O multi … 24 Revised str_cli 2

TELE 402 Lecture 4: I/O multi … 25 Revised str_cli 3 int maxfdp1; fd_set rset; 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);

TELE 402 Lecture 4: I/O multi … 26 Revised str_cli 4 if (FD_ISSET(sockfd, &rset)) { if (readline(sockfd, recvline, MAXLINE)==0) err_quit(“str_cli-server term premature”); fputs(recvline, stdout); } if (FD_ISSET(fileno(fp), &rset)) { if (fgets(fileno(fp), MAXLINE, fp)==NULL) return; writen(sockfd, sendline, strlen(sendline)); }

TELE 402 Lecture 4: I/O multi … 27 Batch Input and Buffering 1 Still have a stop and wait Network between client and server is full- duplex Fig shows only1/8 of capacity used assuming RTT of 8 units.

TELE 402 Lecture 4: I/O multi … 28 Batch Input and Buffering 2

TELE 402 Lecture 4: I/O multi … 29 Batch Input and Buffering 3 Consider redirecting input and output to a file. Batch mode On EOF from file, str_cli returns to main client function which terminates.

TELE 402 Lecture 4: I/O multi … 30 Batch Input and Buffering 4

TELE 402 Lecture 4: I/O multi … 31 shutdown 1 Normal way to terminate a network connection is to call close There are two limitations with close –close decrements the descriptor’s reference count and closes the socket only if count reaches 0. –close terminates both directions of data transfer, reading and writing.

TELE 402 Lecture 4: I/O multi … 32 shutdown 2

TELE 402 Lecture 4: I/O multi … 33 shutdown 3 shutdown function –Can initiate connection termination sequence regardless of the reference count –Can only terminate one direction of data transfer –int shutdown(int sockfd, int howto) –Returns: 0 if OK, -1 on error –howto: SHUT_RD, SHUT_WR, SHUT_RDWR

TELE 402 Lecture 4: I/O multi … 34 str_cli (again) 1 void str_cli (FILE *fp, int sockfd) { int maxfdp1, stdlineof; fd_set rset; char buf[MAXLINE]; int n; stdlineof = 0; FD_ZERO(&rset);

TELE 402 Lecture 4: I/O multi … 35 str_cli (again) 2 for ( ; ; ) { if (stdlineof == 0) FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockdf) + 1; select(maxfdp1, &rset, NULL, NULL, NULL); // deal with socket // deal with file } } // end of str_cli

TELE 402 Lecture 4: I/O multi … 36 str_cli (again) 3 // deal with socket if (FD_ISSET(sockdf, &rset) { if ( (n=read(sockfd, buf, MAXLINE)) == 0) { if (stdlineof == 1) return; else err_quit(“str_cli: server terminated prematurely”); } write(fileno(stdout), buf, n); }

TELE 402 Lecture 4: I/O multi … 37 str_cli (again) 4 // deal with file if (FD_SET(fileno(fp), &rset)) { if ( (n=read(fileno(fp), buf, MAXLINE)) == 0) { stdlineof = 1; shutdown(sockfd, SHUT_WR); // send FIN FD_CLR(fileno(fp), &rset); continue; } writen(sockfd, buf, n); }

TELE 402 Lecture 4: I/O multi … 38 select-based Server Need to keep track of each client and its descriptor (array) Need to keep track of the highest used descriptor Good for many short lived clients

TELE 402 Lecture 4: I/O multi … 39 pselect and poll pselect contains two changes from the normal select –Time structure use nanosecond, instead of microsecond –Add a sixth argument: a pointer to a signal mask. This allows pselect set its own mask to disable delivery of signals poll –Provides functionality similar to select, but poll provides additional information when dealing with stream devices