📄 select_and_poll.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head> <title></title> <link rel="stylesheet" media="screen" type="text/css" href="./style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="./design.css" /> <link rel="stylesheet" media="print" type="text/css" href="./print.css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><div class="toc"><div class="tocheader toctoggle" id="toc__header">Table of Contents</div><div id="toc__inside"><ul class="toc"><li class="clear"><ul class="toc"><li class="level2"><div class="li"><span class="li"><a href="#device_driver_poll_interface" class="toc">Device Driver POLL interface</a></span></div><ul class="toc"><li class="level3"><div class="li"><span class="li"><a href="#poll_in_user_space" class="toc">POLL in User Space</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#select_in_user_space" class="toc">SELECT in User Space</a></span></div></li><li class="level3"><div class="li"><span class="li"><a href="#poll_example_code" class="toc">Poll Example code</a></span></div></li></ul></li></ul></li></ul></div></div><h2><a name="device_driver_poll_interface" id="device_driver_poll_interface">Device Driver POLL interface</a></h2><div class="level2"><p> Sometimes a program wants to wait on events from several devices. A time out can also be used so that the system can perform housekeeping or diagnostic tasks while waiting.</p><p>In BSD Unix systems this was done using the old select system call. More detailed control options are provided by the System V poll interface.</p><p>The device driver code is always provided by the poll interface. This interface is quite simple with the underlying kernel systems providing much of the required infrastructure.</p><p>The system uses a <strong>poll_wait</strong> call to indicate that the <strong>poll</strong> system is interested in events. The <strong>poll_wait</strong> call includes a reference to a wait queue that must be triggered by a driver event. Another argument to <strong>poll_wait</strong> function is a poll_table structure. This is used as an opaque object only.</p><p>The driver <strong>poll</strong> function has two main duties to perform. </p><ul><li class="level1"><div class="li"> Register any wait queues used by the driver with the poll system.</div></li><li class="level1"><div class="li"> Check for any pending events and return a mask with appropriate bits set.</div></li></ul><p>The mask is returned in the events member of the pollfd structure. The include file <strong>sys/poll.h</strong> defines the bit masks.</p><p>The following table describes the most significant bit meanings: </p><table class="inline"> <tr> <td class="leftalign">Name </td><td class="leftalign">Description </td> </tr> <tr> <td>POLLIN </td><td class="leftalign">Set when normal data can be read without blocking </td> </tr> <tr> <td>POLLPRI</td><td class="leftalign">Set when High Priority data (out-of-band) can be read without blocking </td> </tr> <tr> <td>POLLHUP</td><td class="leftalign">Set when the device sees “end-of-file” </td> </tr> <tr> <td>POLLERR</td><td class="leftalign">Set when an error condition has occurred on the device </td> </tr> <tr> <td>POLLOUT</td><td class="leftalign">Set when normal data can be WRITTEN without blocking </td> </tr></table><br /></div><!-- SECTION [1-1746] --><h3><a name="poll_in_user_space" id="poll_in_user_space">POLL in User Space</a></h3><div class="level3"><p> The user space interface is: int POLL(pollfd_structure , int count , int timeout ) An example:</p><pre class="code c"> <span class="coMULTI">/* pollfds structure , number of files of interest , timeout in mS */</span> result = poll<span class="br0">(</span>pfds, <span class="nu0">1</span>, <span class="nu0">1000</span><span class="br0">)</span>;</pre><p>The pollfd structure needs to be set up with the file descriptor and events of interest as follows; </p><pre class="code c"> <span class="kw4">struct</span> pollfd pfds<span class="br0">[</span>NUM_POLLFDS<span class="br0">]</span>; <span class="coMULTI">/* get the fd for the device using an open call */</span> pfds<span class="br0">[</span>index<span class="br0">]</span>.<span class="me1">fd</span>=open<span class="br0">(</span>DEVICE,O_RDWR<span class="br0">)</span> <span class="coMULTI">/* select events of interest in this case read and write */</span> pfds<span class="br0">[</span>index<span class="br0">]</span>.<span class="me1">events</span>=POLLIN|POLLOUT;</pre></div><!-- SECTION [1747-2864] --><h3><a name="select_in_user_space" id="select_in_user_space">SELECT in User Space</a></h3><div class="level3"><p> The <strong>select</strong> call in user space is more complex. It will only work where the io device has been set up as <strong>NON BLOCKING</strong>. The following is an example of setting up a socket as <strong>NON BLOCKING</strong>.</p><pre class="code c"> <span class="co2">#include <ctype.h> </span> <span class="co2">#include <sys/time.h> </span> <span class="co2">#include <fcntl.h> </span> ...<span class="br0">(</span>stuff missing<span class="br0">)</span> <span class="kw4">int</span> ret; ret = fcntl<span class="br0">(</span>socket,F_GETFL<span class="br0">)</span>; <span class="kw1">if</span> <span class="br0">(</span>ret < <span class="nu0">0</span><span class="br0">)</span> <span class="br0">{</span> perror<span class="br0">(</span><span class="st0">"fcntl(F_SETFL)"</span><span class="br0">)</span>; exit<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span>; <span class="br0">}</span> ret = <span class="br0">(</span>ret | O_NONBLOCK<span class="br0">)</span>; <span class="kw1">if</span> <span class="br0">(</span>fcntl<span class="br0">(</span>socket,F_SETFL,ret<span class="br0">)</span> < <span class="nu0">0</span><span class="br0">)</span> <span class="br0">{</span> perror<span class="br0">(</span><span class="st0">"fcntl(F_SETFL)"</span><span class="br0">)</span>; exit<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span>; <span class="br0">}</span></pre><p>The select man page gives a good example on how to use the select function in user space.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -