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.

Slides:



Advertisements
Similar presentations
Socket Options. abstraction Introduction getsockopt and setsockopt function socket state Generic socket option IPv4 socket option ICMPv6 socket option.
Advertisements

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 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.
Multicasting - Part II© 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.
Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Copyright 1999, S.D. Personick. All Rights Reserved. Telecommunications Networking II Lecture 32 Transmission Control Protocol (TCP) Ref: Tanenbaum pp:
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.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
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.
Socket Options Jari Kellokoski. Introduction So far we have discovered some basic usage of socket With socket options we control sockets more suitable.
SOCKETS Lecture #3. The Socket Interface Funded by ARPA (Advanced Research Projects Agency) in Developed at UC Berkeley Objective: to transport.
Computer Networks Sockets. Outline F Socket basics F Socket details.
Computer Network Architecture and Programming
Lecture 8 UDP Sockets & I/O Multiplexing
Socket option Getsockopt ou setsockopt fcntl ioctl.
IP Multiplexing Ying Zhang EECS 489 W07.
ECE 4110 – Internetwork Programming Client-Server Model.
Elementary TCP Sockets
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
Signal-Driven I/O Concepts and steps for using signal-driven I/O
TCP : Transmission Control Protocol Computer Network System Sirak Kaewjamnong.
More on Socket API. How to Place Timeouts on Sockets (1)  Using SIGALRM signal Connection timeout 기간의 축소 Response timeout advio/dgclitimeo3.clib/connect_timeo.c.
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.
Network Programming Eddie Aronovich mail:
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.
More on Socket API. More on Socket I/O Functions  Scatter read and gather write.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
Netprog: Advanced Sockets Programming1 Advanced Sockets Programming Ref: Chapter 7,11,21,22.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
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.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Socket Options Ref: Chapter 7. 2 Socket Options Various attributes that are used to determine the behavior of sockets.Various attributes that are used.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Socket options: a summary –Tcp options, ip options and general socket options can be examined and sometimes modified. getsockopt, setsockopt routines Some.
TELE 402 Lecture 6: Name and address conversions 1 Overview Last Lecture –Socket Options and elementary UDP sockets This Lecture –Name and address conversions.
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.
Today’s topic: UDP Reliable communication over UDP.
Review: –Concurrent server and multiplexed server How they work? Which one is better?
1 Socket Options getsockopt and setsockopt functions Check options and obtain default values Generic socket options IPv4 socket options IPv6 socket options.
CMPT 471 Networking II Network Programming © Janice Regan,
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
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.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
IPv4 socket option Level => IPPROTO_IP
I/O Multiplexing.
Socket Option.
Review: TCP Client-Server Interaction
CHAPTER 8 ELEMENTARY UDP SOCKETS
Network Programming CSC- 341
Lecture 4 Socket Programming Issues
Chapter 07. Socket Options.
Lecture 11 Overview.
Advanced Network Programming spring 2007
Socket Programming.
Advanced Sockets Programming
Socket Programming with UDP
Socket options: a summary
Advanced UNIX programming
Presentation transcript:

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

Blocking I/O Model system call recvfrom application process blocks in a call to recvfrom process datagram kernel datagram ready copy complete copy data from kernel to user wait for data copy datagram no datagram ready return OK

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

I/O Multiplexing Model select application process blocks in a call to select waiting for one of possibly many sockets to become readable process datagram kernel datagram ready copy complete copy data from kernel to user wait for data copy datagram no datagram ready process blocks while data copied into application buffer recvfrom return OK system call return readable

Signal Driven I/O Model establish SIGIO signal handler application process continues executing process datagram kernel datagram ready copy complete copy data from kernel to user wait for data copy datagram process blocks while data copied into application buffer recvfrom return OK sigaction system call system call deliver SIGIO return signal handler

