SlideShare a Scribd company logo
1 of 55
I/O Multiplexing:
select and poll function
abstract

Introduction
I/O Models
Synchronous I/O versus Asynchronous I/O
select function
shutdown function
pselect function
poll function
Introduction
TCP client is handling two inputs : standard
input and a TCP socket
– when the client was blocked in a call to read, the
  server process was killed
– server TCP sends FIN to the client TCP, but the
  client never see FIN since the client is blocked
  reading from standard input
=> the capability to tell the kernel that we want to be
  notified if one or more I/O conditions are ready.
  : I/O multiplexing (select and poll)
When:
– client is handling multiple descriptors (interactive
  input and a network socket).
– Client to handle multiple sockets(rare)
– TCP server handles both a listening socket and its
  connected socket.
– Server handle both TCP and UDP.
– Server handles multiple services and multiple
  protocols
I/O Models
  Blocking I/O
  Nonblocking I/O
  I/O multiplexing(select and poll)
  signal driven I/O(SIGIO)
  asynchronous I/O(posix.1 aio_ functions)

Two distinct phases for an input operations
 1. Waiting for the data to be ready
 2. Copying the data from the kernel to the process
Blocking I/O
                 application                   kernel
                               System call
                   recvfrom                  No datagram ready
                                                                 Wait for
                                                                 data
Process blocks
in a call to                                   Datagram ready
recvfrom                                                         Copy data
                                               copy datagram
                                                                 from kernel
                                                                  to user

                               Return OK
                   Process                      Copy complete
                   datagram
nonblocking I/O
                application                       kernel
                              System call
                  recvfrom                   No datagram ready
                              EWOULDBLOCK

                               System call
                 recvfrom                    No datagram ready
                              EWOULDBLOCK                        Wait for
                                                                 data
Process                       System call
 repeatedly      recvfrom                    datagram ready
call recvfrom                                    copy datagram
wating for an
OK return                                                        Copy data
(polling)                                                        from
                               Return OK                         kernel
                                                                  to user
                  Process                        application
                  datagram
I/O multiplexing(select and poll)
                   application                      kernel
                                 System call
Process block       select                     No datagram ready
in a call to
select waiting                                                      Wait for
for one of                                                          data
possibly many                    Return readable
sockets to                                         Datagram ready
become readable                  System call
                    recvfrom                       copy datagram

Process blocks                                                      Copy data
while data                                                          from kernel
copied                           Return OK                           to user
into application    Process                        Copy complete
buffer              datagram
We first enable the socket for signal-driven I/O and 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. We can either read the datagram from the signal handler by calling
recvfrom and then notify the main loop that the data is ready to be processed
or we can notify the main loop and let it read the datagram.

Advantage:
 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(SIGIO)

                   application                          kernel
                                     Sigaction system call
                   Establish SIGIO
Process
continues           Signal handler
executing                                 Return                      Wait for
                                                                      data

                                     Deliver SIGIO
                   Signal handler                    Datagram ready
                                     System call     copy datagram
                     recvfrom                                         Copy data
Process blocks
while data                                                            from kernel
copied                           Return OK                             to user
into application     Process                         Copy complete
buffer               datagram
asynchronous I/O
We call aio_read and pass the kernel the descriptor, buffer pointer, buffer size ,file
offset, 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.

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

            application                      kernel
                          System call
              aio_read                     No datagram ready

                          Return                               Wait for
                                                               data
Process
continues                                    Datagram ready
executing                                    copy datagram     Copy data
                                                               from kernel
                                                                to user

               Signal     Deliver signal
                handler                       Copy complete
               Process     Specified in aio_read
               datagram
Comparison of the I/O Models
                     I/O           signal-driven asynchronous
blocking nonblocking multiplexing I/O            I/O

initiate   check            check                                  initiate
           check
           check
                                 blocked
                                                                                 wait for
           check                                                                data
           check
           check            ready           notification
                           initiate        initiate
                blocked




                               blocked




                                                blocked                          copy data
                                                                                from kernel
complete complete         complete         complete              notification   to user


      1st phase handled differently,                      handles both phases
     2nd phase handled the same
Synchronous I/O , Asynchronous I/O
 Synchronous I/O : cause the requesting process to be blocked until
 that I/O operation (recvfrom) completes.

 (blocking, nonblocking, I/O multiplexing, signal-driven I/O)

 Asynchronous I/O : does not cause the requesting process to be
 blocked
 (asynchronous I/O)
Select function
Allows the process to instruct the kernel to wait for any one 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.

(readable ,writable , expired time)
#include <sys/select.h>
   #include <sys/time.h>
   int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
   const struct timeval *);

Returns: positive count of ready descriptors, 0 on timeout, –1 on error

   struct timeval
   {
      long tv_sec; /* seconds */
      long tv_usec; /* microseconds */
    }
Condition of select function
  Wait forever : return only descriptor is
  ready(timeval = NULL)
  wait up to a fixed amount of time:
  Do not wait at all : return immediately after
  checking the descriptors(timeval = 0)

wait: normally interrupt if the process catches a
 signal and returns from the signal handler
Readset => descriptor for checking readable
writeset => descriptor for checking writable
exceptset => descriptor for checking
two exception conditions
:arrival of out of band data for a socket
:the presence of control status information to be
read from the master side of a pseudo terminal
Descriptor sets
Array of integers : each bit in each integer
correspond to a descriptor.
fd_set: an array of integers, with each bit in each integer corresponding to a
descriptor.

Void FD_ZERO(fd_set *fdset);         /* clear all bits in fdset */
Void FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fdset */
Void FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in fdset*/
int FD_ISSET(int fd, fd_set *fdset);/* is the bit for fd on in fdset ? */
Example of Descriptor sets
              function
fd_set rset;

FD_ZERO(&rset);/*all bits off : initiate*/
FD_SET(1, &rset);/*turn on bit fd 1*/
FD_SET(4, &rset); /*turn on bit fd 4*/
FD_SET(5, &rset); /*turn on bit fd 5*/
FD_S
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)(example:fd1,2,5 => maxfdp1: 6)
constant FD_SETSIZE defined by
including <sys/select.h>, is the number of
descriptors in the fd_set datatype.(1024)
socket is ready for reading : any one conditions is true

– 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. A read
    operation on the socket will not block and will return a value greater than 0.
       Set low-water mark : SO_RCVLOWAT socket option (defaults 1 :TCP and UDP)


– The read half of the connection is closed.

– The socket is a listening socket and the number of completed connections is
  nonzero.

– 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.
       These pending errors can also be fetched and cleared by calling getsockopt and specifying the
       SO_ERROR socket option.
socket is ready for writing : any one conditions is true
  – 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
         This means that if we set the socket to nonblocking, a write operation will not block and will return
         a positive value. We can set this low-water mark using the SO_SNDLOWAT socket option. low-
         water mark defaults to 2048 for TCP and UDP.


  – The write half of the connection is closed.

  – A socket using a non-blocking connect has completed the connection.

  – 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.
         These pending errors can also be fetched and cleared by calling getsockopt with the SO_ERROR
         socket option.
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.



The purpose of the receive and send low-water marks is to give the application
control over
how much data must be available for reading
how much space must be available for writing
For example, if we know that our application has nothing productive to do
unless at least 64 bytes of data are present, we can set the receive low-water
mark to 64 to prevent select from waking us up if less than 64 bytes are ready
for reading.
Condition that cause a socket to
               be ready for select

                Condition                   Readable? writable? Exception?
Data to read                                   •
read-half of the connection closed             •
new connection ready for listening socket      •
Space available for writing                               •
write-half of the connection closed                       •
Pending error                                   •         •

TCP out-of-band data                                                 •
Condition handled by select in str_cli

could be blocked in the call to fgets when
something happened on the socket

blocks in a call to select instead, waiting for
either standard input or the socket to be
readable.
Condition handled by select in str_cli

                             client

Data of EOF                                   Select for readability
                   • stdin
                                              on either standard
                               Socket
                                              input or socket
                                •

                error                   EOF




          TCP



                 RST         data        FIN
Three conditions are handled with the socket

 Peer TCP send a data, the socket become readable and read returns greater
 than 0

 Peer TCP send a FIN(peer process terminates), the socket become readable
 and read returns 0(end-of-file)

 Peer TCP send a RST(peer host has crashed and rebooted), the socket
 become readable and returns -1 and errno contains the specific error code
Implimentation of str_cli function using select

Void str_cli(FILE *fp, int sockfd)
{
        int      maxfdp1;
        fd_set rset;
        char     sendline[MAXLINE], recvline[MAXLINE];

       FD_ZERO(&rset);
       for ( ; ; ) {
                   FD_SET(fileno(fp), &rset);
                   FD_SET(sockfd, &rset);
                   maxfdp1 = max(fileno(fp), sockfd) + 1;
                   Select(maxfdp1, &rset, NULL, NULL, NULL);


Continue…..
if (FD_ISSET(sockfd, &rset)) {        /* socket is readable */
                               if (Readline(sockfd, recvline, MAXLINE) == 0)
                                        err_quit("str_cli: server terminated
prematurely");
                               Fputs(recvline, stdout);
                      }

                      if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */
                               if (Fgets(sendline, MAXLINE, fp) == NULL)
                                         return;          /* all done */
                               Writen(sockfd, sendline, strlen(sendline));
                      }
             }//for
}//str_cli
client           request
                           time0

                           time1             request
Stop and wait
sends a line to the servertime2
                                                        request
and then waits for the reply

                           time3                                  request   server


                           time4
                                                                  reply      server
                           time5
                                                         reply
                           time6
                                                reply
                           time7
                                   reply
Batch input

Time 7:
          request8 request7   request6 request5

          reply1   reply2     reply3   reply4

Time 7:
          request9 request8   request7 request6

          reply2   reply3     reply4   reply5
The problem with our revised str_cli function
– 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.
A way to close one-half of the TCP connection
– send a FIN to the server, telling it we have finished sending
  data, but leave the socket descriptor open for reading <=
  shutdown function
Shutdown function
 Close one half of the TCP connection
(example:send FIN to server, but leave the
 socket descriptor open for reading)

   Close function :
    decrements the descriptor’s reference count and closes the
    socket only if the count reaches 0,
    terminate both direction(reading and writing)

   Shutdown function : just one of them(reading or writing)
Calling shutdown to close half of a
                     TCP connection
            client                          server
                           data
       write
       write                data               Read returns > 0
   shutdown                  FIN               Read returns > 0
                                               Read returns 0
                     Ack of data and FIN

                            data                write
                                                write
