Top Banner
I/O Models Satish Krishnan
39

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.

Dec 16, 2015

Download

Documents

Lizeth Baty
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

I/O Models

Satish Krishnan

Page 2: 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.

I/O Models

• Blocking I/O

• Non-blocking I/O

• I/O Multiplexing

• Signal driven I/O

• Asynchronous I/O

Page 3: 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 callrecvfrom

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

Page 4: 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.

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

recvfrom no datagram ready

system call

system call

system call

EWOULDBLOCK

EWOULDBLOCK

return OK

Page 5: 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.

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

system call

return readable

Page 6: 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.

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 datagramprocess blocks while data copied into application buffer

recvfrom

return OK

sigaction system call

system call

deliver SIGIO

return

signal handler

Page 7: 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.

Asynchronous I/O Model

system callaio_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 signalspecified in aio_read

return

Page 8: 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.

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)

Page 9: 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.

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.

Page 10: 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.

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.

Page 11: 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.

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.

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

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.

Page 15: 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.

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.

Page 16: 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.

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.

Page 17: 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.

Socket Options

Satish Krishnan

Page 18: 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.

Agenda

• Functions to get and set socket options

• Socket Options– Generic socket options– Protocol specific socket options

Page 19: 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.

Socket Options

• There are 3 ways to get and set options affecting sockets -

– the getsockopt and setsockopt functions.– the fcntl function– the ioctl function

Page 20: 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.

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.

Page 21: 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.

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.

Page 22: 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.

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.

Page 23: 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.

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.

Page 24: 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.

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.

Page 25: 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.

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.

Page 26: 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.

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.

Page 27: 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.

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.

Page 28: 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.

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).

Page 29: 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.

UDP

Satish Krishnan

Page 30: 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.

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

Page 31: 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.

Application details

• Applications will have to do for themselves– Acknowledgement of packets– Flow control– Error detection

Eg dns name query , NFS etc

Page 32: 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.

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);

Page 33: 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.

recvfrom

• Blocking call

• Receives data from bound address and port

• Gives receivers information on struct sockaddr pointer

Page 34: 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.

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

Page 35: 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.

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

Page 36: 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.

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

– }

Page 37: 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.

Name conversions

• Struct hostent *gethostbyaddr(const char *addr,size_t len,int family);

• Uname(struct utsname *name)

utsname has sysname,nodename,release,machine

Page 38: 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.

Services

• Struct servent * Getservbyname(const char *servname,const char *prototype)

• Struct Servent has – char * S_name– char ** s_aliases– int port;– char * s_proto

Page 39: 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.

References

• Unix Network Programming, Volume I– W. Richard Stevens (Ch. 7).

• TCP/IP Illustrated, Volume II– W. Richard Stevens