gtpc2m66C/C++ Language Support User's Guide

select-Monitor Read, Write, and Exception Status

The select function monitors a list of file descriptors for readability, readiness for writing, and exception pending conditions. The list can contain nonsocket file descriptors, socket descriptors, or a combination of both.

Format

#include  <socket.h>
int       select(int *s,
                 short int noreads,
                 short int nowrites,
                 short int noexcepts,
                 long  int timeout);

s
Pointer to an array containing a list of file descriptors to check for readability, followed by a list of file descriptors to check for readiness for writing, followed by a list of file descriptors to check for exception pending conditions. The array can contain nonsocket file descriptors, socket descriptors, or a combination of both.

noreads
Number of file descriptors to be checked for readability.

nowrites
Number of file descriptors to be checked for readiness for writing.

noexcepts
Number of file descriptors to be checked for exception pending conditions.

timeout
Maximum interval, in milliseconds, to wait for the selection to be completed.

If the timeout value is 0, select does not wait before returning to the caller. If the timeout value is -1, select does not time out, but it returns when a file descriptor becomes ready. If the timeout value is a number of milliseconds, select waits for the specified interval before returning to the caller.

Normal Return

The total number of ready file descriptors. A value of 0 indicates an expired time limit. If the return value is greater than 0, the file descriptors in the s argument that were not ready are set to -1.

Error Return

A value of -1.

If an error occurs while monitoring a socket descriptor, errno and sock_errno are set to one of the following error codes. Unless otherwise stated in the description, the following error codes can be returned for either TCP/IP offload support or TCP/IP native stack support.

SOCFAULT
One of the following occurred:

SOCNOTSOCK
One of the sockets descriptors specified in the file descriptor array is not valid.

E1052STATE
The socket was closed because the system was in or cycling down to 1052 state.

EINACT
All offload devices associated with the socket descriptor have been disconnected. The socket is closed. This error code is returned only for TCP/IP offload support.

EINACTWS
An offload device associated with the socket descriptor has been disconnected. The socket is still available. This error code is returned only for TCP/IP offload support.

ESYSTEMERROR
A system error has occurred and closed the socket. This error code is returned only for TCP/IP offload support.

EIBMIUCVERR
An error occurred while the function call was sent to the offload device. This error code is returned only for TCP/IP offload support.

If an error occurs while monitoring a nonsocket file descriptor, errno is set to the following:

EBADF
One or more of the file descriptor sets specified a file descriptor that is not a valid open file descriptor or a file descriptor that is not supported through the select function.

Programming Considerations

Examples

The following example monitors a FIFO special file (or named pipe) and a socket for incoming messages.

#define MSG_BUFF 4096
#define TIMERINT 30
#define MAX_SELECT_FD 20
#define SELECT_TIMEOUT 2*1000
 
#include <sys/socket.h>
#include <sys/signal.h>
#include <stdio.h>
#include <fcntl.h>
 
void SigAlrmH(int SIG_TYPE)    /* Handle alarm */
 
{
  char incoming_data[MSG_BUFF + 1];
  int bytes_in, i;
 
  int fd_fifo;                /* mkfifo file descriptor */
  char fifopath[] = "/tmp/my_fifo";
 
  int sd_inet;                /* socket descriptor */
  int slen;
  struct sockaddr_in sin, sockinet;
 
  int select_array[MAX_SELECT_FD];
  int nfds, Rfd, Wfd, Xfd;
 
  signal(SIGALRM, SigAlrmH);
  alarm(TIMERINT);
 
  /* Open the named pipe. */
  if ((mkfifo(fifopath, 777)) < 0)
     /* Handle the mkfifo error. */
 
  if ((fd_fifo = open(fifopath, O_RDONLY, 0)) < 0)
     /* Handle the open error. */
 
  /* Open the socket. */
  if ((sd_inet = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
     /* Handle socket error. */
  /* Set sockaddr parameters. */
  if ((bind(sd_inet, (struct sockaddr *)&sin;, sizeof(sin))) < 0)
     /* Handle bind error. */
 
  for (;;) {
    /* Initialize select Parameters */
    select_array[0] = fd_fifo;
    select_array[1] = sd_inet;
    Rfd = 2;
    Wfd = 0;
    Xfd = 0;
 
    /* Enable signal interrupts to accept any timeout alarm.  */
    tpf_process_signals();
 
    /* Wait for incoming data, select timeout, or external alarm. */
    nfds = select(select_array, Rfd, Wfd, Xfd, SELECT_TIMEOUT);
    if (nfds == 0)    /* Process the select timeout condition. */
    if (nfds < 0)  /* Process the error condition. */
 
    /* Process the readable file or socket descriptors */
    for (i=0; i<nfds; i++) {
      if (select_array[i] == fd_fifo ) {
        bytes_in = read(fd_fifo, incoming_data, MSG_BUFF);
        /* Process the data from the named pipe. */
      }
      if (select_array[i] == sd_inet) {
        slen = sizeof sockinet;
        bytes_in = recvfrom(sd_inet, incoming_data, MSG_BUFF, 0,
                     (struct sockaddr *) &sockinet, &slen);
        /* Process the socket data. */
      }
    }  /* end for loop */
  }    /* end for loop */
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about nonsocket file descriptors. See TPF Transmission Control Protocol/Internet Protocol for more information about socket file descriptors.