Asynchronous I/O Model system call aio_read application process continues executing signal handler process datagram kernel datagram ready copy complete copy data from kernel to user wait for data copy datagram no datagram ready deliver signal specified in aio_read return

I/O Multiplexing The capability to tell the kernel that we want to be notified if one or more I/O conditions is ready. Provided by select and poll functions. Is required when –a client handles multiple descriptors(e.g. Interactive input and network socket). –a client handles multiple sockets –a server handles both TCP and UDP transport. –a server handles multiple services or protocols (e.g. inetd)

select Function It tells the kernel to wait for one or more descriptors to be ready (for reading, writing or an exception condition) and for how long to wait. 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.

select Function Contd... maxfdp1 specifies the number of descriptors to be tested. readset, writeset and exceptset are value-result arguments. timeout can make select wait forever, or wait for a specified amount of time, or return immediately. select is not automatically restarted in Berkeley implementations if it is interrupted by a signal. The value of timeout can be modified in Linux implementations.

When is a socket ready for read? The number of bytes in the socket receive buffer is >= the low water mark for the socket receive buffer. It defaults to 1 for TCP and UDP sockets. The read half of the connection is closed (FIN received). The socket is a listening socket and the number of completed transactions is non-zero (accept returns the connection). A socket error is pending. read will return an error (-1) with errno set.

When is a socket ready for write? The number of bytes of available space in the socket send buffer is >= the low water mark for the socket send buffer. It defaults to 2048 for TCP and UDP sockets. The write half of the connection is closed. SIGPIPE generated on write. A socket error is pending. write will return an error (-1) with errno set.

Exception Condition A socket has an exception condition pending if there exists out-of-band data for the socket. Out-of-band data is high priority data. TCP terms it urgent mode. OOB data is placed in the next available set of bytes in the send buffer. The next TCP segment sent will have the URG flag set in the TCP header and the urgent offset pointing to the OOB byte. The receiving process is notified on receipt of the SIGURG signal.

pselect Function int pselect(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timespec *timeout, const sigset_t *sigmask); returns count of ready descriptors, 0 on timeout, -1 on error timespec structure can specify time in nanoseconds.

Pselect contd… sigmask allows the program to block or unblock certain signals. This mask is applicable only during the call to pselect. After pselect returns the old mask replaces this mask.

poll Function int poll(struct pollfd *fdarray, unsigned long nfds, int timeout); returns count of ready descriptors, 0 on timeout, -1 on error. Each element of fdarray is a pollfd structure that specifies the condition to be tested for a given descriptor. pollfd contains: file descriptor fd, events of interest on fd, and events that occured on fd. To switch off a descriptor, set the fd member of the pollfd structure to a negative value.

Socket Options Satish Krishnan

Agenda Functions to get and set socket options Socket Options –Generic socket options –Protocol specific socket options

Socket Options There are 3 ways to get and set options affecting sockets - –the getsockopt and setsockopt functions. –the fcntl function –the ioctl function

getsockopt() int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); returns 0 if OK, -1 on error. level indicates whether the socket option is generic or protocol specific. optval is a pointer to a variable into which the current value of the option is stored by getsockopt(). optlen is the size of optval. It is a value-result argument.

setsockopt() int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen); returns 0 if OK, -1 on error. level indicates whether the socket option is generic or protocol specific. optval is a pointer to a variable from which the new value of the option is fetched by setsockopt(). optlen is the size of optval.

Socket Options Two basic type of options - –Flags - binary options that enable or disable a feature. –Values - options that fetch and return specific values. Not supported by all implementations. Socket option fall into 4 main categories - –Generic socket options SO_RCVBUF, SO_SNDBUF, SO_BROADCAST, etc. –IPv4 IP_TOS, IP_MULTICAST_IF, etc. –IPv6 IPv6_HOPLIMIT, IPv6_NEXTHOP, etc. –TCP TCP_MAXSEG, TCP_KEEPALIVE, etc.

