select

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



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

æ¸å¼
       #include <sys/time.h>
       #include <sys/types.h>
       #include <unistd.h>

       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);

説æ
       select ã¯ããã¤ãã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼(file descriptor)ã®ç¶æã
       å¤åããã®ãå¾ã¤ã

       ä¸ã¤ã®ç¬ç«ãããã£ã¹ã¯ãªãã¿ã¼ã®éåãç£è¦ããã readfds ã«ãªã¹ãããããã®ã¯èª‐
       ã¿è¾¼ã¿ãå¯è½ãã©ãããç£è¦ããã writefds
       ã«ãªã¹ãããããã®ã¯ããã«ããã«æ¸ãè¾¼ã¿ãå¯è½ãã©ãããç£è¦ããã exceptfds
       ã«ãããã®ã¯ä¾å¤ã®ç£è¦ãè¡ãªããçµäºããæã«ãå®éã«ã©ã®
       ãã£ã¹ã¯ãªãã¿ã¼ã®ç¶æãå¤åããã示ãããã«éåãå¤æ´ãããã

       éåãæä½ããããã«åã¤ã®ãã¯ã(macro)ãæä¾ãããã FD_ZERO ã¯éåãæ¶å»(clear)ããã FD_SET
       㨠FD_CLR ã¯éåã«ãã£ã¹ã¯ãªãã¿ã¼ãå ãããåãé¤ãããããã FD_ISSET
       ã¯éåã«ãã£ã¹ã¯ãªãã¿ã¼ããããã©ãã調ã¹ã; ãã㯠select ãçµäºããå¾ã§ä¾¿å©ã§ããã

       n ã¯ä¸ã¤ã®éåã«ãããã£ã¹ã¯ãªãã¿ã¼ã®ä¸ã®æ大å¤ã« 1 ã足ãããã®ã

       timeout 㯠select ãè¿ã£ã¦ããã¾ã§ã®çµéæéã®ä¸éãããã¯ã¼ãã«ãããã¨ãã§ãã
       ãã®å ´å㯠select ã¯ããã«è¿ã£ã¦ããããã timeout ã« NULL (æéåã(timeout)ç¡ã)
       ãæå®ãããã¨ã select ã¯ç¡æéã«åæ¢(block)ããã

è¿ãå¤
       æåããå ´åã select ã¯ãã£ã¹ã¯ãªãã¿ã¼éåã«å«ã¾ãã¦ãããã£ã¹ã¯ãªã¿ã¼ã®æ°ãè¿ãã
       ãããã®èå³æ·±ãäºè±¡ã®åã«æéåãã«ãªã£ãå ´åã¯ã¼ãã«ãªããã¨ãããã ã¨ã©ã¼ãªãã° -1
       ãè¿ãã errno ã«é©åãªå¤ãè¨å®ããã; éå㨠timeout
       ã¯æªå®ç¾©ã¨ãªããã¨ã©ã¼ã®å¾ã¯ãããã®å容ã¯ä¿¡é ¼ã§ããªãã

ã¨ã©ã¼
       EBADF   éåã®ã©ããã«ç¡å¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¼ãä¸ããããã

       EINTR   ç¦æ¢(block)ããã¦ããªãã·ã°ãã«(signal)ãåããã

       EINVAL  n ãè² ã§ããã

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

注æ
       select ãå¼ã³åºãã³ã¼ãã«ã¯ä¸ã¤ã®éåå¨ã¦ã«ç©ºãè¨å®ãã n ã«ã¼ãã timeout ã«
       NULL ã§ãªãå¤ãè¨å®ãã移æ¤æ§(portable)ã®ããæ¹æ³ã§ç§åä½ä»¥ä¸ã®
       ã¹ãªã¼ã(sleep)ãå®ç¾ãã¦ãããã®ãããã

       Linux ã§ã¯ã timeout ã¯åæ¢(sleep)ãã¦ããªãæéã®éãåæ ããããã«ä¿®æ£ãããã
       ä»ã®ã»ã¨ãã©ã®ã¤ã³ããªã¡ã³ãã¼ã·ã§ã³ã§ã¯ãã®ããã«ãªã£ã¦ããªãã ãã㯠timeout
       ãèªã¿åã Linux ã®ã³ã¼ããä»ã®ãªãã¬ã¼ãã£ã³ã°ã»ã·ã¹ãã ã¸ç§»æ¤ãã å ´åã¨
       ã«ã¼ãã®ä¸ã§ timval æ§é ä½ãåæåããã«å使ç¨ãã¦è¤æ°åã® select
       ãè¡ãªã£ã¦ããã³ã¼ãã Linux ã¸ç§»æ¤ããå ´åã®ä¸¡æ¹ã§åé¡ãèµ·ããã select
       ãè¿ã£ã¦ããå¾ã¯ timeout ã¯æªå®ç¾©ã§ããã¨èããã¹ãã§ããã

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

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

           /* Watch stdin (fd 0) to see when it has input. */
           FD_ZERO(&rfds);
           FD_SET(0, &rfds);
           /* Wait up to five seconds. */
           tv.tv_sec = 5;
           tv.tv_usec = 0;

           retval = select(1, &rfds, NULL, NULL, &tv);
           /* Don't rely on the value of tv now! */

           if (retval)
               printf("Data is available now.\n");
               /* FD_ISSET(0, &rfds) will be true. */
           else
               printf("No data within five seconds.\n");

           exit(0);
       }


æºæ
       4.4BSD ( select é¢æ°ã¯ 4.2BSD ã§æåã«ç¾ããã)ã ä¸è¬ã« BSD
       ã®ã½ã±ãã層ããµãã¼ããã BSD 以å¤ã®ã·ã¹ãã (System V
       ã®å¤ç¨®ãå«ã)ã¸ã®ç§»æ¤ãã¾ãã¯ããããã®ç§»æ¤ ãã§ãããä¸æ¹ã§ System V
       ã®å¤ç¨®ã¯å¸åçã«æ»ãåã« timeout å¤æ°ãè¨å®ã ãã®ã«å¯¾ãã¦ãBSDã®å¤ç¨®ã§ã¯è¨‐
       å®ããªããã¨ã«æ³¨æãããã¨ã

é¢é£é ç®
       accept(2), connect(2), read(2), recv(2), send(2), write(2).



Linux 1.2                      February 11, 1996                     SELECT(2)