Sei sulla pagina 1di 57

I/O MULTIPLEXING

Meaning

Entertainment Many screen in the same


complex
Molecular biology Laboratory procedure
Electronic communication many
channels in a single medium
IC Packaging Same set of pins used for
different purpose
Digital system Many inputs share a
common output
Multiplexing Many things share a
common infrastructure

In Networking

Multiplexing is not confined to physical


layer alone in networking
Several conversations are multiplexed
through a single virtual circuit Network
layer level
Only one network address is available
all transport connections have to use
that single address TL level
Upward multiplexing - TL level

IPC

Interprocess communication deals with


separate processes
Each process has its own address space
IPC should ensure that each process will not
interfere with each other
IPC is required for network programming
IPC is more generic
Two processes communicate just using
process ids if they are in the local host
Sockets are required if the processes are
across the hosts in a network

IPC

Both IPC within a host and across hosts


in a network can lead to blocking
IPC within the host deals with multiple
streams
IPC across networks deals with both
streams and sockets

Need for Multiplexing

Required - Client TCP should have the


capability to tell the kernel that it wants to
be notified if one or more I/O conditions are
ready
I/O conditions

Input is ready to be read,


or the descriptor is capable of taking more
output

This capability is called I/O multiplexing


Provided by the select and poll functions

Situations demanding I/O


Multiplexing

Client handles multiple descriptors (normally


interactive input and a network socket)
Client deals with multiple requests Web
client
TCP server handles both a listening socket
and its connected sockets
Server handles both TCP and UDP sockets
Server handles multiple services and
multiple protocols

Unix I/O Models

blocking I/O
nonblocking I/O
I/O multiplexing (select and poll)
signal driven I/O (SIGIO)
asynchronous I/O (the POSIX
aio_functions)

Input Operation on a socket

Two distinct phases for an input operation

Waiting for the data to be ready


Copying the data from the kernel to the
process

For an input operation on a socket

First step
Involves waiting for data to arrive on the network
When the packet arrives, it is copied into a buffer
within the kernel

Second step

Copying this data from the kernel's buffer into our


application buffer

1. Blocking I/O

By default, all sockets are blocking


We differentiate between our application
and kernel
recvfrom Berkeley derived kernel system
call System V Kernel a function that
invokes a system call (getmsg system call)
Switch from running in the application to
the kernel and return to application after
some time

Blocking I/O

The process calls recvfrom


recvfrom does not return until the datagram
arrives
Once arrived it is copied into our application
buffer, or an error occurs.
The most common error is the system call
being interrupted by a signal
The process is blocked the entire time from
when it calls recvfrom until it returns.
When recvfrom returns successfully, our
application processes the datagram.

Blocking I/O Model

2. Non Blocking I/O

During the first three times there is no


data to return
Kernel immediately returns an error of
EWOULDBLOCK instead
The fourth time a datagram is ready
Datagram is copied into our application
buffer
recvfrom returns successfully
Application processes the data.

Non Blocking I/O

Application sits in a loop calling recvfrom


on a nonblocking descriptor polling
The application is continually polling the
kernel to see if some operation is ready
This is often a waste of CPU time, but
this model is occasionally encountered,
normally on systems dedicated to one
function. - disadv

Non Blocking I/O Model

3. I/O Multiplexing

We call select or poll and block in one of


these two system calls
We dont block the actual I/O system call
We block in a call to select, waiting for
the datagram socket to be readable
When select returns that the socket is
readable, we then call recvfrom to copy
the datagram into our application buffer

I/O Multiplexing

Dis adv Using select requires two system


calls instead of one
Adv Wait for more than one descriptor to
be ready
Multithreading with blocking I/O
Instead of using select to block on multiple
file descriptors, the program uses multiple
threads (one per file descriptor), and each
thread is then free to call blocking system
calls like recvfrom

I/O Multiplexing Model

4. Signal Driven I/O

First enable the socket for signal-driven I/O


Install a signal handler using the sigaction
system call
The return from this system call is
immediate and our process continues; it is
not blocked
When the datagram is ready to be read,
the SIGIO signal is generated for our
process.

Signal Driven I/O

We can either read the datagram from the