Socket States Options have to be set or fetched depending on the state of a socket. Some socket options are inherited from a listening socket to the connected sockets on the server side. –E.g. SO_RCVBUF and SO_SNDBUF These options have to be set on the socket before calling listen() on the server side and before calling connect() on the client side.

Generic Socket Options SO_BROADCAST –Enables or disables the ability of a process to send broadcast messages. –It is supported only for datagram sockets. –Its default value is off. SO_ERROR –Pending Error - When an error occurs on a socket, the kernel sets the so_error variable. –The process can be notified of the error in two ways - If the process is blocked in select for either read or write, it returns with either or both conditions set. If the process is using signal driven I/O, the SIGIO signal is generated for the process.

Generic Socket Options Contd... SO_KEEPALIVE –Purpose of this option is to detect if the peer host crashes. The SO_KEEPALIVE option will detect half- open connections and terminate them. –If this option is set and no data has been exchanged for 2 hours, then TCP sends keepalive probe to the peer. Peer responds with ACK. Another probe will be sent only after 2 hours of inactivity. Peer responds with RST (has crashed and rebooted). Error is set to ECONNRESET and the socket is closed. No response. 8 more probes are sent after which the socket’s pending error is set to either ETIMEDOUT or EHOSTUNREACH and the socket is closed.

Generic Socket Options Contd... Receive Low Water Mark - –Amount of data that must be in the socket receive buffer for a socket to become ready for read. Send Low Water Mark - –Amount of space that must be available in the socket send buffer for a socket to become ready for write. SO_RCVLOWAT and SO_SNDLOWAT –These options specify the receive low water mark and send low water mark for TCP and UDP sockets.

Generic Socket Options Contd... SO_RCVTIMEO and SO_SNDTIMEO –These options place a timeout on socket receives and sends. –The timeout value is specified in a timeval structure. struct timeval { long tv_sec ; long tv_usec ; } –To disable a timeout, the values in the timeval structure are set to 0.

Generic Socket Options Contd... SO_REUSEADDR –It allows a listening server to restart and bind its well known port even if previously established connections exist. –It allows multiple instances of the same server to be started on the same port, as long as each instance binds a different local IP address. –It allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address. –It allows completely duplicate bindings only for UDP sockets (broadcasting and multicasting).

UDP Satish Krishnan

UDP and TCP When to use? –Applications use broadcasting or multicasting –Cost of connection establishment is high compared to data transferred When not to use? –Flow control is very important –Packet sequence has to be maintained

Application details Applications will have to do for themselves –Acknowledgement of packets –Flow control –Error detection Eg dns name query, NFS etc

Socket functions ssize_t recvfrom(int sockfd,void *buff,size_t nbytes,int flags,struct sockaddr *from,socklen_t addrlen); ssize_t sendto(int sockfd,const void *buff,size_t nbytes,int flags,const struct sockaddr *tosocklen_t addrlen);

recvfrom Blocking call Receives data from bound address and port Gives receivers information on struct sockaddr pointer

Send to Used to send data when the socket is not in a connected state address of the target is specified in the argument list Errors are indicated by a return value of -1

Connect? Connect is done on a udp socket specifying destination address and port Send can be used instead of sendto as destination address is known Does not do any TCP like connection

Name Conversions struct hostent *gethostbyname(const char *hostname) struct hostent { –char *hname; –char **h_aliases –int h_addrtype; –int h_length –int *h_adr_list –}

Name conversions Struct hostent *gethostbyaddr(const char *addr,size_t len,int family); Uname(struct utsname *name) utsname has sysname,nodename,release,machine

Services Struct servent * Getservbyname(const char *servname,const char *prototype) Struct Servent has –char * S_name –char ** s_aliases –int port; –char * s_proto

References Unix Network Programming, Volume I –W. Richard Stevens (Ch. 7). TCP/IP Illustrated, Volume II –W. Richard Stevens