pipes

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

pipes

Postby smartgirl » Fri Apr 28, 2006 8:38 am

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: Select all
 
#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?
smartgirl
 
Posts: 4
Joined: Fri Apr 28, 2006 8:35 am

Examples

Postby bluemonki » Fri Apr 28, 2006 9:20 am

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

http://www.tldp.org/LDP/lpg/node11.html
User avatar
bluemonki
 
Posts: 11
Joined: Sat Oct 29, 2005 8:18 pm
Location: uk

RE: Examples

Postby smartgirl » Fri Apr 28, 2006 10:59 am

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
smartgirl
 
Posts: 4
Joined: Fri Apr 28, 2006 8:35 am

RE: Examples

Postby smartgirl » Fri Apr 28, 2006 11:00 am

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
smartgirl
 
Posts: 4
Joined: Fri Apr 28, 2006 8:35 am

RE: Examples

Postby Nigel » Fri Apr 28, 2006 7:30 pm

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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Examples

Postby Nigel » Fri Apr 28, 2006 8:04 pm

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: Select all
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: Select all
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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: Examples

Postby smartgirl » Sun Apr 30, 2006 3:29 pm

thanx for your reply

the problem occur while reading

we can avoid this problem by reading from pipe using while loop
smartgirl
 
Posts: 4
Joined: Fri Apr 28, 2006 8:35 am


Return to Programming

Who is online

Users browsing this forum: Exabot [Bot] and 0 guests