<span class="Apple-style-span" style="background-color:rgb(255,255,255)"><span class="Apple-style-span" style="font-family:arial,sans-serif;font-size:14px">>For </span><span class="Apple-style-span" style="font-family:arial,sans-serif;font-size:14px">example, I have two pipe in the child process, one is fd1[2] and one</span><br style="font-family:arial,sans-serif;font-size:14px">
<span class="Apple-style-span" style="font-family:arial,sans-serif;font-size:14px">>is fd2[2], if I use fd1[0] and fd2[1], then I must close fd1[1] and</span><br style="font-family:arial,sans-serif;font-size:14px"><span class="Apple-style-span" style="font-family:arial,sans-serif;font-size:14px">>fd2[0]. In the parent process, I use fd1[1] and fd2[0] and I must</span><br style="font-family:arial,sans-serif;font-size:14px">
<span class="Apple-style-span" style="font-family:arial,sans-serif;font-size:14px">>close its pairs.</span></span><div><font class="Apple-style-span" color="#500050" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px"><br>
</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">I think that is the right way of using pipe. Because pipe only support one way communication. </span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">When you create a pipe then fork a child. Both the parent and the child holds fd1[2]. Then you can close the corresponding file descriptor to determine the direction of the data flow.</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">If you want to send data from parent to child using fd1[2].</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">You should close fd1[0] in your parent process and close fd1[1] in your child process.</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">In your parent process: close(fd1[0]), write(fd1[1],data,len);</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">In your child process: close(fd1[1]), read(fd1[0],data,len);</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">Is that what you are asking for?</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="font-size:14px">Thanks</span></font></div>
<div><br><div class="gmail_quote">2012/12/16 hyan2 <span dir="ltr"><<a href="mailto:hyan2@stevens.edu" target="_blank">hyan2@stevens.edu</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Thanks for your answer. But the read end of the pipe, say fd1[0], is connected to STDIN_FILENO(use dup2()), why I need close fd1[0] after dup2(). All of close function is before data communication process not after communication. That's why I am confused. By the way, I used two pipes connect to child's STDIN_FILENO and STDOUT_FILENO so that parent can directly write data(receive from socket) to child's STDIN_FILENO and read data from child's STDOUT_FILENO and send it to client. I think one pipe is not enough in my program.<div>

<br>
<br>
On Sun, 16 Dec 2012 10:36:40 -0500, Chao Cui wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
I think the pipe file descriptor with index 1 fd[1] is used for write<br>
and fd[0] is used for read. If you don't close fd[0] in the read side,<br>
it will block. That's why your browser will waiting for response.<br>
Further more, I don't think you need tow pipes. One is enough. You can<br>
just create one pipe in the parent process. Then you can use it to do<br>
communication between parent and child.<br>
See the example in the Book.<br>
<br>
Thanks<br>
Chao<br>
<br>
2012/12/16 hyan2<br>
<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Hi Professor,<br>
<br>
I use pipe for the communication between child process and parent<br>
process when I implement POST response. I use two pairs pipe but I<br>
find that I must close the other pipe to get the right result. For<br>
example, I have two pipe in the child process, one is fd1[2] and one<br>
is fd2[2], if I use fd1[0] and fd2[1], then I must close fd1[1] and<br>
fd2[0]. In the parent process, I use fd1[1] and fd2[0] and I must<br>
close its pairs. If I don't close those pairs I find my browser is<br>
waiting for result and when I terminate the server's process, the<br>
browser will get the right result. If I close those pairs, every<br>
thing is ok. I am confused about this. Could you explain this<br>
problem£¿<br>
<br>
Thank you.<br>
<br>
-Han<br>
______________________________<u></u>_________________<br>
cs631apue mailing list<br>
</div><a href="mailto:cs631apue@lists.stevens.edu" target="_blank">cs631apue@lists.stevens.edu</a> [1]<br>
<a href="https://lists.stevens.edu/cgi-bin/mailman/listinfo/cs631apue" target="_blank">https://lists.stevens.edu/cgi-<u></u>bin/mailman/listinfo/cs631apue</a> [2]<br>
</blockquote>
<br>
<br>
<br>
Links:<br>
------<br>
[1] mailto:<a href="mailto:cs631apue@lists.stevens.edu" target="_blank">cs631apue@lists.<u></u>stevens.edu</a><br>
[2] <a href="https://lists.stevens.edu/cgi-bin/mailman/listinfo/cs631apue" target="_blank">https://lists.stevens.edu/cgi-<u></u>bin/mailman/listinfo/cs631apue</a><br>
[3] mailto:<a href="mailto:hyan2@stevens.edu" target="_blank">hyan2@stevens.edu</a><br>
</blockquote><div><div>
______________________________<u></u>_________________<br>
cs631apue mailing list<br>
<a href="mailto:cs631apue@lists.stevens.edu" target="_blank">cs631apue@lists.stevens.edu</a><br>
<a href="https://lists.stevens.edu/cgi-bin/mailman/listinfo/cs631apue" target="_blank">https://lists.stevens.edu/cgi-<u></u>bin/mailman/listinfo/cs631apue</a><br>
</div></div></blockquote></div><br>
</div>