select

SELECT(2)                  Linux Programmer's Manual                 SELECT(2)



åå
       select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - åæ I/O ã®å¤éå

æ¸å¼
       /* POSIX.1-2001 ã«å¾ãå ´å */
       #include <sys/select.h>

       /* 以åã®è¦æ ¼ã«å¾ãå ´å */
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int select(int nfds, fd_set *readfds, fd_set *writefds,
                  fd_set *exceptfds, struct timeval *timeout);

       void FD_CLR(int fd, fd_set *set);
       int  FD_ISSET(int fd, fd_set *set);
       void FD_SET(int fd, fd_set *set);
       void FD_ZERO(fd_set *set);

       #include <sys/select.h>

       int pselect(int nfds, fd_set *readfds, fd_set *writefds,
                   fd_set *exceptfds, const struct timespec *timeout,
                   const sigset_t *sigmask);

   glibc åãã®æ©è½æ¤æ»ãã¯ãã®è¦ä»¶ (feature_test_macros(7)  åç§):

       pselect(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600

説æ
       select()  ã pselect()  ã使ãã¨ããã‐
       ã°ã©ã ã§è¤æ°ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãç£è¦ãã
       ä¸ã¤ä»¥ä¸ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ããã種㮠I/O æä½ã® ãready (æºåãã§ãã)ãç¶æ
       (ä¾ãã°ãèªã¿è¾¼ã¿å¯è½ã«ãªã£ãç¶æ) ã«ãªãã¾ã§å¾ã¤ãã¨ãã§ããã
       ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã ready (æºåãã§ãã) ã¨ã¯ã 対å¿ãã I/O æä½ (ä¾ãã°
       read(2) ãªã©) ãåæ¢ (block) ãªãã«å®è¡å¯è½ãªç¶æã«ãããã¨ãæå³ããã

       select()  㨠pselect()  ã®åä½ã¯åãã§ãããã以ä¸ã® 3 ç¹ãç°ãªã:

       (i)    select()  ã§ã¯ãã¿ã¤ã ã¢ã¦ãæéã®æå®ã«æ§é ä½ struct timeval
              (ç§ã»ãã¤ã¯ãç§åä½) ãç¨ããã ä¸æ¹ã pselect()  é¢æ°ã§ã¯ãæ§é ä½ struct
              timespec (ç§ã»ããç§åä½) ãç¨ããã

       (ii)   select()  ã¯æ®ãæéã示ã timeout å¼ãæ°ãæ´æ°ãããã¨ãããã pselect()
              ã¯ãã®å¼ãæ°ãå¤æ´ããªãã

       (iii)  select()  㯠sigmask å¼ãæ°ãæããªãããã®åä½ã¯ sigmask ã« NULL
              ãæå®ããå ´åã® pselect()  ã¨åãã§ããã

       3 ã¤ã®ç¬ç«ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿éåã®ç£è¦ãè¡ãã readfds
       ã«å¥ãããããã£ã¹ã¯ãªãã¿ã«ã¤ãã¦ã¯ãèªã¿è¾¼ã¿ãå¯è½ãã©ããã ç£è¦ãã (ããæ‐
       £ç¢ºã«ããã¨ãåæ¢ (block) ãªãã§èªããã¨ãã§ãããã 調ã¹ãããã¡ã¤ã«ã®çµç«¯
       (end-of-file) ã®å ´åãã ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¯èªã¿è¾¼ã¿å¯è½ã¨ãã¦æ±ããã)ã
       writefds ã«å¥ãããããã£ã¹ã¯ãªãã¿ã«ã¤ãã¦ã¯ãåæ¢ããã«æ¸ãè¾¼ã¿ã
       å¯è½ãã©ãããç£è¦ããã exceptfds
       ã«ãããã®ã«ã¤ãã¦ã¯ãä¾å¤ã®ç£è¦ãè¡ãªããã·ã¹ãã ã³ã¼ã«çµäºæã«ã
       ã©ã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®ç¶æãå®éã«å¤åããã示ãããã«ã éåã®å容ãå¤æ´ãããã
       ãã種å¥ã®ã¤ãã³ããç£è¦ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãä¸ã¤ããªãå ´åã«ã¯ã
       対å¿ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿éåã« NULL ãæå®ãããã¨ãã§ããã

       éåãæä½ããããã« 4 ã¤ã®ãã¯ããæä¾ããã¦ããã FD_ZERO()  ã¯éåãæ¶å»ããã FD_SET()  ã¨
       FD_CLR()  ã¯ããããæå®ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®éåã¸ã®è¿½å ãåé¤ãè¡ãã
       FD_ISSET() ã¯éåã«ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ããããã©ãã調ã¹ã; ãã®ãã¯ã㯠select()
       ãçµäºããå¾ã«ä½¿ãã¨ä¾¿å©ã§ããã

       nfds 㯠3 ã¤ã®éåã«å«ã¾ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®æ大å¤ã« 1 ã足ãããã®ã§ããã

       timeout å¼ãæ°ã§ããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã ready ã«ãªãã®ãå¾ã£ã¦ select() ãåæ‐
       ¢ããæå°ã®åæ¢æéãæå®ãã (ãã®åæ¢æéã¯ã·ã¹ãã ã¯ããã¯ã®ç²åº¦ã«åãä¸ãããã
       ã«ã¼ãã«ã®ã¹ã±ã¸ã¥ã¼ãªã³ã°é延ã«ããå°ãã ãé·ããªãå¯è½æ§ããã)ã timeval
       æ§é ä½ã®ä¸¡æ¹ã®ãã£ã¼ã«ãã 0 ã®å ´åã select() ã¯ããã«å¾© 帰ãã
       (ãã®æ©è½ã¯ãã¼ãªã³ã° (polling) ãè¡ãã®ã«ä¾¿å©ã§ãã)ã timeout ã« NULL
       (ã¿ã¤ã ã¢ã¦ããªã) ãæå®ãããã¨ã select() ã¯ç¡ æéã«åæ¢ (block) ããã

       sigmask ã¯ãã·ã°ãã«ãã¹ã¯ (sigprocmask(2)  ãåç§) ã¸ã®ãã¤ã³ã¿ã§ããã sigmask
       ã NULL ã§ãªãå ´åã pselect()  㯠sigmask
       ãæãã¦ããã·ã°ãã«ãã¹ã¯ã§ç¾å¨ã®ã·ã°ãã«ãã¹ã¯ãç½®ãæãã¦ããã "select"
       é¢æ°ãå®è¡ãã çµäºå¾ã«ã·ã°ãã«ãã¹ã¯ãåã®ã·ã°ãã«ãã¹ã¯ã«æ»ãã

       timeout å¼ãæ°ã®ç²¾åº¦ã®éããé¤ãã¨ã以ä¸ã® pselect()  ã®å¼ã³åºãã¯ã

           ready = pselect(nfds, &readfds, &writefds, &exceptfds,
                           timeout, &sigmask);

       次ã®ã³ã¼ã«ã atomic ã«å®è¡ããã®ã¨ç価ã§ããã

           sigset_t origmask;

           pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
           ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
           pthread_sigmask(SIG_SETMASK, &origmask, NULL);

       pselect()  ãå¿è¦ã«ãªãçç±ã¯ãã·ã°ãã«ããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®ç¶æå¤åã
       å¾ã¡ããã¨ãã«ã¯ã競åç¶æãé¿ããããã« atomic ãªãã¹ããå¿è¦ã«ãªã ããã§ããã
       (ã·ã°ãã«ãã³ãã©ã大åãã©ã°ãè¨å®ãã¦æ»ãå ´åãèãã¦ã¿ããã
       ãã®å¤§åãã©ã°ã®ãã¹ãã«ç¶ã㦠select()  ãå¼ã³åºãã¨ã
       ã·ã°ãã«ããã¹ãã®ç´å¾ãã¤å¼ã³åºãã®ç´åã«å±ããæã«ã¯ select()
       ã¯æ°¸ä¹ã«ãã³ã°ãã¦ãã¾ããããããªãã ä¸æ¹ã pselect()  ã使ãã¨ãã¾ãã·ã°ãã«ãç¦æ¢
       (block) ãã¦ãå¥ã£ã¦ããã·ã°ãã«ãæä½ãã æã¿ã® sigmask 㧠pselect()
       ãå¼ã³åºããã¨ã§ãåè¨ã®ç«¶åãé¿ãããã¨ãã§ããã)

   ã¿ã¤ã ã¢ã¦ã
       ãããã®é¢æ°ã§ä½¿ç¨ãããæéé¢é£ã®æ§é ä½ã¯ã <sys/time.h> ã§

           struct timeval {
               long    tv_sec;         /* ç§ */
               long    tv_usec;        /* ãã¤ã¯ãç§ */
           };

       ã

           struct timespec {
               long    tv_sec;         /* ç§ */
               long    tv_nsec;        /* ããç§ */
           };

       ã®ããã«å®ç¾©ããã¦ããã (POSIX.1-2001 ã§ã®å®ç¾©ã«ã¤ãã¦ã¯ä¸è¨ã®ã注æããåç§)

       ç§åä½ä»¥ä¸ã®ç²¾åº¦ã§ã¹ãªã¼ããå®ç¾ãã 移æ¤æ§ã®é«ãæ¹æ³ã¨ãã¦ã 3
       ã¤ã®éåå¨ã¦ã空ã nfds ã 0 ã timeout ã NULL ã§ãªãå¤ã«è¨å®ã㦠select()
       ãå¼ã³åºãã¨ããæ¹æ³ã使ã£ã¦ããã³ã¼ããããã

       Linux ã§ã¯ã select()  㯠timeout ãå¤æ´ããæ®ãã®åæ¢æéãåæ ããããã«ãªã£ã¦ãããã
       ä»ã®ã»ã¨ãã©ã®å®è£ã§ã¯ãã®ããã«ãªã£ã¦ããªã (POSIX.1-2001
       ã¯ã©ã¡ãã®åä½ãèªãã¦ãã)ã ãã®ããã timeout ãåç§ãã¦ãã Linux
       ã®ã³ã¼ããä»ã®ãªãã¬ã¼ãã£ã³ã°ã»ã·ã¹ãã 㸠移æ¤ããå ´åãåé¡ãèµ·ããã
       ã¾ããã«ã¼ãã®ä¸ã§ timeval æ§é ä½ãåæåããã«ãã®ã¾ã¾åå©ç¨ã㦠select()
       ãè¤æ°åè¡ãªã£ã¦ããã³ã¼ãã Linux ã¸ç§»æ¤ããå ´åã«ããåé¡ãèµ·ããã select()
       ãã復帰ããå¾ã¯ timeout ã¯æªå®ç¾©ã§ããã¨èããã¹ãã§ããã

è¿ãå¤
       æåããå ´åã select()  㨠pselect()  ã¯æ´æ°ããã 3
       ã¤ã®ãã£ã¹ã¯ãªãã¿éåã«å«ã¾ãã¦ãã ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã®æ° (ã¤ã¾ãã readfds,
       writefds, exceptfds ä¸ã® 1 ã«ãªã£ã¦ãããããã®ç·æ°) ãè¿ãã
       ä½ãèµ·ãããã«æéåãã«ãªã£ãå ´åã ãã£ã¹ã¯ãªãã¿ã®æ°ã¯ 0 ã«ãªããã¨ãããã
       ã¨ã©ã¼ãªãã° -1 ãè¿ãã errno ã«é©åãªå¤ãè¨å®ããã; éå㨠timeout
       ã¯æªå®ç¾©ã¨ãªãã®ã§ãã¨ã©ã¼ãèµ·ãã£ãå¾ã¯ãããã®å容ãä¿¡é ¼ãã¦ã¯ãªããªãã

ã¨ã©ã¼
       EBADF  ããããã®éåã«ç¡å¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãæå®ããã (ããããã¯ããã§ã«ã¯ã‐
              ã¼ãºããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãã
              ã¨ã©ã¼ãçºçãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãæå®ããã)ã

       EINTR  ã·ã°ãã«ãåä¿¡ããã

       EINVAL n ãè² ãã¾ã㯠timeout ã«å¥ã£ã¦ããå¤ãä¸æ£ã§ããã

       ENOMEM åé¨ãã¼ãã«ã«ã¡ã¢ãªãå²ãå½ã¦ããã¨ãã§ããªãã£ãã

ãã¼ã¸ã§ã³
       pselect()  ã¯ã«ã¼ãã« 2.6.16 㧠Linux ã«è¿½å ãããã ãã以åã¯ã pselect()
       㯠glibc ã§ã¨ãã¥ã¬ã¼ãããã¦ãã (ããã°ãã®ç« ãåç§)ã

æºæ
       select()  㯠POSIX.1-2001 㨠4.4BSD (select()  㯠4.2BSD ã§æåã«ç»å ´ãã)
       ã«æºæ ããã BSD ã½ã±ãã層ã®ã¯ãã¼ã³ããµãã¼ããã¦ããé BSD ã·ã¹ãã  (System V
       ç³»ãå«ã) ã¨ã®éã§ã ããã移æ¤æ§ããããããã System V ç³»ã§ã¯ ãããã timeout å¤æ°ã
       exit ã®åã«ã»ãããããã BSD ç³»ã§ã¯ããã§ãªãã®ã§æ³¨æãããã¨ã

       pselect()  㯠POSIX.1g 㨠POSIX.1-2001 ã§å®ç¾©ããã¦ããã

注æ
       fd_set ã¯åºå®ãµã¤ãºã®ãããã¡ã§ããã è² ã FD_SETSIZE 以ä¸ã®å¤ãæ㤠fd
       ã«å¯¾ã㦠FD_CLR()  ã FD_SET()  ãå®è¡ããå ´åã
       ã©ã®ãããªåä½ããããã¯å®ç¾©ããã¦ããªãã ã¾ãã POSIX ã§ã¯ fd
       ã¯æå¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã§ãªããã°ãªããªãã¨è¦å®ããã¦ããã

       å宣è¨ã«é¢ãã¦ã¯ãæãªããã®ç¶æ³ã§ã¯ timeval æ§é ä½ã® 2 ã¤ã®ãã£ã¼ã«ãã¯
       (ä¸è¨ã®ããã«) 両æ¹ã¨ã long åã§ãããæ§é ä½ã¯ <sys/time.h> ã§å®ç¾©ããã¦ããã
       POSIX.1-2001 ã®ä¸ã§ã¯ã以ä¸ã®ããã«ãªã£ã¦ããã

           struct timeval {
                 time_t         tv_sec;     /* ç§ */
                 suseconds_t    tv_usec;    /* ãã¤ã¯ãç§ */
           };

       ãã®æ§é ä½ã¯ <sys/select.h> ã§å®ç¾©ããã¦ããããã¼ã¿å time_t 㨠suseconds_t
       㯠<sys/types.h> ã§å®ç¾©ããã¦ããã

       ãããã¿ã¤ãã«é¢ãã¦ã¯ãæãªããã®ç¶æ³ã§ select()  ã使ãããå ´åã¯ã <time.h>
       ãã¤ã³ã¯ã«ã¼ãããã°ããã POSIX.1-2001 ã®ç°å¢ã§ select()  㨠pselect()
       ã使ãããå ´åã¯ã <sys/select.h> ãã¤ã³ã¯ã«ã¼ãããã°ããã

       ããããã¡ã¤ã« <sys/select.h> 㯠libc4 㨠libc5 ã«ã¯ãªããglibc 2.0 以éã«å‐
       å¨ããã æªããã¨ã« glibc 2.0 以åã§ã¯ pselect()  ã®ãããã¿ã¤ããééã£ã¦ããã glibc
       2.1 ãã 2.2.1 ã§ã¯ _GNU_SOURCE ãå®ç¾©ããã¦ããå ´åã«ã pselect()  ãæä¾ãããã
       glibc 2.2.2 以éã§ã¯ã pselect()
       ã使ç¨ããã«ã¯ããæ¸å¼ãã«è¨è¼ãããè¦ä»¶ãæºããå¿è¦ãããã

   Multithreaded applications
       If a file descriptor being monitored by select()  is closed in another
       thread, the result is unspecified.  On some UNIX systems, select()
       unblocks and returns, with an indication that the file descriptor is
       ready (a subsequent I/O operation will likely fail with an error,
       unless another the file descriptor reopened between the time select()
       returned and the I/O operations was performed).  On Linux (and some
       other systems), closing the file descriptor in another thread has no
       effect on select().  In summary, any application that relies on a
       particular behavior in this scenario must be considered buggy.

   Linux ã§ã®æ³¨æ
       ãã®ãã¼ã¸ã§èª¬æãã¦ãã pselect() ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯ãglibc ã«
       å®è£ããã¦ãããã®ã§ãããåé¨ã§å¼ã³åºããã Linux ã®ã·ã¹ãã ã³ã¼ã«ã¯ pselect6()
       ã¨ããååã§ããããã®ã·ã¹ãã ã³ã¼ã«ã¯ glibc ã®ã©ããã¼ é¢æ°ã¨ã¯å°ãéã£ãåä½ãããã

       Linux ã® pselect6() ã·ã¹ãã ã³ã¼ã«ã¯ timeout å¼ãæ°ãå¤æ´ããã ããããglibc
       ã®ã©ããã¼é¢æ°ã¯ãã·ã¹ãã ã³ã¼ã«ã«æ¸¡ã timeout å¼ãæ°ã¨ ãã¦ã‐
       ã¼ã«ã«å¤æ°ã使ããã¨ã§ãã®åä½ãé è½ãã¦ããããã®ãããglibc ã® pselect() é¢æ°ã¯
       timeout å¼ãæ°ãå¤æ´ããªãã ããã POSIX.1-2001 ãè¦æ±ãã¦ããåä½ã§ããã

       pselect6() ã·ã¹ãã ã³ã¼ã«ã®æå¾ã®å¼ãæ°ã¯ sigset_t * åã®
       ãã¤ã³ã¿ã§ã¯ãªãã以ä¸ã«ç¤ºãæ§é ä½ã§ããã

           struct {
               const sigset_t *ss;     /* ã·ã°ãã«éåã¸ã®ãã¤ã³ã¿ */
               size_t          ss_len; /* 'ss' ãæããªãã¸ã§ã¯ãã®ãµã¤ãº
                                          (ãã¤ãæ°) */
           };

       ãã®ããã«ãããã¨ã§ãã»ã¨ãã©ã®ã¢ã¼ããã¯ãã£ããµãã¼ããã¦ãã
       ã·ã¹ãã ã³ã¼ã«ã®å¼ãæ°ãæ大㧠6 åã¨ããäºå®ãæºããã¤ã¤ã pselect6()
       ã·ã¹ãã ã³ã¼ã«ãã·ã°ãã«éåã¸ã®ãã¤ã³ã¿ã¨ã·ã°ãã«éå
       ã®ãµã¤ãºã®ä¸¡æ¹ãåå¾ãããã¨ãã§ããã®ã§ããã

ãã°
       glibc 2.0 ã§ã¯ã sigmask å¼ãæ°ãåããªããã¼ã¸ã§ã³ã® pselect()  ãæä¾ããã¦ããã

       ãã¼ã¸ã§ã³ 2.1 以éã® glibc ã§ã¯ã pselect()  㯠sigprocmask(2)  ã¨
       select() ã使ã£ã¦ã¨ãã¥ã¬ã¼ãããã¦ããã
       ãã®å®è£ã«ã¯ããã©ã競åæ¡ä»¶ã«ããã¦èå¼±æ§ãæ®ã£ã¦ããã
       ãã®ç«¶åæ¡ä»¶ã«ãããåé¡ãé²æ¢ããããã« pselect()  ã¯è¨è¨ãããã®ã§ããã
       æè¿ã®ãã¼ã¸ã§ã³ã® glibc ã§ã¯ãã«ã¼ãã«ããµãã¼ããã¦ããå ´åã«ã¯ã
       (競åãèµ·ãããªã) pselect()  ã·ã¹ãã ã³ã¼ã«ã使ç¨ãããã

       pselect() ããªãã·ã¹ãã ã«ããã¦ãã·ã°ãã«ã®ææãä¿¡é ¼æ§ããã (ç§»æ¤ æ§ãé«ã)
       æ¹æ³ã§è¡ãã«ã¯ã èªå·±ãã¤ã (self-pipe) ã¨ããæã使ãã¨ããã
       ãã®æ¹æ³ã§ã¯ãã·ã°ãã«ãã³ãã©ã¯ãã¤ã㸠1 ãã¤ãã®ãã¼ã¿ãæ¸ãè¾¼ã¿ã
       åããã¤ãã®ããä¸ç«¯ãã¡ã¤ã³ããã°ã©ã ã® select() ã§ç£è¦ãã (ä¸æ¯ã«
       ãªã£ããã¤ãã¸ã®æ¸ãè¾¼ã¿ã空ã®ãã¤ãããèªã¿åºããè¡ã£ãéã«èµ·ããã§ã ããåæ¢
       (blocking) ãé¿ããããã«ã¯ããã¤ãã¸ã®èªã¿æ¸ãã®éã«ã¯ éåæ¢ (nonblocking) I/O
       ã使ç¨ããã¨ãã)ã

       Linux ã§ã¯ã select()  ãã½ã±ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã§ "èª‐
       ã¿è¾¼ã¿ã®æºåãã§ãã" ã¨å ±åããå ´åã§ãã ãã®å¾ã§ read ãè¡ãã¨åæ¢ (block)
       ãããã¨ãããããã®ãããªç¶æ³ã¯ã
       ä¾ãã°ããã¼ã¿ãå°çããããæ¤æ»ã§ãã§ãã¯ãµã ç°å¸¸ãè¦ã¤ããå»æ£ãããæ
       ãªã©ã«èµ·ãããããä»ã«ããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãæºåã§ããã¨ééã£ã¦
       å ±åãããç¶æ³ãèµ·ãããããããªãã ãããã£ã¦ãåæ¢ãã¹ãã§ã¯ãªãã½ã±ããã«å¯¾ãã¦ã¯
       O_NONBLOCK ã使ãã¨ããå®å¨ã§ãããã

       Linux ã§ã¯ã select()  ãã·ã°ãã«ãã³ãã©ã«ããå²ãè¾¼ã¾ããå ´å (ã¤ã¾ã EINTR
       ã¨ã©ã¼ãè¿ãå ´å)ã timeout ãå¤æ´ããã ãã㯠POSIX.1-2001
       ã§ã¯èªãããã¦ããªãæåã§ããã Linux ã® pselect() ã·ã¹ãã ã³ã¼ã«ãåãæåããããã glibc
       ã®ã©ããã¼é¢æ°ããã®æåãé è½ãã¦ããã å·ä½çã«ã¯ãglibc ã®ã©ããã¼é¢æ°ã®åé¨ã§ã
       timeout ããã¼ã«ã«å¤æ°ã«ã³ãã¼ãã ãã®ãã¼ã«ã«å¤æ°ãã·ã¹ãã ã³ã¼ã«ã«æ¸¡ãã¦ããã

ä¾
       #include <stdio.h>
       #include <stdlib.h>
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       int
       main(void)
       {
           fd_set rfds;
           struct timeval tv;
           int retval;

           /* stdin (fd 0) ãç£è¦ããå¥åããã£ãå ´åã«è¡¨ç¤ºããã*/
           FD_ZERO(&rfds);
           FD_SET(0, &rfds);

           /* 5 ç§éç£è¦ããã*/
           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(1, &rfds, NULL, NULL, &tv);
           /* ãã®æç¹ã§ã® tv ã®å¤ãä¿¡é ¼ãã¦ã¯ãªããªãã*/

           if (retval == -1)
            perror("select()");
           else if (retval)
               printf("ä»ããã¼ã¿ãåå¾ã§ãã¾ããã\n");
               /* FD_ISSET(0, &rfds) ã true ã«ãªãã*/
           else
               printf("5 ç§ä»¥åã«ãã¼ã¿ãå¥åããã¾ããã§ããã\n");

           exit(EXIT_SUCCESS);
       }

é¢é£é ç®
       accept(2), connect(2), poll(2), read(2), recv(2), send(2),
       sigprocmask(2), write(2), epoll(7), time(7)

       èå¯ã¨ä½¿ç¨ä¾ã®æ¸ããããã¥ã¼ããªã¢ã«ã¨ãã¦ã select_tut(2)  ãããã

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2012-08-17                         SELECT(2)