| View previous topic :: View next topic |
| Author |
Message |
smartgirl
Joined: Fri Apr 28, 2006 9:35 am Posts: 4
|
Posted: Fri Apr 28, 2006 9:38 am Post subject: pipes |
|
|
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 |
|
 |
bluemonki

Joined: Sat Oct 29, 2005 9:18 pm Posts: 11 Location: uk
|
|
| Back to top |
|
 |
smartgirl
Joined: Fri Apr 28, 2006 9:35 am Posts: 4
|
Posted: Fri Apr 28, 2006 11:59 am Post subject: RE: Examples |
|
|
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 |
|
 |
smartgirl
Joined: Fri Apr 28, 2006 9:35 am Posts: 4
|
Posted: Fri Apr 28, 2006 12:00 pm Post subject: RE: Examples |
|
|
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 |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Fri Apr 28, 2006 8:30 pm Post subject: RE: Examples |
|
|
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 |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Fri Apr 28, 2006 9:04 pm Post subject: RE: Examples |
|
|
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 |
|
 |
smartgirl
Joined: Fri Apr 28, 2006 9:35 am Posts: 4
|
Posted: Sun Apr 30, 2006 4:29 pm Post subject: RE: Examples |
|
|
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 previous topic :: View next topic |
|