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.

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
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.
Chapter 6 I/O Multiplexing: select and poll function.
1 Select and poll and Signals CS 241 April 6, 2012 University of Illinois.
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.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
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.
Event-Driven Programming Vivek Pai Dec 5, GedankenBits  What does a raw bit cost?  IDE  40GB: $100  120GB: $180  32MB USB Pen: $38  FireWire:
Operating Systems Course Hebrew University Spring 2007 Signals & User Thread.
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.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
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.
Signal-Driven I/O Concepts and steps for using signal-driven I/O
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Socket programming. Sockets are a protocol independent method of creating a connection between processes. Sockets can be either: connection based or connectionless:
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 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.
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.
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.
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,
Cs423-cotter1 Concurrency Issues in Client/Server Applications Chapters 15,16, 28.
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.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
UNIX signals & pipes. UNIX Signals A UNIX signal corresponds to an event –It is raised by one process (or hardware) to call another process’s attention.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 14 Threads 2 Read Ch.
PA3: Improving Performance with I/O Multiplexing Part 1-1: Nov. 7, Part 1-2: Nov. 10 Part 2-1: Nov. 17, Part 2-2: Nov.20.
Events in General. Agenda Post/wait technique I/O multiplexing Asynchronous I/O Signal-driven I/O Database events Publish/subscribe model Local vs. distributed.
Single Process, Concurrent, Connection-Oriented Servers (TCP) (Chapter 12)
TCP Client-Server Example
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
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.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
CSCI 330 UNIX and Network Programming Unit XVII: Socket Programming Detail.
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 에서 사용.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
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.
I/O Multiplexing.
Socket Option.
Review: TCP Client-Server Interaction
Imam Ahmad Trinugroho, ST., MMSI
Sockets, part 2 (Addresses, poll())
Concurrent Servers Topics Limitations of iterative servers
Advanced Sockets Introduction to Networking
Lecture 4 Socket Programming Issues
Lecture 11 Overview.
Socket Programming.
Advanced UNIX 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:

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 I/O  I/O multiplexing ( select(..) and poll(..) functions)  Signal driven I/O  Asynchronous I/O 5. Comparison of models

Motivation  Read/write from multiple descriptors?  What if data arrive from both fd1 & fd2 ? If you do recv(fd1,..), then you blocked until something arrives. If something arrives at fd2, you have no idea! Ex: Web client, download accelerator, proxies.  Solution : I/O multiplexing

Description of I/O multiplexing We need  to check both fd1 & fd2  receive from the one which has data.  This is called I/O multiplexing.  Achieved by select and poll fcns.

Scenarios to use I/O multiplexing 1. Client handle multiple descriptors  Ex: interactive input & network socket 2. Client handle multiple sockets  Web client, download accelerator, proxies 3. Server handles both TCP & UDP  DNS listens both TCP53 & UDP53 4. Server handles multiple services or protocols

A note..  These techniques are not limited to network programming.  I/O multiplexing is also used by  File I/O  Interactive I/O  Network I/O

Blocking I/O ApplicationKernel recvfrom system call no ready datagram datagram ready copy datagram copy complete wait for data here copy data from kernel to user return OK process datagram Application blocks here Now let’s talk about this method.  Do you think it is a good approach?  What are pros and cons?

Non-blocking I/O ApplicationKernel recvfrom system call no ready datagram EWOULDBLOCK recvfrom system call no ready datagram EWOULDBLOCK recvfrom system call datagram ready copy datagram process datagram return OK copy complete copy data from kernel to user Process repeatedly calls recvfrom waiting for an OK (polling) wait for data here

Non-blocking I/O cont. Requires socket to be set non-blocking mode. fcntl (stands for file control) sets a socket to non-blocking mode Example: UdpNonblockingIO.c #include int fcntl(int fd, int cmd,...)  returns: depends on cmd if OK, -1 on error.

I/O multiplexing model ApplicationKernel select system call no ready datagram recvfrom return readable system call process datagram return OK copy complete datagram ready copy datagram copy data from kernel to user wait for data here Process blocks while data is copied from kernel Process blocks in select waiting for one of possibly many sockets to become readable

