The Open Source Swiss Army Knife

/code/c/unix_c/select/
/code/c/unix_c/select/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /c   /unix_c   /select 
Permalink: select.htm
Title: sockets and select call order
article options : please login   |  print view

  1. introduction to select()
  2. syntax
  3. proper usage

introduction to select()

The select function call allows a process to wait on multiple file descriptors simultanously with an optional timeout. The select() call will return as soon as it is possible to perform operations on any of the indicated file descriptors. This allows a process to perform some basic multi-tasking without forking another process or starting another thread.

It also does not hogtie the CPU. the CPU is hogtied when using blocking IO such as the following:

blocking I/O means the use of the cpu is BLOCKED

while (( z= read(fd, buf, sizeof buf)) == -1 && errno==EAGAIN) ;

instead, we want to relinquish the CPU for a little while

So, it used whenever there are sub-processes (each with a file descriptor -- i don't think these are really called file descriptors). For example, FIFO pipes created with popen("ls","r");

while we are here, other ways to go around blocking IO are:

  • a separate process monitors each file descriptor: synchronous IO
  • select: asynchronous IO with monitoring (nonblocking)
  • poll: asynchronous IO with monitoring (nonblocking)
  • POSIX asynchronous IO (complicated code! what is this?!)
  • threads: synchronous IO

syntax

  NAME
     select, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous
     I/O multiplexing

  SYNOPSIS
     #include
     #include
     #include
    
     int select(int n, fd_set *readfds, fd_set *writefds,
     fd_set *exceptfds, struct timeval *timeout);
    
     FD_CLR(int fd, fd_set *set);
     FD_ISSET(int fd, fd_set *set);
     FD_SET(int fd, fd_set *set);
     FD_ZERO(fd_set *set);
    
    

  1. set the first argument to select = nu of filedes +1
  2. fd_set manipulations (see below)
  3. timeout value: indicates when teh select call should give up and return zero. if a timeout is not required, NULL is
  • FD_ZERO
  • FD_SET
  • use of FD_ISSET with FD_SET
    • create the usual bind and listen calls on a new socket
      • FD_ZERO
      • FD_SET
    • store the max file descriptor in another variable by assignment
    • it is important to maintain two sets of file descriptors. create the first outside the for loop
    • inside top of for loop,
  • check return values from select statement
  • check FD_ISSET(socket=s, & second file descriptor set);

    return values:

    1. -1 if call failed (check errno)
    2. 0 in case of a timeout
    3. > 0 , and the number returned will be the number of file descriptors which have changed.

      proper usage of select()

      1. making a call to select will ruin the filedescriptor sets! everytime before select is called, it is necessary to do FD_ZERO, etc. (populate sets)

      basic strategy: note that simple examples, will do the FD_* macros, issue a simple select which reads from a fd or socket-fd, then issue a read/write or exit. Anything more practical will undoubtedly be involved in multiple read/write loops. Thus a skeleton implementation is here:
      do {
      FD_ZERO....
      FD_SET....
      WRONG: (while ((rv = select() > 0) {
      RIGHT: while (( rv = select() == -1 && errno=EINTR) ....
      if (rv == -1) quit;
      if (rv == 0 ) // timeout; handle how see fit
      if (FD_ISSET .... and query all file descriptors in the fd_sets.
      }
      sometimes teh above is implemented using for(;;)
      the purpose of the loop is to, like I said, to multiple reads/writes as in any application, server or client, but we need simultaneously to reinitialize the FD_ macros.

      there is a sequence of steps for interacting with sockets and the select call

      serverclient
      fd = socket()NA
      listen(fd)NA
      NAconnect
      triggers read event, so server must then call acceptNA
      readNA
      NAEOF/closed socket
      NANA

      Leave a Reply
      Your Name:     anonymous
      Your Email:
      Website:  
      Comments:

      The author will be notified of your reply.
      return to top