Read returns > 0              data              close
Read returns > 0           FIN
Read returns 0        Ack of data and FIN
#include<sys/socket.h>
int shutdown(int sockfd, int howto);
/* return : 0 if OK, -1 on error */

howto argument
SHUT_RD : read-half of the connection closed
SHUT_WR : write-half of the connection closed
SHUT_RDWR : both closed
Str_cli function using select and
                    shutdown
#include "unp.h"
void str_cli(FILE *fp, int sockfd)
{
         int     maxfdp1, stdineof;
         fd_set rset;
         char    sendline[MAXLINE], recvline[MAXLINE];

        stdineof = 0;
        FD_ZERO(&rset);
        for ( ; ; ) {
                    if (stdineof == 0) // select on standard input for readability
                              FD_SET(fileno(fp), &rset);
                    FD_SET(sockfd, &rset);
                    maxfdp1 = max(fileno(fp), sockfd) + 1;
                    Select(maxfdp1, &rset, NULL, NULL, NULL);
Continue…..
if (FD_ISSET(sockfd, &rset)) {         /* socket is readable */
                 if (Readline(sockfd, recvline, MAXLINE) == 0) {
                           if (stdineof == 1)
                                     return;            /* normal termination */
                           else
                           err_quit("str_cli: server terminated prematurely");
                 }
                 Fputs(recvline, stdout);
        }
        if (FD_ISSET(fileno(fp), &rset))
        { /* input is readable */
                 if (Fgets(sendline, MAXLINE, fp) == NULL)
                 {
                           stdineof = 1;
                           Shutdown(sockfd, SHUT_WR); /* send FIN */
                           FD_CLR(fileno(fp), &rset);
                           continue;
                 }
                 Writen(sockfd, sendline, strlen(sendline));
        }
    }
}
TCP echo server
Rewrite the server as a single process that uses
select to handle any number of clients, instead
of forking one child per client.
Data structure TCP server(1)
    Before first client has established a connection

                  Client[]        fd0    fd1    fd2     fd3
            [0]      -1      rset: 0     0       0       1
            [1]      -1
            [2]      -1                 Maxfd + 1 = 4




                                   fd:0(stdin),1(stdout),2(stderr)
[FD_SETSIZE -1]      -1
                                   fd:3 => listening socket fd
Data structure TCP server(2)
    After first client connection is established

                   Client[]         fd0    fd1 fd2 fd3 fd4
             [0]      4        rset: 0     0    0   1   1
             [1]      -1
             [2]      -1                  Maxfd + 1 = 5




                                    * fd3 => listening socket fd
[FD_SETSIZE -1]       -1
                                     *fd4 => client socket fd
Data structure TCP server(3)
    After second client connection is established

                   Client[]        fd0    fd1 fd2 fd3 fd4 fd5
             [0]      4       rset: 0     0    0   1   1   1
             [1]      5
             [2]      -1                 Maxfd + 1 = 6




                                   * fd3 => listening socket fd
[FD_SETSIZE -1]       -1
                                   * fd4 => client1 socket fd
                                   * fd5 => client2 socket fd
Data structure TCP server(4)
       After first client terminates its connection

                  Client[]        fd0    fd1 fd2 fd3 fd4 fd5
            [0]      -1      rset: 0     0    0   1   0   1
            [1]      5
            [2]      -1                 Maxfd + 1 = 6


                                  *Maxfd does not change
                                   * fd3 => listening socket fd
[FD_SETSIZE -1]      -1
                                   * fd4 => client1 socket fd deleted
                                   * fd5 => client2 socket fd
TCP echo server using single
                    process
#include"unp.h"
int main(int argc, char **argv)
{
         int                        i, maxi, maxfd, listenfd, connfd, sockfd;
         int                        nready, client[FD_SETSIZE];
         ssize_t                    n;
         fd_set                     rset, allset;
         char                       line[MAXLINE];
         socklen_t                  clilen;
         struct sockaddr_in         cliaddr, servaddr;
         listenfd = Socket(AF_INET, SOCK_STREAM, 0);
         bzero(&servaddr, sizeof(servaddr));
         servaddr.sin_family     = AF_INET;
         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
         servaddr.sin_port      = htons(SERV_PORT);
         Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
         Listen(listenfd, LISTENQ);
maxfd = listenfd;                /* initialize */
maxi = -1;                                  /* index into client[] array */
for (i = 0; i < FD_SETSIZE; i++)
          client[i] = -1;                   /* -1 indicates available entry */
FD_ZERO(&allset);
FD_SET(listenfd, &allset);

for ( ; ; ) {
            rset = allset;           /* structure assignment */
            nready = Select(maxfd+1, &rset, NULL, NULL, NULL);

         if (FD_ISSET(listenfd, &rset)) { /* new client connection */
                  clilen = sizeof(cliaddr);
                  connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
for (i = 0; i < FD_SETSIZE; i++)
              if (client[i] < 0)
              {
                         client[i] = connfd; /* save descriptor */
                         break;
              }
    if (i == FD_SETSIZE)
              err_quit("too many clients");

    FD_SET(connfd, &allset); /* add new descriptor to set */
    if (connfd > maxfd)
              maxfd = connfd;                   /* for select */
    if (i > maxi)
              maxi = i;       /* max index in client[] array */

    if (--nready <= 0)
              continue;         /* no more readable descriptors */
}
for (i = 0; i <= maxi; i++)
    {         /* check all clients for data */
              if ( (sockfd = client[i]) < 0)
                        continue;
              if (FD_ISSET(sockfd, &rset)) {
                        if ( (n = Read(sockfd, line, MAXLINE)) == 0) {
                                            /*4connection closed by client */
                                  Close(sockfd);
                                  FD_CLR(sockfd, &allset);
                                  client[i] = -1;
                        } else
                                  write(sockfd, line, n);

                       if (--nready <= 0)
                                 break; /* no more readable descriptors */
             }
    }
}
Denial of service attacks
If Malicious client connect to the server,
send 1 byte of data(other than a newline),
and then goes to sleep.
=>call readline, server is blocked.
Solution
use nonblocking I/O
have each client serviced by a separate thread of
control (spawn a process or a thread to service
each client)
place a timeout on the I/O operation
Poll function
  Similar to select, but provide additional
  information when dealing with streams devices
  #include <poll.h>
  int poll(struct pollfd *fdarray, unsigned long
  nfds, int timeout);
/*return : count of ready descriptors, 0 on timeout,
  -1 on error*/
Struct pollfd{
 int fd; /* descriptor to check */
 short events; /* events of interest on fd */
 short revents;/*events that occurred on fd*/
}
specifies the conditions to be tested for a given
descriptor fd
events: the conditions to be tested
revents:the status of that descriptor
Input events and returned revents
              for poll

             Input to Result from
 Constant                                     Description
             events ? revents ?
POLLIN          •          •        Normal or priority band data can be read
POLLRDNORM      •          •        normal data can be read
POLLRDBAND      •          •        priority band data can be read
POLLPRI
                •          •        high-priority data can be read
POLLOUT          •         •        normal data can be written
POLLWRNORM       •         •        normal data can be written
POLLWRBAND       •         •        priority band data can be written

POLLERR                   •         An error has occurred
                                    hangup has occurred
POLLHUP                   •
POLLNVAL                            descriptor is not an open file
                          •
Timeout value for poll
Specifies how long the function is to wait before returning

   Timeout value                Description
      INFTIM         Wait forever
        0            Return immediately, do not block
         >0          Wait specified number of milliseconds


    If we are no longer interested in
    particular descriptor, just set the
    fd member of the pollfd structure
Np unit iv i
Np unit iv i

More Related Content

What's hot

RISC-V 30907 summit 2020 joint picocom_mentor
RISC-V 30907 summit 2020 joint picocom_mentorRISC-V 30907 summit 2020 joint picocom_mentor
RISC-V 30907 summit 2020 joint picocom_mentorRISC-V International
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card艾鍗科技
 
Online test program generator for RISC-V processors
Online test program generator for RISC-V processorsOnline test program generator for RISC-V processors
Online test program generator for RISC-V processorsRISC-V International
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack FirmwareSimen Li
 
Basics of digital verilog design(alok singh kanpur)
Basics of digital verilog design(alok singh kanpur)Basics of digital verilog design(alok singh kanpur)
Basics of digital verilog design(alok singh kanpur)Alok Singh
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20DefconRussia
 
Unit 2
Unit 2Unit 2
Unit 2siddr
 
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]RootedCON
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackKernel TLV
 
Linux cnc overview
Linux cnc overviewLinux cnc overview
Linux cnc overviewNylon
 
eBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureeBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureNetronome
 
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
An Open Discussion of RISC-V BitManip, trends, and comparisons _ ClaireRISC-V International
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver艾鍗科技
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT CompilerNetronome
 
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- MulticoreLec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- MulticoreHsien-Hsin Sean Lee, Ph.D.
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in ElixirAaron Seigo
 
F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1Benux Wei
 
Me3D: A Model-driven Methodology Expediting Embedded Device Driver Development
Me3D: A Model-driven Methodology  Expediting Embedded Device  Driver DevelopmentMe3D: A Model-driven Methodology  Expediting Embedded Device  Driver Development
Me3D: A Model-driven Methodology Expediting Embedded Device Driver Developmenthuichenphd
 

What's hot (20)

RISC-V 30907 summit 2020 joint picocom_mentor
RISC-V 30907 summit 2020 joint picocom_mentorRISC-V 30907 summit 2020 joint picocom_mentor
RISC-V 30907 summit 2020 joint picocom_mentor
 
建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card建構嵌入式Linux系統於SD Card
建構嵌入式Linux系統於SD Card
 
Linux interrupts
Linux interruptsLinux interrupts
Linux interrupts
 
Online test program generator for RISC-V processors
Online test program generator for RISC-V processorsOnline test program generator for RISC-V processors
Online test program generator for RISC-V processors
 
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee 應用實作 - 使用 TI Z-Stack Firmware
 
Basics of digital verilog design(alok singh kanpur)
Basics of digital verilog design(alok singh kanpur)Basics of digital verilog design(alok singh kanpur)
Basics of digital verilog design(alok singh kanpur)
 
Zn task - defcon russia 20
Zn task  - defcon russia 20Zn task  - defcon russia 20
Zn task - defcon russia 20
 
Unit 2
Unit 2Unit 2
Unit 2
 
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network Stack
 
Linux cnc overview
Linux cnc overviewLinux cnc overview
Linux cnc overview
 
eBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging InfrastructureeBPF Tooling and Debugging Infrastructure
eBPF Tooling and Debugging Infrastructure
 
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
An Open Discussion of RISC-V BitManip, trends, and comparisons _ Claire
 