select System Call select allows the process to instruct the kernel to wait for any 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. #include int select(int maxfd, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *timeout)  returns: positive count of descriptors, 0 on timeout, -1 on error. struct timeval{ longtv_sec; /* seconds */ long tv_usec; /* milliseconds */ };

select cont. 3 possibilities of timeout 1. Wait forever: NULL timeout argument. 2. Wait up to a fixed amount of time. 3. No wait at all: Return immediately after checking the descriptors. This is called “polling”. both tv_sec & tv_usec are set to “0”.

An example We can call select and tell the kernel to return only when any of the descriptors in the set {1,4,5} are ready for reading. {2,7} are ready for writing. {2,4,5} have an exception seconds have elapsed. Tell the kernel what descriptors we are interested in (for reading, writing, or an exception condition) and how long to wait.

How to specify descriptor set? There are fuctions to set, clear, and query on a descriptor set. Example: selectEx.c void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */ void FD_SET(int fd, fd_set *fdset); /* turn on bits */ void FD_CLR(int fd, fd_set *fdset); /* turn off bits */ int FD_ISSET(int fd, fd_set *fdset); /* is the bit for fd on in fdset? */ fd_set rset; FD_ZERO(&rset); FD_SET(1, &rset); FD_SET(4, &rset); FD_CLR(5, &rset);

poll System Call Very similar to select. fdarray is a pointer to the first element of an array of structures. Each element in the array is a pollfd structure that specifies the conditions to be tested for a given descriptor, fd. Most important events: POLLIN, POLLOUT. #include 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 */ shortevents; /* events of interests on fd */ short revents; /* events occured on fd */ };

Signal driven I/O  A signal is a notification to a process that an event has occured.  They are asynchronous. Example:  Pressing ^C while program running.  Child process terminates and kernel sends SIGCHLD signal to parent.

Signal driven I/O cont.  Every signal has a disposition.  We set disposition by calling “ sigaction ” with 3 options. 1. Provide a function (signal handler) for signal occurence. SIGKILL & SIGSTOP signals cannot be caught. 2. Ignore a signal by setting disposition to SIG_IGN. SIGKILL & SIGSTOP cannot be ignored. 3. Set default disposition by SIG_DFL. Example: signalEx1.c (handling ^C ) signalEx2.c (using SIGALRM to perform I/O)

Signal driven I/O cont.  The idea is to set the I/O descriptor for signal driven I/O and then set SIGIO handler calling sigaction.  Whenever the descriptor is ready for reading, the kernel will generate SIGIO signal.  The I/O can then be performed within signal handler.

Signal driven I/O diagram ApplicationKernel Establish SIGIO signal handler sigaction system call initialize return signal handler deliver SIGIO datagram ready system call copy datagram process datagram return OK copy complete Process continues wait for data here recvfrom Process waits copy data from kernel to user This is an advanced I/O model & we will not cover this in this class.

Asynchronous I/O The idea is to start the I/O operation and let the kernel notify us when the entire process is complete (including the copy of the data from the kernel to our buffer). Main difference between signal driven I/O is that with signal driven I/O the kernel tells when an I/O operation can be initiated. But here kernel tells when I/O operation is complete.

Asynchronous I/O diagram ApplicationKernel aio_read no ready datagram datagram ready copy datagram copy complete wait for data here copy data from kernel to user Signal handler process datagram Process continues executing system call return Deliver signal specified in aio_read  Few POSIX systems support asynchronous I/O

Comparison of I/O Models st phase: wait for data InitiateCheck ReadyNotification Initiate 2nd phase: Copy data from kernel to user Complete 1st phase handled differently 2nd phase handled the same (i.e. blocked in call to recvfrom) Handles both phases blockedblocked blockedblocked blockedblocked blockedblocked

Last word The easiest to use among them ◦ Blocked I/O ◦ I/O Multiplexing If your system supports threads, you can emulate other I/O models using multiple threads: ◦ One thread to wait for I/O for first and third models & the other thread doing processing if something needs to be processed. Thus we overlap I/O & computation within a single application.