pipe


Forum: Programming and Scripting
Topic: pipe
started by: smart girl

Posted by smart girl on April 27 2006,23:32
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 Sample

#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?

Posted by dtf on April 28 2006,03:54
If you don't mind me speculating a little.

I believe the problem is in the

nb=read(fd[0],word,sizeof(word));

You are thinking that you are reading one line incrementing the file pointer then reading a second etc.  But I think you are possibly reading in the entire message and what is in the word buffer is the entire phrase

I<null>want<null>to<null>print<null>this<null>line<null&g
t;twice<null>

and when you do the printf you alway print from the first word "I".

Try dumping the contents of the word buffer character by character to see what is in there after the first read.

Posted by smart girl on April 28 2006,10:49
Quote (dtf @ April 27 2006,23:54)
If you don't mind me speculating a little.

your kind response is greatly appreciated  


Quote


I believe the problem is in the

nb=read(fd[0],word,sizeof(word));

You are thinking that you are reading one line incrementing the file pointer then reading a second etc.  But I think you are possibly reading in the entire message and what is in the word buffer is the entire phrase

I<null>want<null>to<null>print<null>this<null>line<null&a
mp;g
t;twice<null>

and when you do the printf you alway print from the first word "I".



I didn't get what you means with that
Quote

Try dumping the contents of the word buffer character by character to see what is in there after the first read


do you mean I add this code after read?


Code Sample

for (k=0;k<strlen(word);k++)
printf("%c",word[k]);

Posted by dtf on April 28 2006,11:09
Whey you do the read

nb=read(fd[0],word,sizeof(word));

you are read the size of word which is BUFSIZ characters.  I do not know what  BUFSIZ is defined as but let say it is 1024.  The firts read (when i = 0) will read in all 1024 characters and not just the first line you wrote.  So after the first read your word buffer contains the entire phrase.  It could be that subsequent reads just fail because you are already at the end of the pipe.

Try checking the return rb to see how many characters are read as a test.  Like

printf("%d\n", rb);

This way you can tell how many characters are being read with each itteration of the for loop.

Yes I was suggesting adding code not fix you problem to debug it.  Once you understand the issue and have fixed it
you can remove the debug statements.  The code sample of dumping the buffer would work.

Posted by dtf on April 28 2006,11:15
Sorry but sometimes I drop letters and words when typing.  Let me try this sentence again.

Yes I was suggesting adding code not to fix your problem but to debug it.

and

The code sample for dumping the buffer will work.

Posted by dtf on April 28 2006,13:10
This will work.

Code Sample

dtf@eliza:~/myprograms/test1/src> cat pipes.c
#include<stdio.h>
#include<string.h>


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

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]);

nb=read(fd[0],word,sizeof(word));

bufptr = word;

for( i=0;i<7;i++){

printf("%s\n",bufptr);

bufptr = bufptr + strlen(bufptr);
bufptr++;


}

}

return 0;

}


Does this make sense?

Below is the complied output

dtf@eliza:~/myprograms/test1/src> gcc -o pipes pipes.c
dtf@eliza:~/myprograms/test1/src> ./pipes
from child:
I
want
to
print
this
line
twice
from parent:
I
want
to
print
this
line
twice
dtf@eliza:~/myprograms/test1/src>

The only reason I changed BUFSIZ to 128 was so I could see what I was working with. You can leave your code as is.

Posted by smart girl on April 28 2006,13:31
yes as you said
when I do dummping I get the full line

thanx alot :)

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.