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; } |