signal handler by calling recvfrom
Then notify the main loop that the data is
ready to be processed
Alternatively we can notify the main loop
and let it read the datagram
Adv We are not blocked while waiting for
the datagram to arrive
The main loop can continue executing and
just wait to be notified by the signal handler
that either the data is ready to process or
the datagram is ready to be read.

Signal Driven I/O Model

5. Asynchronous I/O

Here functions work by telling the kernel to


start the operation and to notify us when the
entire operation (including the copy of the data
from the kernel to our buffer) is complete.
The main difference between this model and
the signal-driven I/O model in the previous
section is
With signal-driven I/O, the kernel tells us when
an I/O operation can be initiated
But with asynchronous I/O, the kernel tells us
when an I/O operation is complete.

Asynchronous I/O

We call aio_read and pass the kernel the descriptor,


buffer pointer, buffer size (the same three arguments
for read), file offset (similar to lseek), and how to
notify us when the entire operation is complete.
This system call returns immediately and our process
is not blocked while waiting for the I/O to complete.
We assume in this example that we ask the kernel to
generate some signal when the operation is
complete.
This signal is not generated until the data has been
copied into our application buffer, which is different
from the signal-driven I/O model.

Asynchronous I/O Model

Comparison of the five I/O


models

Select Function

Allows the process to instruct the kernel to wait for


any one of multiple events to occur
Also the process to instruct the kernel to wake up the
process only when one or more of these events occurs
or when a specified amount of time has passed.
As 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
Any of the descriptors in the set {2, 7} are ready for
writing
Any of the descriptors in the set {1, 4} have an
exception condition pending
10.2 seconds have elapsed

Select
#include <sys/select.h>
#include <sys/time.h>
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

const qualifier on the timeout argument means it is not modified b

Select
Select can deal with how long the kernel has to wait
1) Wait forever Return only when one of the
specified descriptors is ready for I/O timeout
argument as a null pointer.
2) Wait up to a fixed amount of time time out
argument is the number of seconds and
microseconds
3) Do not wait at all Return immediately after
checking the descriptors. This is called polling

Time out argument is et to 0


In all cases timeout argument points towards a
timeval structure

Select

Through select call a process tells the kernel


what descriptors the process is interested for

1.

2.

reading,
writing, or
an exception condition

only two exception conditions


The arrival of out-of-band data(expedited
data) for a socket.
The presence of control status information to
be read from the master side of a pseudoterminal that has been put into packet mode.

Select

select uses descriptor sets, typically an array of integers,


with each bit in each integer corresponding to a descriptor.

For example

Select When ready to read


A socket is ready for reading if any of the
following four conditions is true:
1. The number of bytes of data in the socket receive
buffer is greater than or equal to the current size
of the low-water mark for the socket receive
buffer.

2.

A read operation on the socket will not block


and will return a value greater than 0 (i.e., the
data that is ready to be read).

The read half of the connection is closed (i.e., a


TCP connection that has received a FIN).

A read operation on the socket will not block and


will return 0 (i.e., EOF).

Select When ready to read


3.

The socket is a listening socket and the


number of completed connections is
nonzero.

4.

An accept on the listening socket will


normally not block

A socket error is pending.

A read operation on the socket will not


block and will return an error (1) with
errno set to the specific error condition.

Select When ready to


write
A socket is ready for writing if any of the following
four conditions is true:
1. The number of bytes of available space in the socket
send buffer is greater than or equal to the current
size of the low-water mark for the socket send buffer
and either:

(i) the socket is connected,


(ii) the socket does not require a connection (e.g., UDP).

If we set the socket to nonblocking , a write


operation will not block and will return a positive
value.
Low-water mark normally defaults to 2048 for TCP
and UDP sockets.

Select When ready to


write
2. The write half of the connection is closed
3. A socket using a non-blocking connect has
completed the connection, or the connect has
failed
4. A socket error is pending.

A write operation on the socket will not block and


will return an error (1) with errno set to the specific
error condition

A socket has an exception condition pending if


there is out-of-band data for the socket or the
socket is still at the out-of-band mark

Summary of conditions
that cause a socket to be
ready for select

Maximum Number of
Descriptors for select

maxfdp1 argument specifies the number


of descriptors to be tested.
Its value is the maximum descriptor to
be tested plus one (hence our name of
maxfdp1).
The constant FD_SETSIZE, defined by
including <sys/select.h>, is the number
of descriptors in the fd_set datatype. Its
value is often 1024.

Select - str_cli Function


(Revisited)

Advantage of using select is a process is


notified as soon as the server process
terminates.
The problem with out multiplexing is that
the process gets blocked in the call to fgets
when something happened on the socket
I/O Multiplexing using select blocks in a call
to select instead, waiting for either
standard input or the socket to be readable

Conditions handled by select


in str_cli

Conditions handled with


Socket

1)

2)

3)

If the peer TCP sends data, the socket


becomes readable and read returns
greater than 0 (i.e., the number of bytes
of data).
If the peer TCP sends a FIN (the peer
process terminates), the socket becomes
readable and read returns 0 (EOF).
If the peer TCP sends an RST (the peer
host has crashed and rebooted), the
socket becomes readable, read returns
1, and errno contains the specific error
code.

Implementation of str_cli function


using select

Shutdown Function

Close is a normal way to terminate a network


connection.
two limitations with close that can be avoided
with shutdown

close decrements the descriptor's reference count and


closes the socket only if the count reaches 0.
With shutdown, we can initiate TCP's normal connection
termination sequence, regardless of the reference
count.
2. close terminates both directions of data transfer,
reading and writing, whereas shutdown waits for the I/O
to finish.
1.

shutdown Function- Calling


shutdown to close half of a
TCP connection

Shutdown Function
#include <sys/socket.h>
int shutdown(int sockfd, int howto);
Returns: 0 if OK, 1 on error

howto argument:
1. SHUT_RD - The read half of the connection is
closed
2. SHUT_WR - The write half of the connection
is closed
3. SHUT_RDWR - The read half and the write
half of the connection are both closed

Difference between shut down


& close
shut down
1. With shutdown we
can
initiate
TCPs
normal
connection
termination
sequence
regardless of reference
count
2. Shut down function
tell the other end that
we have finish sending
even though that end
might have send to
send more data.

close function
1. In this case of close
function decrement the
reference count and
closes the socket only if
the count reaches 0.
2.
Close
function
terminates
both
directions
of
data
transfer reading and
writing.

str_cli Function (Revisited


Again)

TCP Echo Server


(Revisited)
rewrite the server as a single process that uses select to handle any number
of clients, instead of forking one child per client.

TCP server before first client has established a connection.

Data structures
for TCP server
with just a
listening socket,
server maintains
a read descriptor
set.

descriptors 0, 1, and 2 are set to standard input, output, and error.


Here, the available descriptor for the listening socket is 3.

TCP Echo Server


(Revisited)
TCP server after first client establishes connection

Data structures after first client connection is established

TCP Echo Server


(Revisited)
TCP server after second client connection is established

Data structures after second client connection is established

TCP Echo Server


(Revisited)
Data structures after first client terminates its connection

TCP Echo Server (Revisited) TCP


server using a single process and
select: initialization

TCP server using a single process


and select loop

Poll Function

poll provides functionality that is similar to select,


but poll provides additional information
Conditions that cause poll to return the event are

All regular TCP data and all UDP data is considered normal
TCP's out-of-band data is considered priority band.
When the read half of a TCP connection is closed (e.g., a
FIN is received), this is also considered normal data and a
subsequent read operation will return 0.
The presence of an error for a TCP connection can be
considered either normal data or an error (POLLERR).
The availability of a new connection on a listening socket
can be considered either normal data or priority data. Most
implementations consider this normal data.
The completion of a nonblocking connect is considered to
make a socket writable.

Poll Function
#include <poll.h>
int poll (struct pollfd *fdarray, unsigned long nfds, int timeout);
Returns: count of ready descriptors, 0 on timeout, 1 on error

Input events and returned revents for poll

Poll Function
timeout values for poll
constant INFTIM is defined
to be a negative value.
return value from poll is 1 if an error occurred, 0 if no descriptors are ready before the timer expires, otherwise it is the number of descriptors that have a nonzero revents member.

TCP Echo Server


(Revisited Again)

TCP Echo Server


(Revisited Again)

TCP Echo Server


(Revisited Again)

Potrebbero piacerti anche