The use of the Select () function is described in the 63.2.1 the Select () System Call of the "the Linux programming Interface"
Here's the prototype:
1 #include <sys/time.h> /* Span style= "color: #008000;" > for portability */ 2 #include <sys/select . H>int select (int Nfds, Fd_set * Readfds, Fd_set *writefds, fd_set *exceptfds, 4 struct timeval *timeout); 5 6 Returns number of Ready file Descriptors, 0 on timeout, or–1 On Error
View Code
Where $Readfds, $Writefds, $Exceptfds are handed to select monitor for read, write, exception file descriptors, timeout specifies the time period for monitoring,
That is, in the time period of timeout, if a set file descriptor device is ready, it returns immediately, otherwise the set device is no longer monitored after time-out.
The $Nfds argument must be set one greater than the highest file descriptor
Number included in any of the three file descriptor sets. This argument allows
Select () To is more efficient, since the kernel then knows not to check whether file
Descriptor numbers higher than this value is part of the each file descriptor set.
There are 4 macros for the relationship between the corresponding action file descriptor and the file descriptor collection:
1#include <sys/Select.h>2 voidFd_zero (Fd_set *fdset);3 voidFd_set (intFD, Fd_set *fdset);4 voidFD_CLR (intFD, Fd_set *fdset);5 intFd_isset (intFD, Fd_set *fdset);6Returnstrue(1)ifFd is inchFdset, orfalse(0) otherwise
View Code
$FD _zero () initializes the set pointed to by Fdset to be empty.
$FD _set () adds the file descriptor FD to the set pointed to by Fdset.
$FD _clr () removes the file descriptor FD from the set pointed to by Fdset.
$FD _isset () returns True if the file descriptor FD is a member of the set pointed to
by Fdset.
The following is the sample code in the book:
1 /*************************************************************************2 * Copyright (C) Michael Kerrisk, 2015. *3 * *4 * This program is a free software. Modify, and redistribute it *5 * Under the terms of the GNU general public License as published by the *6 * Free software Foundation, either version 3 or (at your option) any *7 * later version. Distributed without any warranty. See *8 * The file copying.gpl-v3 for details. *9 \*************************************************************************/Ten One /*Listing 63-1*/ A - /*t_select.c - the Example of the use of the Select () system call to monitor multiple - file descriptors. - - Usage as shown in Usageerror (). + */ -#include <sys/time.h> + #if! Defined (__HPUX) A /*HP-UX One-doesn ' t has this header file*/ at#include <sys/Select.h> - #endif -#include"Tlpi_hdr.h" - - Static void -Usageerror (Const Char*progname) in { -fprintf (stderr,"Usage:%s {timeout|-} fd-num[rw]...\n", progname); tofprintf (stderr,"-means infinite timeout; \ n"); +fprintf (stderr,"r = Monitor for read\n"); -fprintf (stderr,"w = monitor for write\n\n"); thefprintf (stderr,"e.g.:%S-0RW 1w\n", progname); * exit (exit_failure); $ }Panax Notoginseng - int theMainintargcChar*argv[]) + { AFd_set Readfds, Writefds;//read, write file descriptor the intReady , Nfds, FD, Numread, J; + structTimeval timeout;//timed out - structTimeval *PTO; $ Charbuf[Ten];/*Large enough to hold "rw\0"*/ $ - if(ARGC <2|| strcmp (argv[1],"--help") ==0) -Usageerror (argv[0]); the - /*Timeout for Select () was specified in argv[1]*/Wuyi the if(strcmp (argv[1],"-") ==0) { -PTO = NULL;/*Infinite Timeout*/ Wu}Else { -PTO = &timeout; AboutTimeout.tv_sec = Getlong (argv[1],0,"Timeout"); $Timeout.tv_usec =0;/*No microseconds*/ - } - - /*Process remaining arguments to build file descriptor sets*/ A +Nfds =0; theFd_zero (&Readfds); -Fd_zero (&Writefds); $ the for(j =2; J < argc; J + +) { theNumread = sscanf (Argv[j],"%D%2[RW]", &FD, BUF);//Enter the file descriptor into FD and write the input/output parameters to buf[] the if(Numread! =2) theUsageerror (argv[0]); - if(FD >=fd_setsize) inCmdlineerr ("file descriptor exceeds limit (%d) \ n", fd_setsize); the the if(FD >=Nfds) AboutNfds = FD +1;/*Record Maximum FD + 1*/ the if(STRCHR (BUF,'R') !=NULL) theFd_set (FD, &Readfds); the if(STRCHR (BUF,'W') !=NULL) +Fd_set (FD, &Writefds); - } the Bayi /*We ' ve built all of the arguments; now call Select ()*/ the theReady =Select(Nfds, &readfds, &Writefds, NULL, PTO); - /*Ignore Exceptional Events*/ - if(Ready = =-1) theErrexit ("Select"); the the /*Display Results of select ()*/ the -printf"Ready =%d\n", ready); the for(FD =0; FD < Nfds; fd++) theprintf"%d:%s%s\n", FD, Fd_isset (FD, &readfds)?"R":"", theFd_isset (FD, &writefds)?"W":"");94 the if(PTO! =NULL) theprintf"timeout after select ():%ld.%0 3ld\n", the(Long) Timeout.tv_sec, (Long) Timeout.tv_usec/ +);98 exit (exit_success); About}View Code
Attachment is the book's Code bundle: Link: http://pan.baidu.com/s/1qWYJ54w Password: WMQB
The Select function is simple to use the legal process