📄 c-iosys3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><link rel="STYLESHEET" type="text/css" href="wrs.css"><title> I/O System </title></head><body bgcolor="FFFFFF"><p class="navbar" align="right"><a href="index.html"><img border="0" alt="[Contents]" src="icons/contents.gif"></a><a href="GuideIX.html"><img border="0" alt="[Index]" src="icons/index.gif"></a><a href="c-iosys.html"><img border="0" alt="[Top]" src="icons/top.gif"></a><a href="c-iosys2.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-iosys4.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p><font face="Helvetica, sans-serif" class="sans"><h3 class="H2"><i><a name="83659">3.3 Basic I/O</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83660"> </a>Basic I/O is the lowest level of I/O in VxWorks. The basic I/O interface is source-compatible with the I/O primitives in the standard C library. There are seven basic I/O calls, shown in the following table. <p class="table"><h4 class="EntityTitle"><a name="83663"><font face="Helvetica, sans-serif" size="-1" class="sans">Table 3-1: Basic I/O Routines</font></a></h4><table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="83667"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Call</font></b></div></th><th rowspan="1" colspan="1"><div class="CellHeading"><b><a name="83669"> </a><font face="Helvetica, sans-serif" size="-1" class="sans">Description</font></b></div></th></tr><tr><td colspan="20"><hr class="tablerule2"></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83676"> </a><b class="routine"><i class="routine">creat</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83678"> </a>Create a file. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83681"> </a><b class="routine"><i class="routine">remove</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83683"> </a>Remove a file. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83686"> </a><b class="routine"><i class="routine">open</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83688"> </a>Open a file. (Optionally, create a file.) </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83691"> </a><b class="routine"><i class="routine">close</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83693"> </a>Close a file. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83696"> </a><b class="routine"><i class="routine">read</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83698"> </a>Read a previously created or opened file. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83701"> </a><b class="routine"><i class="routine">write</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83703"> </a>Write a previously created or opened file. </div></td></tr><tr valign="top"><td colspan=1 rowspan=1><div class="CellBody"><a name="83707"> </a><b class="routine"><i class="routine">ioctl</i></b><b>(</b> <b>)</b> </div></td><td colspan=1 rowspan=1><div class="CellBody"><a name="83709"> </a>Perform special control functions on files or devices. </div></td></tr><tr><td colspan="20"><hr class="tablerule"></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p></p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="83716">3.3.1 File Descriptors</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83717"> </a>At the basic I/O level, files are referred to by a <i class="term">file descriptor</i>, or <i class="acronym_lc">fd</i>. An <i class="acronym_lc">fd</i> is a small integer returned by a call to <b class="routine"><i class="routine">open</i></b><b>(</b> <b>)</b> or <b class="routine"><i class="routine">creat</i></b><b>(</b> <b>)</b>. The other basic I/O calls take an <i class="acronym_lc">fd</i> as a parameter to specify the intended file. An <i class="acronym_lc">fd</i> has no meaning discernible to the user; it is only a handle for the I/O system.</p><dd><p class="Body"><a name="83718"> </a>When a file is opened, an <i class="acronym_lc">fd</i> is allocated and returned. When the file is closed, the <i class="acronym_lc">fd</i> is deallocated. There are a finite number of <i class="acronym_lc">fd</i>s available in VxWorks. To avoid exceeding the system limit, it is important to close <i class="acronym_lc">fd</i>s that are no longer in use. The number of available <i class="acronym_lc">fd</i>s is specified in the initialization of the I/O system.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="83720">3.3.2 Standard Input, Standard Output, and Standard Error</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83721"> </a>Three file descriptors are reserved and have special meanings:</p><dl class="margin"><dd><div class="Indent"><a name="83722"> </a>0 = standard input <br>1 = standard output <br>2 = standard error output</div><br></dl><dd><p class="Body"><a name="83723"> </a>These <i class="acronym_lc">fd</i>s are never returned as the result of an <b class="routine"><i class="routine">open</i></b><b>(</b> <b>)</b> or <b class="routine"><i class="routine">creat</i></b><b>(</b> <b>)</b>, but serve rather as indirect references that can be redirected to any other open <i class="acronym_lc">fd</i>.</p><dd><p class="Body"><a name="83724"> </a>These standard <i class="acronym_lc">fd</i>s are used to make tasks and modules independent of their actual I/O assignments. If a module sends its output to standard output (<i class="acronym_lc">fd</i> = 1), then its output can be redirected to any file or device, without altering the module. </p><dd><p class="Body"><a name="83725"> </a>VxWorks allows two levels of redirection. First, there is a global assignment of the three standard <i class="acronym_lc">fd</i>s. Second, individual tasks can override the global assignment of these <i class="acronym_lc">fd</i>s with their own assignments that apply only to that task.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="83727">Global Redirection</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83728"> </a>When VxWorks is initialized, the global assignments of the standard <i class="acronym_lc">fd</i>s are directed, by default, to the system console. When tasks are spawned, they initially have no task-specific <i class="acronym_lc">fd</i> assignments; instead, they use the global assignments.</p><dd><p class="Body"><a name="83731"> </a>The global assignments can be redirected using <b class="routine"><i class="routine">ioGlobalStdSet</i></b><b>(</b> <b>)</b>. The parameters to this routine are the global standard <i class="acronym_lc">fd</i> to be redirected, and the <i class="acronym_lc">fd</i> to direct it to. </p><dd><p class="Body"><a name="83732"> </a>For example, the following call sets global standard output (<i class="acronym_lc">fd</i> = 1) to be the open file with a file descriptor of <b class="symbol_lc">fileFd</b>:</p><dl class="margin"><dd><pre class="Code2"><b><a name="83733">ioGlobalStdSet (1, fileFd);</a></b></pre></dl><dd><p class="Body"><a name="83734"> </a>All tasks in the system that do not have their own task-specific redirection write standard output to that file thereafter. For example, the task<b class="task"> tRlogind </b>calls <b class="routine"><i class="routine">ioGlobalStdSet</i></b><b>(</b> <b>)</b> to redirect I/O across the network during an <b class="command">rlogin</b> session.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H4"><i><a name="83735">Task-Specific Redirection</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83737"> </a>The assignments for a specific task can be redirected using the routine <b class="routine"><i class="routine">ioTaskStdSet</i></b><b>(</b> <b>)</b>. The parameters to this routine are the task ID (0 = self) of the task with the assignments to be redirected, the standard <i class="acronym_lc">fd</i> to be redirected, and the <i class="acronym_lc">fd</i> to direct it to. For example, a task can make the following call to write standard output to <b class="symbol_lc">fileFd</b>:</p><dl class="margin"><dd><pre class="Code2"><b><a name="83738">ioTaskStdSet (0, 1, fileFd);</a></b></pre></dl><dd><p class="Body"><a name="83739"> </a>All other tasks are unaffected by this redirection, and subsequent global redirections of standard output do not affect this task.</p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="83741">3.3.3 Open and Close</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83742"> </a>Before I/O can be performed to a device, an <i class="acronym_lc">fd</i> must be opened to that device by invoking the <b class="routine"><i class="routine">open</i></b><b>(</b> <b>)</b> routine (or <b class="routine"><i class="routine">creat</i></b><b>(</b> <b>)</b>, as discussed in the next section). The arguments to <b class="routine"><i class="routine">open</i></b><b>(</b> <b>)</b> are the file name, the type of access, and, when necessary, the mode:</p><dl class="margin"><dd><pre class="Code2"><b><a name="83744"> <i class="textVariable">fd</i> = open ("<i class="textVariable">name</i>", <i class="textVariable">flags</i>, <i class="textVariable">mode</i>);</a></b></pre></dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -