modified iv unit

Upload: sandeep-khandekar

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Modified IV Unit

    1/43

    I/O Multiplexing

    IV-Unit

  • 8/13/2019 Modified IV Unit

    2/43

    INPUT / OUTPUT MODEL

    SYNCHRONOUS I/O ASYNCHRONOUS I/O

    SYNCHRONOUS I/O

    A synchronous I/O causes the requestingprocess to be blocked until the

    operation Completes.

    ASYNCHRONOUS I/O

    A asynchronous I/O operation does not cause the requesting process to be

    blocked.

    SYNCHRONOUS I/O

    BLOCKING I/O NON-BLOCKING I/O I/O MULTIPLUXING SIGNAL DRIVEN I/O

  • 8/13/2019 Modified IV Unit

    3/43

    I/O Models

    1. Blocking I/O

    2. Non-blocking I/O

    3. I/O multiplexingselect()

    4. Signal driven I/O

    5. Asynchronous I/O

  • 8/13/2019 Modified IV Unit

    4/43

    Blocking I/O

    By default all sockets are blocking

    The process calls recvfrom and system call

    does not return until the datagram arrives

    and is copied into our application buffer or

    error occurs.

    When recvfromreturns successfully,ourapplication process the datagram.

  • 8/13/2019 Modified IV Unit

    5/43

    Blocking I/O

    recvfrom() No datagram ready

    datagram ready

    Copy datagram

    Copy completeProcess datagram

    System call

    Application Operating system

    Wait for data

    Copy data to user

    Return ok

    Process blocks

  • 8/13/2019 Modified IV Unit

    6/43

    Non-Blocking I/O

    When we set a socket to be nonblocking, we

    are telling the kernel when an I/O operation

    that I request cannt be completed without

    putting the process to sleep, but return an

    error instead.

    The first 3 times that we call recvfrom, there

    is no data to return, so kernel immediately

    returns an error of EWOULDBLOCKinstead.

    Fourth time we call recvfrom, a datagram is

    ready , it is copied into our application buffer,

    and recvfrom returns successfully

  • 8/13/2019 Modified IV Unit

    7/43

    Non-Blocking I/O

    recvfrom() No datagram ready

    datagram ready

    Copy datagram

    Copy completeProcess datagram

    System call

    Application Operating system

    Copy data to user

    Return ok

    Process blocks

    EWOULDBLOCK

    recvfrom() No datagram readySystem call

    EWOULDBLOCK

    recvfrom() System call

  • 8/13/2019 Modified IV Unit

    8/43

    I/O Multiplexing

    With I/O multiplexing, we call selectandpoll and block in one of these two system

    calls , instead of blocking in the actual I/O

    system call. We block in a call to select, waiting for the

    datagram socket to be readable.

    When selectreturns that the socket isreadable, we call recvfromto copy the

    datagram into our application buffer.

  • 8/13/2019 Modified IV Unit

    9/43

    What is I/O multiplexing? When an application needs to handle

    multiple I/O descriptors at the same timeE.g. file and socket descriptors, multiple socket

    descriptors

    If a server handles multiple services and

    perhaps multiple protocols,I/O multiplexing

    is used.

    If a TCP server handles both a listening

    socket and its concerned sockets, I/O

    multiplexing is used.

    If a server handles both TCP & UDP,I/O

    multiplexing is used.

  • 8/13/2019 Modified IV Unit

    10/43

    Non-forking concurrent server

    Concurrent Server

    select()

    C1 C2 C3 C4 C5

    listenfd

    fd1 fd2 fd3 fd4 fd5Sockets

    Clients

  • 8/13/2019 Modified IV Unit

    11/43

    I/O Multiplexing

    select() No datagram ready

    datagram ready

    Copy datagram

    Copy completeProcess datagram

    System call

    Application Operating system

    Wait for data

    Copy data to user

    Return ok

    Process blocks

    Return readable

    Process blocks

    recvfrom() System call

  • 8/13/2019 Modified IV Unit

    12/43

    Signal driven I/O

    We can use the signals, telling the kernel to

    notify us with the SIGIO signal when the

    descriptor is ready.

    First enable the socket for signal-driven I/O

    and install a signal handlerusing the

    sigactionsystem call

  • 8/13/2019 Modified IV Unit

    13/43

    Signal driven I/O

    Establish SIGIO

    Signal handler No datagram ready

    datagram ready

    Copy datagram

    Copy completeProcess datagram

    System call

    Application Operating system

    Wait for data

    Copy data to user

    Return ok

    Process blocks

    Deliver SIGIO

    Process continues

    recvfrom() System callSignal Handler

  • 8/13/2019 Modified IV Unit

    14/43

    Asynchronous I/O

    These functions work by telling the kernel tostart the operation and to notify us when the

    entire operation (including the copy of the

    data from kernel to our buffer) is complete.

    The difference b/w the signal-driven I/O

    and this model is, in signal-driven I/O model

    the kernel tells us when an I/O operation can

    be initiated. but with asynchronous I/O,thekernel tells us when an I/O operation is

    complete.

  • 8/13/2019 Modified IV Unit

    15/43

    Asynchronous I/O

    aio_read() No datagram ready

    datagram ready

    Copy datagram

    Copy completeSignal handler

    Process datagram

    System call

    Application Operating system

    Wait for data

    Copy data to user

    Return ok

    Process continues

    return

  • 8/13/2019 Modified IV Unit

    16/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006

    16

    Comparison of the I/O Models

    Blocking NonblockingI/O

    multiplexing

    Signal-driven

    I/O

    Asynchronous

    I/O

    initiate

    complete

    check

    check

    check

    check

    checkcheck

    complete

    blocked

    check

    blocked

    ready

    initiateblocked

    complete

    notification

    initiateblocked

    complete

    initiate

    notification

    wait for

    data

    copy data

    from kernel

    to user

    1st phase handled differently,

    2nd phase handled the samehandles both phases

  • 8/13/2019 Modified IV Unit

    17/43

    select()call Allows a process to wait for an event to occur on

    any one of its descriptors. Types of event

    ready for read

    ready for write

    Exception condition

    Ex: we can call select and tell the kernel to return only

    when: Any of the descriptors in the set {1,4,7} are ready for

    reading

    Any of the descriptors in the set {2,5} are ready for

    writing

  • 8/13/2019 Modified IV Unit

    18/43

    select()call

    Syntax:

    #include

    #include

    int select (int maxfdp1,fd_set *readset, fd_set*writeset, fd_set *excceptset, const struct timeval*timeout)

    Returns:

    success : positive count of ready descriptorserror : -1

  • 8/13/2019 Modified IV Unit

    19/43

    select() call

    int select(int maxfdp1, /* max. fd + 1 */

    fd_set *readfds, /* read ready? */

    fd_set *writefds, /* write ready? */

    fd_set*exceptfds, /* exceptions? */struct timeval*timeout); /* which tellsthe kernel how long to wait for one of thespecified descriptor to become ready.*/

    struct timeval{

    long tv_sec; /* seconds */

    long tv_usec; /* microseconds */

    }

  • 8/13/2019 Modified IV Unit

    20/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006

    20

    Possibilities for select function

    Wait forever: return only when descriptor (s) is ready (specifytimeoutargument as NULL)

    wait up to a fixed amount of time

    Do not wait at all: return immediately after checking the

    descriptors. Polling (specify timeoutargument as pointing to a

    timevalstructure where the timer value is 0)

    The wait is normally interrupted if the process catches a signal

    and returns from the signal handler selectmight return an error of EINTR

    Actual return value from function = -1

  • 8/13/2019 Modified IV Unit

    21/43

    struct fd_set How to specify one or more descriptors values for each of these

    3 arguments, select uses descriptor sets, typically an array of

    intergers,with each bit in each integer corresponding to adescriptor.

    Set of descriptors that we want to wait on for events.

    We set and test the bits in the set using these macros

    Manipulation macros void FD_ZERO(fd_set*fds) /* clear all bits in fdset */

    void FD_SET (int fd, fd_set*fds) /* turn ON the bitfor fdset */ void FD_CLR (int fd, fd_set*fds) /* turn OFF thr bits

    for fd in fdset */ void FD_ISSET(int fd, fd_set*fds) /* is the bit for fd

    on in fdset */

    d fi i bl f fd d

  • 8/13/2019 Modified IV Unit

    22/43

    Ex: to define a variable of type fd_set and

    then turn ON the bits for descriptors 1,4,5

    fd_set rset:

    FD_ZERO(&rset);

    FD_SET(1, &rset);

    FD_SET(4, &rset);FD_SET(5,&rset);

    The maxfdp1 argument specifies the number

    of descriptors to be tested.

  • 8/13/2019 Modified IV Unit

    23/43

    Non-forking Concurrent Server

    fdsetrdset, wrset;int listenfd, connfd1, connfd2;

    int maxfdp1;

    Connection establishment etc.

    /* initialize */

    FD_ZERO(&rdset);

    FD_ZERO(&wrset);

    for( ;; ) {

  • 8/13/2019 Modified IV Unit

    24/43

    for( ;; ) {

    FD_SET(connfd1, &rdset);

    FD_SET(connfd2, &wrset);

    FD_SET(listenfd, &rdset);

    maxfdp1 = max(connfd1, connfd2, listenfd) + 1;

    /* wait for some event */

    Select(maxfdp1, &rdset, &wrset, NULL, NULL);

    if( FD_ISSET(connfd1, &rdset) ) {Read data from connfd1

    }

    if( FD_ISSET(connfd2, &wrset) ) {

    Write data to connfd2

    }

    if( FD_ISSET(listenfd, &rdset) {Process a new connection

    }

    }

  • 8/13/2019 Modified IV Unit

    25/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006

    25

    str_clioperates in a

    stop-and-wait

    modesends a line to

    the server and then

    waits for the reply

    Assume RTT of 8 units

    of timeonly using

    one-eighth of the pipecapacity

    Ignore TCP ACKs

    request

    request

    serverrequest

    request

    serverreply

    reply

    reply

    reply

    client

    time1

    time2

    time3

    time4

    time5

    time6

    time7

    time0

    Batch Input and Buffering 1/2

  • 8/13/2019 Modified IV Unit

    26/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006

    26

    With batch input, can send as fast as we can

    The problem with the revised str_clifunction

    After the handling of an end-of-file on input, the send

    function returns to the main function, that is, the program is

    terminated.

    However, in batch mode, there are still other requests and

    replies in the pipe.

    We need a way to close one-half of the TCP connection

    send a FINto the server, telling it we have finished sendingdata, but leave the socket descriptor open for reading

    shutdownfunction

    Batch Input and Buffering 2/2

  • 8/13/2019 Modified IV Unit

    27/43

    When select statement is used in the program

    When connection is released at this point of time the all the data

    grams at that Time in the network will be lost so we have to introduce a

    Shout down function which Will terminate the connection in a proper manner

  • 8/13/2019 Modified IV Unit

    28/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006

    28

    Shutdown function 1/3 The normal way to terminate a network connection is to call a

    close function.

    Close one half of the TCP connection

    send FINto server, but leave the socket descriptor open forreading

    Limitations with closefunctiondecrements the descriptors reference count and closes the

    socket only if the count reaches 0

    With shutdown, can initiate TCP normal connection

    termination regardless of the reference count terminates both directions (reading and writing)

    With shutdown, we can tell other end that we are donesending, although that end might have more data to sendus

  • 8/13/2019 Modified IV Unit

    29/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 29

    Shutdown function 2/3

    client server

    data

    data

    FIN

    ACK of data and FIN

    data

    data

    FIN

    ACK of data and FIN

    Read returns > 0

    Read returns > 0

    Read returns 0

    write

    write

    close

    write

    write

    shutdown

    Read returns > 0

    Read returns > 0

    Read returns 0

  • 8/13/2019 Modified IV Unit

    30/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 30

    #include

    int shutdown ( int sockfd, int howto );/* return : 0 if OK, -1 on error */

    howtoargument

    SHUT_RD

    read-half of the connection closed

    Any data in receive buffer is discarded

    Any data received after this call is ACKed and then discarded

    SHUT_WR

    write-half of the connection closed (half-close)Data in socket send buffer sent, followed by connection termination

    SHUT_RDWR

    both closed

    Shutdown function 3/3

  • 8/13/2019 Modified IV Unit

    31/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 31

    TCP echo server using select 1/5 Rewrite the server as a single process that uses selectto handle

    any number of clients, instead of forking one child per client. Before first client has established a connection

    Client[]

    [0][1]

    [2]

    -1-1

    -1

    -1[FD_SETSIZE -1]

    rset:

    fd0 fd1 fd2 fd3

    0 0 0 1

    Maxfd + 1 = 4

    fd:0(stdin), 1(stdout), 2(stderr)

    fd:3listening socket fd

  • 8/13/2019 Modified IV Unit

    32/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 32

    TCP echo server using select 2/5 After first client connection is established (assuming connected

    descriptor returned by acceptis 4)

    Client[]

    [0]

    [1][2]

    4

    -1-1

    -1[FD_SETSIZE -1]

    rset:

    fd0 fd1 fd2 fd3

    0 0 0 1

    Maxfd + 1 = 5

    fd:0(stdin), 1(stdout), 2(stderr)fd:3listening socket fd

    fd:4first connected fd

    1

    fd4

  • 8/13/2019 Modified IV Unit

    33/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 33

    TCP echo server using select 3/5 After second client connection is established (assuming

    connected descriptor returned by acceptis 5)

    Client[]

    [0]

    [1][2]

    4

    5-1

    -1[FD_SETSIZE -1]

    rset:

    fd0 fd1 fd2 fd3

    0 0 0 1

    Maxfd + 1 = 6

    fd:0(stdin), 1(stdout), 2(stderr)fd:3listening socket fd

    fd:4first connected socket fd

    fd:5second connected socket fd

    1

    fd4 fd5

    1

  • 8/13/2019 Modified IV Unit

    34/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 34

    TCP echo server using select 4/5 First client terminates its connection (fd4 readable and read

    returns 0client TCP sent a FIN)

    Client[]

    [0]

    [1][2]

    -1

    5-1

    -1[FD_SETSIZE -1]

    rset:

    fd0 fd1 fd2 fd3

    0 0 0 1

    Maxfd + 1 = 6

    fd:0(stdin), 1(stdout), 2(stderr)fd:3listening socket fd

    fd:5second connected socket fd

    0

    fd4 fd5

    1

  • 8/13/2019 Modified IV Unit

    35/43

    I/O Multiplexing Dr. Ayman Abdel-Hamid, CS4254Spring 2006 35

    TCP echo server using select 5/5

    As clients arrive, record connected socket descriptor in firstavailable entry in client array (first entry = -1)

    Add connected socket to read descriptor set

    Keep track of

    Highest index in client array that is currently in use

    Maxfd +1

    The limit on number of clients to be served

    Min(FD_SETSIZE, Max ( Number of descriptors allowed forthis process by the kernel))

    Source code in tcpcliserv/tcpservselect01.c

  • 8/13/2019 Modified IV Unit

    36/43

    PollFunction

    Thepoll() function provides applications with amechanism for multiplexing input/output over a

    set of file descriptors. For each member of the

    array pointed to byfds,poll() shall examine thegiven file descriptor for the event(s) specified in

    events. The number of pollfdstructures in thefds

    array is specified by nfds. Thepoll() function shallidentify those file descriptors on which an

    application can read or write data, or on which

    certain events have occurred.

  • 8/13/2019 Modified IV Unit

    37/43

    poll function

    Syntax:

    #include

    int poll(struct pollfd *fdarray , unsigned long

    nfds, int timeout);

    Returns :

    count of the ready descriptors of OK0 on time out

    -1 on error.

  • 8/13/2019 Modified IV Unit

    38/43

    Arguments to poll:

    The first argument is a pointer to the first elementof an array of structures. Each element of the array is a pollfd

    structure.this specifies the conditions to be tested.

    struct pollfd{

    int fd; /* descriptor to check */

    short event; /* events of interest on fd */

    short revent; /* events that occurred on fd */

    };

    Time out is:

    INFTIM: wait for ever.

    0 return immediately.

    -1 error.

  • 8/13/2019 Modified IV Unit

    39/43

    Socket Options

  • 8/13/2019 Modified IV Unit

    40/43

    Managing socket options3 ways

    getsockopt() and setsockopt()

    fcntl()

    ioctl()

  • 8/13/2019 Modified IV Unit

    41/43

    getsockopt() and setsockopt()

    int getsockopt(

    int sockfd,

    int level, /* SOL_SOCKET, IPPROTO_IP, IPPROTO_TCP etc */

    int optname, /* option name */void *optval, /* option value */

    socklen_t *optlen); /* data length of option*/

    int setsockopt(

    int sockfd,int level,

    int optname,

    const void *optval,

    socklen_t optlen);

  • 8/13/2019 Modified IV Unit

    42/43

    Some socket options

    SO_BROADCAST - enable socket for sending broadcast

    SO_KEEPALIVEregularly probes on inactive TCPconnection.

    SO_LINGERlinger till all data is sent before returningfrom close()on a TCP connection.

    SO_RCVBUF/ SO_SNDBUFsize of receive/sendbuffers on socket.

    SO_RCVTIMEO/SO_SNDTIMEOplace timeout onreceive/send operation.

  • 8/13/2019 Modified IV Unit

    43/43

    fcntl()

    int fcntl(int fd, int cmd, long arg);

    Miscellaneous file control operationsNon-blocking I/O (O_NONBLOCK, F_SETFL)

    Signal-driven I/O (O_ASYNC, F_SETFL)

    Set socket owner (F_SETOWN)