Verilog HDL
Verilog HDLVerilog HDL
Verilog HDL
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver
 
Demystify eBPF JIT Compiler
Demystify eBPF JIT CompilerDemystify eBPF JIT Compiler
Demystify eBPF JIT Compiler
 
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- MulticoreLec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
Lec13 Computer Architecture by Hsien-Hsin Sean Lee Georgia Tech -- Multicore
 
Bare metal performance in Elixir
Bare metal performance in ElixirBare metal performance in Elixir
Bare metal performance in Elixir
 
F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1F9 Microkernel code reading - part 1
F9 Microkernel code reading - part 1
 
Me3D: A Model-driven Methodology Expediting Embedded Device Driver Development
Me3D: A Model-driven Methodology  Expediting Embedded Device  Driver DevelopmentMe3D: A Model-driven Methodology  Expediting Embedded Device  Driver Development
Me3D: A Model-driven Methodology Expediting Embedded Device Driver Development
 

Viewers also liked (7)

Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Np unit iii
Np unit iiiNp unit iii
Np unit iii
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Cookies
CookiesCookies
Cookies
 
Unit 1wt
Unit 1wtUnit 1wt
Unit 1wt
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Np unit2
Np unit2Np unit2
Np unit2
 

Similar to Np unit iv i

W5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptW5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptiqrayounus5
 
SystemCallsAndInvocationMethods_Mayin074.pptx
SystemCallsAndInvocationMethods_Mayin074.pptxSystemCallsAndInvocationMethods_Mayin074.pptx
SystemCallsAndInvocationMethods_Mayin074.pptxBlackGoku18
 
Dot Net Application Monitoring
Dot Net Application MonitoringDot Net Application Monitoring
Dot Net Application MonitoringRavi Okade
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in indiaEdhole.com
 
Inter Task Communication On Volatile Nodes
Inter Task Communication On Volatile NodesInter Task Communication On Volatile Nodes
Inter Task Communication On Volatile Nodesnagarajan_ka
 
Flow control in computer
Flow control in computerFlow control in computer
Flow control in computerrud_d_rcks
 
NoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATNoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATHenryBowers
 
System Calls - Introduction
System Calls - IntroductionSystem Calls - Introduction
System Calls - IntroductionTo Sum It Up
 
Dcs control workshop 2002
Dcs control workshop 2002Dcs control workshop 2002
Dcs control workshop 2002akshit000
 
03. top level view of computer function &amp; interconnection
03. top level view of computer function &amp; interconnection03. top level view of computer function &amp; interconnection
03. top level view of computer function &amp; interconnectionnoman yasin
 
Handling Kernel Upgrades at Scale - The Dirty Cow Story
Handling Kernel Upgrades at Scale - The Dirty Cow StoryHandling Kernel Upgrades at Scale - The Dirty Cow Story
Handling Kernel Upgrades at Scale - The Dirty Cow StoryDataWorks Summit
 
Process management
Process managementProcess management
Process managementBirju Tank
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍jeffz
 

Similar to Np unit iv i (20)

W5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.pptW5 system call, DD, OS structure.ppt
W5 system call, DD, OS structure.ppt
 
SystemCallsAndInvocationMethods_Mayin074.pptx
SystemCallsAndInvocationMethods_Mayin074.pptxSystemCallsAndInvocationMethods_Mayin074.pptx
SystemCallsAndInvocationMethods_Mayin074.pptx
 
Ch13 annotated
Ch13 annotatedCh13 annotated
Ch13 annotated
 
Chapter03
Chapter03Chapter03
Chapter03
 
Process
ProcessProcess
Process
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Replistor Resume
Replistor ResumeReplistor Resume
Replistor Resume
 
Dot Net Application Monitoring
Dot Net Application MonitoringDot Net Application Monitoring
Dot Net Application Monitoring
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Inter Task Communication On Volatile Nodes
Inter Task Communication On Volatile NodesInter Task Communication On Volatile Nodes
Inter Task Communication On Volatile Nodes
 
Flow control in computer
Flow control in computerFlow control in computer
Flow control in computer
 
OS - Process
OS - ProcessOS - Process
OS - Process
 
NoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RATNoCOUG Presentation on Oracle RAT
NoCOUG Presentation on Oracle RAT
 
System Calls - Introduction
System Calls - IntroductionSystem Calls - Introduction
System Calls - Introduction
 
Dcs control workshop 2002
Dcs control workshop 2002Dcs control workshop 2002
Dcs control workshop 2002
 
03. top level view of computer function &amp; interconnection
03. top level view of computer function &amp; interconnection03. top level view of computer function &amp; interconnection
03. top level view of computer function &amp; interconnection
 
Fault tolerance
Fault toleranceFault tolerance
Fault tolerance
 
Handling Kernel Upgrades at Scale - The Dirty Cow Story
Handling Kernel Upgrades at Scale - The Dirty Cow StoryHandling Kernel Upgrades at Scale - The Dirty Cow Story
Handling Kernel Upgrades at Scale - The Dirty Cow Story
 
Process management
Process managementProcess management
Process management
 
Windows内核技术介绍
Windows内核技术介绍Windows内核技术介绍
Windows内核技术介绍
 

More from vamsitricks (19)

Unit 6
Unit 6Unit 6
Unit 6
 
