ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/pipe2.htm

291 lines
7.5 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>pipe()--Create an Interprocess Channel</title>
<!-- Begin Header Records -->
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Change History: -->
<!-- YYMMDD USERID Change description -->
<!-- Created by Jerry Von Berge for V5R1-->
<!-- Change History: -->
<!-- 020530 JTROUS Changes for Virus scanner, 98860, V5R3 -->
<!-- 0207?? JET This file has undergone html cleanup -->
<!-- 050404 JTROUS: fix enums, no change flag, V5R4 -->
<!-- End Header Records -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!--Java sync-link-->
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
</script>
<a name="Top_Of_Page"></a>
<h2>pipe()--Create an Interprocess Channel</h2>
<div class="box" style="width: 60%;">
<br>
&nbsp;&nbsp;Syntax<br>
<pre>
#include &lt;unistd.h&gt;
int pipe(int <em>fildes[2]</em>);
</pre>
&nbsp;&nbsp;Service Program Name: QP0LLIB1<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Default Public Authority: *USE<br>
<!-- iddvc RMBR -->
<br>
&nbsp;&nbsp;Threadsafe: Yes<br>
<!-- iddvc RMBR -->
<br>
</div>
<p>The <strong>pipe()</strong> function creates a data pipe and places two file
descriptors, one each into the arguments <em>fildes[0]</em> and <em>
fildes[1]</em>, that refer to the open file descriptions for the read and write
ends of the pipe, respectively. Their integer values will be the two lowest
available at the time of the <strong>pipe()</strong> call. The O_NONBLOCK and
FD_CLOEXEC flags will be clear on both descriptors. NOTE: these flags can,
however, be set by the <strong>fcntl()</strong> function.</p>
<p>Data can be written to the file descriptor <em>fildes[1]</em> and read from
file descriptor <em>fildes[0]</em>. A read on the file descriptor <em>
fildes[0]</em> will access data written to the file descriptor <em>
fildes[1]</em> on a first-in-first-out basis. File descriptor <em>
fildes[0]</em> is open for reading only. File descriptor <em>fildes[1]</em> is
open for writing only.</p>
<p>The <strong>pipe()</strong> function is often used with the <strong>
spawn()</strong> function to allow the parent and child processes to send data
to each other.</p>
<p>Upon successful completion, <strong>pipe()</strong> will update the access
time, change time, and modification time of the pipe.</p>
<br>
<h3>Parameters</h3>
<dl>
<dt><strong><em>fildes[2]</em></strong></dt>
<dd>(Output) An integer array of size 2 that will receive the pipe
descriptors.</dd>
</dl>
<br>
<h3>Authorities</h3>
<p>None.</p>
<br>
<h3>Return Value</h3>
<table cellpadding="5">
<!-- cols="5 95" -->
<tr>
<td align="left" valign="top"><em>0</em></td>
<td align="left" valign="top"><strong>pipe()</strong> was successful.</td>
</tr>
<tr>
<td align="left" valign="top"><em>-1</em></td>
<td align="left" valign="top"><strong>pipe()</strong> was not successful. The
<em>errno</em> variable is set to indicate the error.</td>
</tr>
</table>
<br>
<br>
<h3>Error Conditions</h3>
<p>If <strong>pipe()</strong> is not successful, <em>errno</em> usually
indicates one of the following errors. Under some conditions, <em>errno</em>
could indicate an error other than those listed here.</p>
<table cellpadding="5">
<!-- cols="25 75" -->
<tr>
<th align="left" valign="bottom">Error condition</th>
<th align="left" valign="bottom">Additional information</th>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EFAULT">EFAULT</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EMFILE">EMFILE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENFILE">ENFILE</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#ENOMEM">ENOMEM</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
<tr>
<td align="left" valign="top">
<em>[<a href="unix14.htm#EUNKNOWN">EUNKNOWN</a>]</em></td>
<td align="left" valign="top">
&nbsp;
</td>
</tr>
</table>
<br>
<h3><a name="USAGE_NOTES">Usage Notes</a></h3>
<ol>
<li>If this function is called
by a thread executing one of the scan-related exit programs (or any of its
created threads), the descriptors that are returned are scan descriptors. See
<a href="ifsopenexit.htm">Integrated File System Scan on Open Exit Programs</a>
and <a href="ifscloseexit.htm">Integrated File System Scan on Close Exit
Programs</a> for more information. If a process is spawned, these scan
descriptors are not inherited by the spawned process and therefore cannot be
used in that spawned process. Therefore, in this case, the descriptors returned
by <strong>pipe()</strong> function will only work within the same process.
</li>
</ol>
<br>
<h3>Related Information</h3>
<ul>
<li>The &lt;<strong>unistd.h</strong>&gt; file (see <a href="unix13.htm">Header
Files for UNIX-Type Functions</a>)<br>
</li>
<li>The &lt;<strong>fcntl.h</strong>&gt; file (see <a href="unix13.htm">Header
Files for UNIX-Type Functions</a>)<br>
</li>
<li><a href="fcntl.htm">fcntl()</a>--Perform File Control Command<br>
</li>
<li><a href="fstat.htm">fstat()</a>--Get File Information by Descriptor<br>
</li>
<li><a href="pipe.htm">Qp0zPipe()</a>--Create Interprocess Channel with
Sockets<br>
</li>
<li><a href="read.htm">read()</a>--Read from Descriptor<br>
</li>
<li><a href="spawn.htm">spawn()</a>--Spawn Process<br>
</li>
<li><a href="write.htm">write()</a>--Write to Descriptor</li>
</ul>
<br>
<h3>Example</h3>
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<p>The following example creates a pipe, writes 10 bytes of data to the pipe,
and then reads those 10 bytes of data from the pipe.</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
void main()
{
int fildes[2];
int rc;
char writeData[10];
char readData[10];
int bytesWritten;
int bytesRead;
memset(writeData,'A',10);
if (-1 == pipe(fildes))
{
perror("pipe error");
return;
}
if (-1 == (bytesWritten = write(fildes[1],
writeData,
10)))
{
perror("write error");
}
else
{
printf("wrote %d bytes\n",bytesWritten);
if (-1 == (bytesRead = read(fildes[0],
readData,
10)))
{
perror("read error");
}
else
{
printf("read %d bytes\n",bytesRead);
}
}
close(fildes[0]);
close(fildes[1]);
return;
}
</pre>
<br>
<hr>
API introduced: V5R1
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>