Linux系统调用(219)\_newselect()函数UnixLinux

JavaScript ww9992019com 发表于 5 天前

<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="1641">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - 同步I / O复用</p>

<h1 class="manpages">

内容简介</h1>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

/ According to POSIX.1-2001 /

<b>#include &lt;sys/select.h&gt;</b>

/ According to earlier standards /

<b>#include &lt;sys/time.h&gt;</b>

<b>#include &lt;sys/types.h&gt;</b>

<b>#include &lt;unistd.h&gt;</b>

</pre>

            <p>

                <b>int select(int </b><i>nfds</i><b>, fd_set *</b><i>readfds</i><b>, fd_set *</b><i>writefds</i><b>, fd_set *</b><i>exceptfds</i><b>, struct timeval *</b><i>timeout</i>); <b>void FD_CLR(int </b><i>fd</i><b>, fd_set *</b><i>set</i><b>);</b> <b>int FD_ISSET(int </b><i>fd</i><b>, fd_set *</b><i>set</i><b>);</b> <b>void FD_SET(int </b><i>fd</i><b>, fd_set *</b><i>set</i><b>);</b> <b>void FD_ZERO(fd_set *</b><i>set</i><b>);</b></p>

            <b>#define _XOPEN_SOURCE 600</b> <b>#include &lt;sys/select.h&gt;</b>

            <p>

                <b>int pselect(int </b><i>nfds</i><b>, fd_set *</b><i>readfds</i><b>, fd_set *</b><i>writefds</i><b>, fd_set *</b><i>exceptfds</i><b>, const struct timespec *</b><i>timeout</i><b>, const sigset_t *</b><i>sigmask</i><b>); </b></p>

        </td>

    </tr>

</tbody>

</table>

<h1 class="manpages">

描述</h1>

<p>

<b>select</b>() and <b>pselect</b>() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., <b>read</b>(2)) without blocking.</p>

<p>

The operation of <b>select</b>() and <b>pselect</b>() is identical, with three differences:</p>

<table border="1" cellpadding="5" cellspacing="0" class="src">

<tbody>

    <tr>

        <th width="25%">

            S.N.</th>

        <th>

            描述</th>

    </tr>

    <tr valign="top">

        <td>

            (i)</td>

        <td valign="bottom">

            <b>select</b>() uses a timeout that is a <i>struct timeval</i> (with seconds and microseconds), while <b>pselect</b>() uses a <i>struct timespec</i> (with seconds and nanoseconds).</td>

    </tr>

    <tr valign="top">

        <td>

            (ii)</td>

        <td valign="bottom">

            <b>select</b>() may update the <i>timeout</i> argument to indicate how much time was left. <b>pselect</b>() does not change this argument.</td>

    </tr>

    <tr valign="top">

        <td>

            (iii)</td>

        <td valign="bottom">

            <b>select</b>() has no <i>sigmask</i> argument, and behaves as <b>pselect</b>() called with NULL <i>sigmask</i>.</td>

    </tr>

</tbody>

</table>

<p>

Three independent sets of file descriptors are watched. Those listed in <i>readfds</i> will be watched to see if characters become available for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file),those in<i>writefds</i> will be watched to see if a write will not block, and those in <i>exceptfds</i> will be watched for exceptions. On exit, the sets are modified in place to indicate which file descriptors actually changed status. Each of the three file descriptor sets may be specified as NULL if no file descriptors are to be watched for the corresponding class of events.</p>

<p>

Four macros are provided to manipulate the sets.</p>

<ul>

<li>

    <p>

        <b>FD_ZERO</b>() clears a set.</p>

</li>

<li>

    <p>

        <b>FD_SET</b>() and</p>

</li>

<li>

    <p>

        <b>FD_CLR</b>() respectively add and remove a given file descriptor from a set.</p>

</li>

<li>

    <p>

        <b>FD_ISSET</b>() tests to see if a file descriptor is part of the set;</p>

</li>

</ul>

<p>

this is useful after <b>select</b>() returns.</p>

<p>

<i>nfds</i> is the highest-numbered file descriptor in any of the three sets, plus 1.</p>

<p>

<i>timeout</i> is an upper bound on the amount of time elapsed before <b>select</b>() returns. It may be zero, causing <b>select</b>() to return immediately. (This is useful for polling.) If<i>timeout</i> is NULL (no timeout), <b>select</b>() can block indefinitely.</p>

<p>

<i>sigmask</i> is a pointer to a signal mask (see <b>sigprocmask</b>(2)); if it is not NULL, then<b>pselect</b>() first replaces the current signal mask by the one pointed to by <i>sigmask</i>, then does the ‘select’ function, and then restores the original signal mask.</p>

<p>

Other than the difference in the precision of the <i>timeout</i> argument, the following<b>pselect</b>() call:</p>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

ready = pselect(nfds, &amp;readfds, &amp;writefds, &amp;exceptfds,

                timeout, &amp;sigmask);

</pre>

        </td>

    </tr>

</tbody>

</table>

<p>

is equivalent to <i>atomically</i> executing the following calls:</p>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

sigset_t origmask;

sigprocmask(SIG_SETMASK, &amp;sigmask, &amp;origmask);

ready = select(nfds, &amp;readfds, &amp;writefds, &amp;exceptfds, timeout);

sigprocmask(SIG_SETMASK, &amp;origmask, NULL);

</pre>

        </td>

    </tr>

</tbody>

</table>

<p>

The reason that <b>pselect</b>() is needed is that if one wants to wait for either a signal or for a file descriptor to become ready, then an atomic test is needed to prevent race conditions. (Suppose the signal handler sets a global flag and returns. Then a test of this global flag followed by a call of <b>select</b>() could hang indefinitely if the signal arrived just after the test but just before the call. By contrast, <b>pselect</b>() allows one to first block signals, handle the signals that have come in, then call <b>pselect</b>() with the desired<i>sigmask</i>, avoiding the race.)</p>

<h2 liberation sans freesans sans-serif margin-bottom: margin-top: color: rgb>

<i>The timeout</i></h2>

<p>

The time structures involved are defined in <i>&lt;sys/time.h&gt;</i> and look like</p>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

struct timeval {

long    tv_sec;         /* seconds */

long    tv_usec;        /* microseconds */

};

</pre>

        </td>

    </tr>

</tbody>

</table>

<p>

and</p>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

struct timespec {

long    tv_sec;         /* seconds */

long    tv_nsec;        /* nanoseconds */

};

</pre>

        </td>

    </tr>

</tbody>

</table>

<p>

(However, see below on the POSIX.1-2001 versions.). Some code calls <b>select</b>() with all three sets empty, <i>n</i> zero, and a non-NULL <i>timeout</i> as a fairly portable way to sleep with subsecond precision.</p>

<p>

On Linux, <b>select</b>() modifies <i>timeout</i> to reflect the amount of time not slept; most other implementations do not do this. (POSIX.1-2001 permits either behaviour.) This causes problems both when Linux code which reads <i>timeout</i> is ported to other operating systems, and when code is ported to Linux that reuses a struct timeval for multiple<b>select</b>()s in a loop without reinitializing it. Consider <i>timeout</i> to be undefined after<b>select</b>() returns.</p>

<h1 class="manpages">

返回值</h1>

<p>

On success, <b>select</b>() and <b>pselect</b>() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in <i>readfds</i>,<i>writefds</i><i>exceptfds</i>) which may be zero if the timeout expires before anything interesting happens. On error, -1 is returned, and <i>errno</i> is set appropriately; the sets and <i>timeout</i>become undefined, so do not rely on their contents after an error.</p>

<h1 class="manpages">

错误</h1>

<table border="1" cellpadding="5" cellspacing="0" class="src">

<tbody>

    <tr>

        <th width="25%">

            标签</th>

        <th>

            描述</th>

    </tr>

    <tr valign="top">

        <td>

            <b>EBADF</b></td>

        <td valign="bottom">

            An invalid file descriptor was given in one of the sets. (Perhaps a file descriptor that was already closed, or one on which an error has occurred.)</td>

    </tr>

    <tr valign="top">

        <td>

            <b>EINTR</b></td>

        <td valign="bottom">

            A signal was caught.</td>

    </tr>

    <tr valign="top">

        <td>

            <b>EINVAL</b></td>

        <td valign="bottom">

            <i>nfds</i> is negative or the value contained within<i>timeout</i> is invalid.</td>

    </tr>

    <tr valign="top">

        <td>

            <b>ENOMEM</b></td>

        <td valign="bottom">

            unable to allocate memory for internal tables.</td>

    </tr>

</tbody>

</table>

<h1 class="manpages">

EXAMPLE</h1>

<table cellpadding="5" cellspacing="5" class="src">

<tbody>

    <tr>

        <td>

            <pre courier new monospace font-size: margin-bottom:>

include &lt;stdio.h&gt;

include &lt;sys/time.h&gt;

include &lt;sys/types.h&gt;

include &lt;unistd.h&gt;

int main(void) {

fd_set rfds;

struct timeval tv;

int retval;



/* Watch stdin (fd 0) to see when it has input. */

FD_ZERO(&amp;rfds);

FD_SET(0, &amp;rfds);

/* Wait up to five seconds. */

tv.tv_sec = 5;

tv.tv_usec = 0;

retval = select(1, &amp;rfds, NULL, NULL, &amp;tv);

/* Don’t rely on the value of tv now! */



if (retval == -1)

    perror("select()");

else if (retval)

    printf("Data is available now.\n");

    /* FD_ISSET(0, &amp;rfds) will be true. */

else

    printf("No data within five seconds.\n");

return 0;

}

</pre>

        </td>

    </tr>

</tbody>

</table>

<h1 class="manpages">

遵循于</h1>

<p>

<b>select</b>() conforms to POSIX.1-2001 and 4.4BSD</p>

<p>

<b>pselect</b>() is defined in POSIX.1g, and in POSIX.1-2001.</p>



                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="nanosleep()函数 Unix/Linux"><i class="layui-icon"></i>
                            nanosleep()函数 Unix/Linux</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:nfsservctl()函数 Unix/Linux"><i class="layui-icon"></i>
                            nfsservctl()函数 Unix/Linux</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复