⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-iosys4.html

📁 vxworks相关论文
💻 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-iosys3.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-iosys5.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="83997">3.4  &nbsp;&nbsp;Buffered I/O: Stdio</a></i></h3></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="83999"> </a>The VxWorks I/O library provides a buffered I/O package that is compatible with the UNIX and Windows <i class="acronym_lc">stdio</i> package and provides full ANSI C support. To include the <i class="acronym_lc">stdio</i> package in the VxWorks system, select <b class="symbol_UC">INCLUDE_ANSI_STDIO</b> for inclusion in the project facility VxWorks view; see <i class="title">Tornado User's Guide: Projects</i> for information on configuring VxWorks. </p><dd><p class="Body"><a name="84000"> </a>Note that the implementation of <b class="routine"><i class="routine">printf</i></b><b>(</b>&nbsp;<b>)</b>, <b class="routine"><i class="routine">sprintf</i></b><b>(</b>&nbsp;<b>)</b>, and <b class="routine"><i class="routine">sscanf</i></b><b>(</b>&nbsp;<b>)</b>, traditionally considered part of the <i class="acronym_lc">stdio</i> package, is part of a different package in VxWorks. These routines are discussed in <a href="c-iosys5.html#84032"><i class="title">3.5&nbsp;Other Formatted I/O</i></a>. </p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="84004">3.4.1  &nbsp;&nbsp;Using Stdio</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84005"> </a>Although the VxWorks I/O system is efficient, some overhead is associated with each low-level call. First, the I/O system must dispatch from the device-independent user call (<b class="routine"><i class="routine">read</i></b><b>(</b>&nbsp;<b>)</b>, <b class="routine"><i class="routine">write</i></b><b>(</b>&nbsp;<b>)</b>, and so on) to the driver-specific routine for that function. Second, most drivers invoke a mutual exclusion or queuing mechanism to prevent simultaneous requests by multiple users from interfering with each other.</p><dd><p class="Body"><a name="84006"> </a>Because the VxWorks primitives are fast, this overhead is quite small. However, an application processing a single character at a time from a file incurs that overhead for each character if it reads each character with a separate <b class="routine"><i class="routine">read</i></b><b>(</b>&nbsp;<b>)</b> call:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84007"><i class="textVariable">n</i> = read (<i class="textVariable">fd</i>, &amp;<i class="textVariable">char</i>, 1);</a></b></pre></dl><dd><p class="Body"><a name="84008"> </a>To make this type of I/O more efficient and flexible, the <i class="acronym_lc">stdio</i> package implements a buffering scheme in which data is read and written in large chunks and buffered privately. This buffering is transparent to the application; it is handled automatically by the <i class="acronym_lc">stdio</i> routines and macros. To access a file with <i class="acronym_lc">stdio</i>, a file is opened with <b class="routine"><i class="routine">fopen</i></b><b>(</b>&nbsp;<b>)</b> instead of <b class="routine"><i class="routine">open</i></b><b>(</b>&nbsp;<b>)</b> (many <i class="acronym_lc">stdio</i> calls begin with the letter <i class="i">f</i>):</p><dl class="margin"><dd><pre class="Code2"><b><a name="84010"><i class="textVariable">fp</i> = fopen ("/usr/foo", "r");</a></b></pre></dl><dd><p class="Body"><a name="84012"> </a>The returned value, a <i class="term">file pointer</i> (or<i class="acronym_lc"> fp</i>) is a handle for the opened file and its associated buffers and pointers. An <i class="acronym_lc">fp</i> is actually a pointer to the associated data structure of type <b class="symbol_UC">FILE</b> (that is, it is declared as <b class="symbol_UC">FILE *</b>). By contrast, the low-level I/O routines identify a file with a <i class="term">file descriptor</i> (<i class="acronym_lc">fd</i>), which is a small integer. In fact, the <b class="symbol_UC">FILE</b> structure pointed to by the <i class="acronym_lc">fp</i> contains the underlying <i class="acronym_lc">fd</i> of the open file.</p><dd><p class="Body"><a name="84014"> </a>An already open <i class="acronym_lc">fd</i> can be associated belatedly with a <b class="symbol_UC">FILE</b> buffer by calling <b class="routine"><i class="routine">fdopen</i></b><b>(</b>&nbsp;<b>)</b>:</p><dl class="margin"><dd><pre class="Code2"><b><a name="84015"><i class="textVariable">fp</i> = fdopen (<i class="textVariable">fd</i>, "r");</a></b></pre></dl><dd><p class="Body"><a name="84017"> </a>After a file is opened with <b class="routine"><i class="routine">fopen</i></b><b>(</b>&nbsp;<b>)</b>, data can be read with <b class="routine"><i class="routine">fread</i></b><b>(</b>&nbsp;<b>)</b>, or a character at a time with <b class="routine"><i class="routine">getc</i></b><b>(</b>&nbsp;<b>)</b>, and data can be written with <b class="routine"><i class="routine">fwrite</i></b><b>(</b>&nbsp;<b>)</b>, or a character at a time with <b class="routine"><i class="routine">putc</i></b><b>(</b>&nbsp;<b>)</b>.</p><dd><p class="Body"><a name="84021"> </a>The routines and macros to get data into or out of a file are extremely efficient. They access the buffer with direct pointers that are incremented as data is read or written by the user. They pause to call the low-level read or write routines only when a read buffer is empty or a write buffer is full.</p></dl></dl><dl class="margin"><dd><p class="table" callout><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td valign="top" width="40"><br><img border="0" alt="*" src="icons/warning.gif"></td><td><hr><div class="CalloutCell"><a name="96582"><b class="symbol_UC"><font face="Helvetica, sans-serif" size="-1" class="sans">WARNING:  </font></b></a>The <i class="acronym_lc">stdio</i> buffers and pointers are <i class="term">private</i> to a particular task. They are <i class="emphasis">not</i> interlocked with semaphores or any other mutual exclusion mechanism, because this defeats the point of an efficient private buffering scheme. Therefore, multiple tasks must not perform I/O to the same <i class="acronym_lc">stdio</i> <b class="symbol_UC">FILE</b> pointer at the same time.</div></td></tr><tr valign="top"><td></td><td><hr></td></tr><tr valign="middle"><td colspan="20"></td></tr></table></p callout><dl class="margin"><dd><p class="Body"><a name="96588"> </a>The <b class="symbol_UC">FILE</b> buffer is deallocated when <b class="routine"><i class="routine">fclose</i></b><b>(&nbsp;)</b> is called. </p></dl></dl><font face="Helvetica, sans-serif" class="sans"><h4 class="H3"><i><a name="96589">3.4.2  &nbsp;&nbsp;Standard Input, Standard Output, and Standard Error</a></i></h4></font><dl class="margin"><dl class="margin"><dd><p class="Body"><a name="84028"> </a>As discussed earlier in <a href="c-iosys3.html#83659"><i class="title">3.3&nbsp;Basic I/O</i></a>, there are three special file descriptors (0, 1, and 2) reserved for standard input, standard output, and standard error. Three corresponding <i class="acronym_lc">stdio</i> <b class="symbol_UC">FILE</b> buffers are automatically created when a task uses the standard file descriptors, <i class="acronym_lc">stdin</i>, <i class="acronym_lc">stdout</i>, and <i class="acronym_lc">stderr,</i> to do buffered I/O to the standard <i class="acronym_lc">fd</i>s. Each task using the standard I/O <i class="acronym_lc">fds</i> has its own <i class="acronym_lc">stdio</i> <b class="symbol_UC">FILE</b> buffers. The <b class="symbol_UC">FILE</b> buffers are deallocated when the task exits. </p></dl></dl><a name="foot"><hr></a><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-iosys3.html"><img border="0" alt="[Prev]" src="icons/prev.gif"></a><a href="c-iosys5.html"><img border="0" alt="[Next]" src="icons/next.gif"></a></p></body></html><!---by WRS Documentation (), Wind River Systems, Inc.    conversion tool:  Quadralay WebWorks Publisher 4.0.11    template:         CSS Template, Jan 1998 - Jefro --->

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -