<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0">
<channel>
  <title>Linux Format forums</title>
  <link>http://linuxformat.com/forums/index.php</link>
  <description>Help, discussion, magazine feedback and more</description>
  <language>english</language>
  <copyright>(c) Copyright Thu May 23, 2013 5:02 pm by Linux Format forums</copyright>
  <managingEditor>webmaster@linuxformat.com</managingEditor>
  <webMaster>webmaster@linuxformat.com</webMaster>
  <pubDate>Thu May 23, 2013 5:02 pm</pubDate>
  <lastBuildDate>Thu May 23, 2013 5:02 pm</lastBuildDate>
  <docs>http://backend.userland.com/rss</docs>
  <generator>phpBB2 RSS Syndication Mod by Lucas</generator>
  <ttl>1</ttl>

  <image>
    <title>Linux Format forums</title>
    <url></url>
    <link>http://linuxformat.com/forums/</link>
    <description>Help, discussion, magazine feedback and more</description>
  </image>

                                      <item>
                                        <title>RE: Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22386#22386</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7355'&gt;smartgirl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Sun Apr 30, 2006 4:29 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      thanx for your reply &lt;br /&gt;
&lt;br /&gt;
the problem occur while reading&lt;br /&gt;
&lt;br /&gt;
we can avoid this problem by reading from pipe using  while loop</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22386#22386</comments>
                                        <author>smartgirl</author>
                                        <pubDate>Sun Apr 30, 2006 4:29 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22386#22386</guid>
                                      </item>
                                      <item>
                                        <title>RE: Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22265#22265</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=162'&gt;Nigel&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 9:04 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      DOH! - I've just spotted what's really wrong here.&lt;br /&gt;
&lt;br /&gt;
OK, first problem - you're writing too long a string to the pipe from the child process. Your line is &lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;write&amp;#40;fd&amp;#91;1&amp;#93;,token,&amp;#40;strlen&amp;#40;token&amp;#41;+1&amp;#41;&amp;#41;;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; &lt;br /&gt;
&lt;br /&gt;
This means you are also writing the trailing NULL at the end of each &quot;token&quot;.&lt;br /&gt;
&lt;br /&gt;
Next problem - your parent process is waiting until the child has exited before it starts reading from the pipe.&lt;br /&gt;
So as you are asking for &lt;span style=&quot;font-style: italic&quot;&gt;sizeof(word)&lt;/span&gt; characters, it is reading the entire contents of the pipe at once.&lt;br /&gt;
BUT, because you have put the trailing NULLs after each token, you only get to see &quot;I&quot; in word.&lt;br /&gt;
&lt;br /&gt;
You can cure the first problem by using this statement to write to the pipe&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;write&amp;#40;fd&amp;#91;1&amp;#93;,token,strlen&amp;#40;token&amp;#41;&amp;#41;;&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt; &lt;br /&gt;
But I think you might have to do something to get the child to wait until the parent has read the pipe before it writes the next token to cure the second problem, as even when I took out the wait statement, it still didn't put anything out from the parent before the child had finished writing.</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22265#22265</comments>
                                        <author>Nigel</author>
                                        <pubDate>Fri Apr 28, 2006 9:04 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22265#22265</guid>
                                      </item>
                                      <item>
                                        <title>RE: Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22261#22261</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=162'&gt;Nigel&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 8:30 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      I put an extra print statement in the parent part to print out nb after each read from the pipe. &lt;br /&gt;
The first read gets 32 characters, the other 6 reads get 0 characters. Each time you are printing out the first character of word, which hasn't changed (because nothing got read from the pipe).&lt;br /&gt;
&lt;br /&gt;
I think that the problem is you've told the read statement you want sizeof(word) characters, so it doesn't return to parent until it either has that many characters or the pipe is closed. But Ive not done much with pipes, so I'm not certain.&lt;br /&gt;
&lt;br /&gt;
Perhaps you should try reading a single character at a time, building up word until you get a space or return ?</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22261#22261</comments>
                                        <author>Nigel</author>
                                        <pubDate>Fri Apr 28, 2006 8:30 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22261#22261</guid>
                                      </item>
                                      <item>
                                        <title>RE: Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22234#22234</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7355'&gt;smartgirl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 12:00 pm&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      it is some how similar but there was easier since they only send the string once and recieved once but in mine sends I want to send 7 strings and recive 7 by the parent .. did you get what I meant?&lt;br /&gt;
&lt;br /&gt;
Thanx anyway for help and kind response</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22234#22234</comments>
                                        <author>smartgirl</author>
                                        <pubDate>Fri Apr 28, 2006 12:00 pm</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22234#22234</guid>
                                      </item>
                                      <item>
                                        <title>RE: Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22233#22233</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7355'&gt;smartgirl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 11:59 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      it is some how similar but there was easier since they only send the string once and recieved once but in mine sends I want to send 7 strings and recive 7 by the parent .. did you get what I meant?&lt;br /&gt;
&lt;br /&gt;
Thanx anyway for help and kind response</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22233#22233</comments>
                                        <author>smartgirl</author>
                                        <pubDate>Fri Apr 28, 2006 11:59 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22233#22233</guid>
                                      </item>
                                      <item>
                                        <title>Examples</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22225#22225</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=4003'&gt;bluemonki&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 10:20 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      Check this page out - near the bottom they have an example similar to yours.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.tldp.org/LDP/lpg/node11.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot; class=&quot;postlink&quot;&gt;http://www.tldp.org/LDP/lpg/node11.html&lt;/a&gt;</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22225#22225</comments>
                                        <author>bluemonki</author>
                                        <pubDate>Fri Apr 28, 2006 10:20 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22225#22225</guid>
                                      </item>
                                      <item>
                                        <title>pipes</title>
                                        <link>http://linuxformat.com/forums/viewtopic.php?p=22222#22222</link>
                                        <description>&lt;br /&gt;
                                      Author: &lt;a href='http://linuxformat.com/forums/profile.php?mode=viewprofile&amp;u=7355'&gt;smartgirl&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
                                      Posted: Fri Apr 28, 2006 9:38 am&lt;br /&gt;&lt;br /&gt;
                                      &lt;br /&gt;&lt;br /&gt;
                                      hi all&lt;br /&gt;
&lt;br /&gt;
I am trying to be much familier with pipes &lt;br /&gt;
&lt;br /&gt;
I tried to write a c program that generates the following output&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight: bold&quot;&gt;from child:&lt;br /&gt;
I &lt;br /&gt;
want &lt;br /&gt;
to &lt;br /&gt;
print&lt;br /&gt;
this&lt;br /&gt;
line&lt;br /&gt;
twice&lt;br /&gt;
&lt;br /&gt;
from parent:&lt;br /&gt;
I &lt;br /&gt;
want &lt;br /&gt;
to &lt;br /&gt;
print&lt;br /&gt;
this&lt;br /&gt;
line&lt;br /&gt;
twice&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
what I get is&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-weight: bold&quot;&gt;from child:&lt;br /&gt;
I &lt;br /&gt;
want &lt;br /&gt;
to &lt;br /&gt;
print&lt;br /&gt;
this&lt;br /&gt;
line&lt;br /&gt;
twice&lt;br /&gt;
&lt;br /&gt;
from parent:&lt;br /&gt;
I &lt;br /&gt;
I&lt;br /&gt;
I&lt;br /&gt;
I&lt;br /&gt;
I&lt;br /&gt;
I&lt;br /&gt;
I&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
my code is&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;table width=&quot;90%&quot; cellspacing=&quot;1&quot; cellpadding=&quot;3&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;&lt;tr&gt; 	  &lt;td&gt;&lt;span class=&quot;genmed&quot;&gt;&lt;b&gt;Code:&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;	&lt;/tr&gt;	&lt;tr&gt;	  &lt;td class=&quot;code&quot;&gt;&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;string.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main&amp;#40;void&amp;#41;&amp;#123;&lt;br /&gt;
int childpid,fd&amp;#91;2&amp;#93;,nb,i,j;&lt;br /&gt;
char line&amp;#91;BUFSIZ&amp;#93;=&amp;quot;I want to print this line twice&amp;quot;;&lt;br /&gt;
char word&amp;#91;BUFSIZ&amp;#93;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
pipe&amp;#40;fd&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
childpid=fork&amp;#40;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
if&amp;#40;childpid==0&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
printf&amp;#40;&amp;quot;from child&amp;#58;\n&amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
close&amp;#40;fd&amp;#91;0&amp;#93;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
char *token=strtok&amp;#40;line,&amp;quot; &amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
while&amp;#40;token!=NULL&amp;#41;&lt;br /&gt;
&amp;#123;&lt;br /&gt;
printf&amp;#40;&amp;quot; %s\n&amp;quot;,token&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
write&amp;#40;fd&amp;#91;1&amp;#93;,token,&amp;#40;strlen&amp;#40;token&amp;#41;+1&amp;#41;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
token=strtok&amp;#40;NULL,&amp;quot; &amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
else&lt;br /&gt;
&amp;#123;&lt;br /&gt;
&lt;br /&gt;
wait&amp;#40;NULL&amp;#41;;&lt;br /&gt;
printf&amp;#40;&amp;quot;from parent&amp;#58;\n&amp;quot;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
close&amp;#40;fd&amp;#91;1&amp;#93;&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
for&amp;#40; i=0;i&amp;lt;7;i++&amp;#41;&amp;#123;&lt;br /&gt;
nb=read&amp;#40;fd&amp;#91;0&amp;#93;,word,sizeof&amp;#40;word&amp;#41;&amp;#41;;&lt;br /&gt;
printf&amp;#40;&amp;quot;%s\n&amp;quot;,word&amp;#41;;&lt;br /&gt;
&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
&lt;br /&gt;
&amp;#125;&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;	&lt;/tr&gt;&lt;/table&gt;&lt;span class=&quot;postbody&quot;&gt;&lt;br /&gt;
&lt;br /&gt;
can any one tell me what's wronge with this code and how to correct it?</description>
                                        <comments>http://linuxformat.com/forums/viewtopic.php?p=22222#22222</comments>
                                        <author>smartgirl</author>
                                        <pubDate>Fri Apr 28, 2006 9:38 am</pubDate>
                                        <guid isPermaLink="true">http://linuxformat.com/forums/viewtopic.php?p=22222#22222</guid>
                                      </item></channel></rss>