Np unit1
Np unit1Np unit1
Np unit1
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
Unit 7
Unit 7Unit 7
Unit 7
 
Npc16
Npc16Npc16
Npc16
 
Npc14
Npc14Npc14
Npc14
 
Npc13
Npc13Npc13
Npc13
 
Npc08
Npc08Npc08
Npc08
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 5
Unit 5Unit 5
Unit 5
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit 7
Unit 7Unit 7
Unit 7
 
Unit 6
Unit 6Unit 6
Unit 6
 
Unit 4
Unit 4Unit 4
Unit 4
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Javabeans
JavabeansJavabeans
Javabeans
 
Javabeanproperties
JavabeanpropertiesJavabeanproperties
Javabeanproperties
 

Np unit iv i

  • 2. abstract Introduction I/O Models Synchronous I/O versus Asynchronous I/O select function shutdown function pselect function poll function
  • 3. Introduction TCP client is handling two inputs : standard input and a TCP socket – when the client was blocked in a call to read, the server process was killed – server TCP sends FIN to the client TCP, but the client never see FIN since the client is blocked reading from standard input => the capability to tell the kernel that we want to be notified if one or more I/O conditions are ready. : I/O multiplexing (select and poll)
  • 4. When: – client is handling multiple descriptors (interactive input and a network socket). – Client to handle multiple sockets(rare) – TCP server handles both a listening socket and its connected socket. – Server handle both TCP and UDP. – Server handles multiple services and multiple protocols
  • 5. I/O Models Blocking I/O Nonblocking I/O I/O multiplexing(select and poll) signal driven I/O(SIGIO) asynchronous I/O(posix.1 aio_ functions) Two distinct phases for an input operations 1. Waiting for the data to be ready 2. Copying the data from the kernel to the process
  • 6. Blocking I/O application kernel System call recvfrom No datagram ready Wait for data Process blocks in a call to Datagram ready recvfrom Copy data copy datagram from kernel to user Return OK Process Copy complete datagram
  • 7. nonblocking I/O application kernel System call recvfrom No datagram ready EWOULDBLOCK System call recvfrom No datagram ready EWOULDBLOCK Wait for data Process System call repeatedly recvfrom datagram ready call recvfrom copy datagram wating for an OK return Copy data (polling) from Return OK kernel to user Process application datagram
  • 8. I/O multiplexing(select and poll) application kernel System call Process block select No datagram ready in a call to select waiting Wait for for one of data possibly many Return readable sockets to Datagram ready become readable System call recvfrom copy datagram Process blocks Copy data while data from kernel copied Return OK to user into application Process Copy complete buffer datagram
  • 9. We first enable the socket for signal-driven I/O and 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. We can either read the datagram from the signal handler by calling recvfrom and then notify the main loop that the data is ready to be processed or we can notify the main loop and let it read the datagram. Advantage: 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.
  • 10. signal driven I/O(SIGIO) application kernel Sigaction system call Establish SIGIO Process continues Signal handler executing Return Wait for data Deliver SIGIO Signal handler Datagram ready System call copy datagram recvfrom Copy data Process blocks while data from kernel copied Return OK to user into application Process Copy complete buffer datagram
  • 11. asynchronous I/O We call aio_read and pass the kernel the descriptor, buffer pointer, buffer size ,file offset, 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. 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.
  • 12. asynchronous I/O application kernel System call aio_read No datagram ready Return Wait for data Process continues Datagram ready executing copy datagram Copy data from kernel to user Signal Deliver signal handler Copy complete Process Specified in aio_read datagram
  • 13. Comparison of the I/O Models I/O signal-driven asynchronous blocking nonblocking multiplexing I/O I/O initiate check check initiate check check blocked wait for check data check check ready notification initiate initiate blocked blocked blocked copy data from kernel complete complete complete complete notification to user 1st phase handled differently, handles both phases 2nd phase handled the same
  • 14. Synchronous I/O , Asynchronous I/O Synchronous I/O : cause the requesting process to be blocked until that I/O operation (recvfrom) completes. (blocking, nonblocking, I/O multiplexing, signal-driven I/O) Asynchronous I/O : does not cause the requesting process to be blocked (asynchronous I/O)
  • 15. Select function Allows the process to instruct the kernel to wait for any one 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. (readable ,writable , expired time)
  • 16. #include <sys/select.h> #include <sys/time.h> int select (int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, const struct timeval *); Returns: positive count of ready descriptors, 0 on timeout, –1 on error struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ }
  • 17. Condition of select function Wait forever : return only descriptor is ready(timeval = NULL) wait up to a fixed amount of time: Do not wait at all : return immediately after checking the descriptors(timeval = 0) wait: normally interrupt if the process catches a signal and returns from the signal handler
  • 18. Readset => descriptor for checking readable writeset => descriptor for checking writable exceptset => descriptor for checking two exception conditions :arrival of out of band data for a socket :the presence of control status information to be read from the master side of a pseudo terminal
  • 19. Descriptor sets Array of integers : each bit in each integer correspond to a descriptor. fd_set: an array of integers, with each bit in each integer corresponding to a descriptor. Void FD_ZERO(fd_set *fdset); /* clear all bits in fdset */ Void FD_SET(int fd, fd_set *fdset); /* turn on the bit for fd in fdset */ Void FD_CLR(int fd, fd_set *fdset); /* turn off the bit for fd in fdset*/ int FD_ISSET(int fd, fd_set *fdset);/* is the bit for fd on in fdset ? */
  • 20. Example of Descriptor sets function fd_set rset; FD_ZERO(&rset);/*all bits off : initiate*/ FD_SET(1, &rset);/*turn on bit fd 1*/ FD_SET(4, &rset); /*turn on bit fd 4*/ FD_SET(5, &rset); /*turn on bit fd 5*/ FD_S
  • 21. 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)(example:fd1,2,5 => maxfdp1: 6) constant FD_SETSIZE defined by including <sys/select.h>, is the number of descriptors in the fd_set datatype.(1024)
  • 22. socket is ready for reading : any one conditions is true – 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. A read operation on the socket will not block and will return a value greater than 0. Set low-water mark : SO_RCVLOWAT socket option (defaults 1 :TCP and UDP) – The read half of the connection is closed. – The socket is a listening socket and the number of completed connections is nonzero. – 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. These pending errors can also be fetched and cleared by calling getsockopt and specifying the SO_ERROR socket option.
  • 23. socket is ready for writing : any one conditions is true – 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 This means that if we set the socket to nonblocking, a write operation will not block and will return a positive value. We can set this low-water mark using the SO_SNDLOWAT socket option. low- water mark defaults to 2048 for TCP and UDP. – The write half of the connection is closed. – A socket using a non-blocking connect has completed the connection. – 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. These pending errors can also be fetched and cleared by calling getsockopt with the SO_ERROR socket option.
  • 24. 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. The purpose of the receive and send low-water marks is to give the application control over how much data must be available for reading how much space must be available for writing For example, if we know that our application has nothing productive to do unless at least 64 bytes of data are present, we can set the receive low-water mark to 64 to prevent select from waking us up if less than 64 bytes are ready for reading.
  • 25. Condition that cause a socket to be ready for select Condition Readable? writable? Exception? Data to read • read-half of the connection closed • new connection ready for listening socket • Space available for writing • write-half of the connection closed • Pending error • • TCP out-of-band data •
  • 26. Condition handled by select in str_cli could be blocked in the call to fgets when something happened on the socket blocks in a call to select instead, waiting for either standard input or the socket to be readable.
  • 27. Condition handled by select in str_cli client Data of EOF Select for readability • stdin on either standard Socket input or socket • error EOF TCP RST data FIN
  • 28. Three conditions are handled with the socket Peer TCP send a data, the socket become readable and read returns greater than 0 Peer TCP send a FIN(peer process terminates), the socket become readable and read returns 0(end-of-file) Peer TCP send a RST(peer host has crashed and rebooted), the socket become readable and returns -1 and errno contains the specific error code
  • 29. Implimentation of str_cli function using select Void str_cli(FILE *fp, int sockfd) { int maxfdp1; fd_set rset; char sendline[MAXLINE], recvline[MAXLINE]; FD_ZERO(&rset); for ( ; ; ) { FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockfd) + 1; Select(maxfdp1, &rset, NULL, NULL, NULL); Continue…..
  • 30. if (FD_ISSET(sockfd, &rset)) { /* socket is readable */ if (Readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prematurely"); Fputs(recvline, stdout); } if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ if (Fgets(sendline, MAXLINE, fp) == NULL) return; /* all done */ Writen(sockfd, sendline, strlen(sendline)); } }//for }//str_cli
  • 31. client request time0 time1 request Stop and wait sends a line to the servertime2 request and then waits for the reply time3 request server time4 reply server time5 reply time6 reply time7 reply
  • 32. Batch input Time 7: request8 request7 request6 request5 reply1 reply2 reply3 reply4 Time 7: request9 request8 request7 request6 reply2 reply3 reply4 reply5
  • 33. The problem with our revised str_cli function – 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. A way to close one-half of the TCP connection – send a FIN to the server, telling it we have finished sending data, but leave the socket descriptor open for reading <= shutdown function
  • 34. Shutdown function Close one half of the TCP connection (example:send FIN to server, but leave the socket descriptor open for reading) Close function : decrements the descriptor’s reference count and closes the socket only if the count reaches 0, terminate both direction(reading and writing) Shutdown function : just one of them(reading or writing)
  • 35. Calling shutdown to close half of a TCP connection client server data write write data Read returns > 0 shutdown FIN Read returns > 0 Read returns 0 Ack of data and FIN data write write Read returns > 0 data close Read returns > 0 FIN Read returns 0 Ack of data and FIN
  • 36. #include<sys/socket.h> int shutdown(int sockfd, int howto); /* return : 0 if OK, -1 on error */ howto argument SHUT_RD : read-half of the connection closed SHUT_WR : write-half of the connection closed SHUT_RDWR : both closed
  • 37. Str_cli function using select and shutdown #include "unp.h" void str_cli(FILE *fp, int sockfd) { int maxfdp1, stdineof; fd_set rset; char sendline[MAXLINE], recvline[MAXLINE]; stdineof = 0; FD_ZERO(&rset); for ( ; ; ) { if (stdineof == 0) // select on standard input for readability FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); maxfdp1 = max(fileno(fp), sockfd) + 1; Select(maxfdp1, &rset, NULL, NULL, NULL); Continue…..
  • 38. if (FD_ISSET(sockfd, &rset)) { /* socket is readable */ if (Readline(sockfd, recvline, MAXLINE) == 0) { if (stdineof == 1) return; /* normal termination */ else err_quit("str_cli: server terminated prematurely"); } Fputs(recvline, stdout); } if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ if (Fgets(sendline, MAXLINE, fp) == NULL) { stdineof = 1; Shutdown(sockfd, SHUT_WR); /* send FIN */ FD_CLR(fileno(fp), &rset); continue; } Writen(sockfd, sendline, strlen(sendline)); } } }
  • 39. TCP echo server Rewrite the server as a single process that uses select to handle any number of clients, instead of forking one child per client.
  • 40. Data structure TCP server(1) Before first client has established a connection Client[] fd0 fd1 fd2 fd3 [0] -1 rset: 0 0 0 1 [1] -1 [2] -1 Maxfd + 1 = 4 fd:0(stdin),1(stdout),2(stderr) [FD_SETSIZE -1] -1 fd:3 => listening socket fd
  • 41. Data structure TCP server(2) After first client connection is established Client[] fd0 fd1 fd2 fd3 fd4 [0] 4 rset: 0 0 0 1 1 [1] -1 [2] -1 Maxfd + 1 = 5 * fd3 => listening socket fd [FD_SETSIZE -1] -1 *fd4 => client socket fd
  • 42. Data structure TCP server(3) After second client connection is established Client[] fd0 fd1 fd2 fd3 fd4 fd5 [0] 4 rset: 0 0 0 1 1 1 [1] 5 [2] -1 Maxfd + 1 = 6 * fd3 => listening socket fd [FD_SETSIZE -1] -1 * fd4 => client1 socket fd * fd5 => client2 socket fd
  • 43. Data structure TCP server(4) After first client terminates its connection Client[] fd0 fd1 fd2 fd3 fd4 fd5 [0] -1 rset: 0 0 0 1 0 1 [1] 5 [2] -1 Maxfd + 1 = 6 *Maxfd does not change * fd3 => listening socket fd [FD_SETSIZE -1] -1 * fd4 => client1 socket fd deleted * fd5 => client2 socket fd
  • 44. TCP echo server using single process #include"unp.h" int main(int argc, char **argv) { int i, maxi, maxfd, listenfd, connfd, sockfd; int nready, client[FD_SETSIZE]; ssize_t n; fd_set rset, allset; char line[MAXLINE]; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; listenfd = Socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); Listen(listenfd, LISTENQ);
  • 45. maxfd = listenfd; /* initialize */ maxi = -1; /* index into client[] array */ for (i = 0; i < FD_SETSIZE; i++) client[i] = -1; /* -1 indicates available entry */ FD_ZERO(&allset); FD_SET(listenfd, &allset); for ( ; ; ) { rset = allset; /* structure assignment */ nready = Select(maxfd+1, &rset, NULL, NULL, NULL); if (FD_ISSET(listenfd, &rset)) { /* new client connection */ clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);
  • 46. for (i = 0; i < FD_SETSIZE; i++) if (client[i] < 0) { client[i] = connfd; /* save descriptor */ break; } if (i == FD_SETSIZE) err_quit("too many clients"); FD_SET(connfd, &allset); /* add new descriptor to set */ if (connfd > maxfd) maxfd = connfd; /* for select */ if (i > maxi) maxi = i; /* max index in client[] array */ if (--nready <= 0) continue; /* no more readable descriptors */ }
  • 47. for (i = 0; i <= maxi; i++) { /* check all clients for data */ if ( (sockfd = client[i]) < 0) continue; if (FD_ISSET(sockfd, &rset)) { if ( (n = Read(sockfd, line, MAXLINE)) == 0) { /*4connection closed by client */ Close(sockfd); FD_CLR(sockfd, &allset); client[i] = -1; } else write(sockfd, line, n); if (--nready <= 0) break; /* no more readable descriptors */ } } }
  • 48. Denial of service attacks If Malicious client connect to the server, send 1 byte of data(other than a newline), and then goes to sleep. =>call readline, server is blocked.
  • 49. Solution use nonblocking I/O have each client serviced by a separate thread of control (spawn a process or a thread to service each client) place a timeout on the I/O operation
  • 50. Poll function Similar to select, but provide additional information when dealing with streams devices #include <poll.h> int poll(struct pollfd *fdarray, unsigned long nfds, int timeout); /*return : count of ready descriptors, 0 on timeout, -1 on error*/
  • 51. Struct pollfd{ int fd; /* descriptor to check */ short events; /* events of interest on fd */ short revents;/*events that occurred on fd*/ } specifies the conditions to be tested for a given descriptor fd events: the conditions to be tested revents:the status of that descriptor
  • 52. Input events and returned revents for poll Input to Result from Constant Description events ? revents ? POLLIN • • Normal or priority band data can be read POLLRDNORM • • normal data can be read POLLRDBAND • • priority band data can be read POLLPRI • • high-priority data can be read POLLOUT • • normal data can be written POLLWRNORM • • normal data can be written POLLWRBAND • • priority band data can be written POLLERR • An error has occurred hangup has occurred POLLHUP • POLLNVAL descriptor is not an open file •
  • 53. Timeout value for poll Specifies how long the function is to wait before returning Timeout value Description INFTIM Wait forever 0 Return immediately, do not block >0 Wait specified number of milliseconds If we are no longer interested in particular descriptor, just set the fd member of the pollfd structure