gtpc2m66 | C/C++ Language Support User's Guide |
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);
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.
If an error occurs while monitoring a nonsocket file descriptor, errno is set to the following:
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.