Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pipes

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
smartgirl



Joined: Fri Apr 28, 2006 9:35 am
Posts: 4

PostPosted: Fri Apr 28, 2006 9:38 am    Post subject: pipes Reply with quote

hi all

I am trying to be much familier with pipes

I tried to write a c program that generates the following output


from child:
I
want
to
print
this
line
twice

from parent:
I
want
to
print
this
line
twice


what I get is


from child:
I
want
to
print
this
line
twice

from parent:
I
I
I
I
I
I
I


my code is



Code:

 
#include<stdio.h>
#include<string.h>

int main(void){
int childpid,fd[2],nb,i,j;
char line[BUFSIZ]="I want to print this line twice";
char word[BUFSIZ];


pipe(fd);

childpid=fork();

if(childpid==0)
{
printf("from child:\n");

close(fd[0]);

char *token=strtok(line," ");

while(token!=NULL)
{
printf(" %s\n",token);

write(fd[1],token,(strlen(token)+1));

token=strtok(NULL," ");

}

}

else
{

wait(NULL);
printf("from parent:\n");



close(fd[1]);

for( i=0;i<7;i++){
nb=read(fd[0],word,sizeof(word));
printf("%s\n",word);

}

}

return 0;

}



can any one tell me what's wronge with this code and how to correct it?
Back to top
View user's profile Send private message
bluemonki



Joined: Sat Oct 29, 2005 9:18 pm
Posts: 11
Location: uk

PostPosted: Fri Apr 28, 2006 10:20 am    Post subject: Examples Reply with quote

Check this page out - near the bottom they have an example similar to yours.

http://www.tldp.org/LDP/lpg/node11.html
Back to top
View user's profile Send private message Visit poster's website
smartgirl



Joined: Fri Apr 28, 2006 9:35 am
Posts: 4

PostPosted: Fri Apr 28, 2006 11:59 am    Post subject: RE: Examples Reply with quote

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?

Thanx anyway for help and kind response
Back to top
View user's profile Send private message
smartgirl



Joined: Fri Apr 28, 2006 9:35 am
Posts: 4

PostPosted: Fri Apr 28, 2006 12:00 pm    Post subject: RE: Examples Reply with quote

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?

Thanx anyway for help and kind response
Back to top
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Fri Apr 28, 2006 8:30 pm    Post subject: RE: Examples Reply with quote

I put an extra print statement in the parent part to print out nb after each read from the pipe.
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).

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.

Perhaps you should try reading a single character at a time, building up word until you get a space or return ?
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Fri Apr 28, 2006 9:04 pm    Post subject: RE: Examples Reply with quote

DOH! - I've just spotted what's really wrong here.

OK, first problem - you're writing too long a string to the pipe from the child process. Your line is
Code:
write(fd[1],token,(strlen(token)+1));


This means you are also writing the trailing NULL at the end of each "token".

Next problem - your parent process is waiting until the child has exited before it starts reading from the pipe.
So as you are asking for sizeof(word) characters, it is reading the entire contents of the pipe at once.
BUT, because you have put the trailing NULLs after each token, you only get to see "I" in word.

You can cure the first problem by using this statement to write to the pipe
Code:
write(fd[1],token,strlen(token));

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.
_________________
Hope this helps,

Nigel.
Back to top
View user's profile Send private message
smartgirl



Joined: Fri Apr 28, 2006 9:35 am
Posts: 4

PostPosted: Sun Apr 30, 2006 4:29 pm    Post subject: RE: Examples Reply with quote

thanx for your reply

the problem occur while reading

we can avoid this problem by reading from pipe using while loop
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast