/* This example creates a pipe, forks a child then sends 42 down the pipe. It uses non-ansi C function fdopen to allow stream operations on the pipe. */ #include #include void parent(int fd[]) { FILE* out; close(fd[0]); /* won't be reading this end.*/ printf("Sending 42\n"); out=fdopen(fd[1],"w"); fprintf(out,"%d",42); fclose(out); } void child(int fd[]) { FILE* pipein; int num; close(fd[1]); /* won't be writing.*/ sleep(1); pipein=fdopen(fd[0],"r"); fscanf(pipein,"%d",&num); printf("Read value %d\n",num); fclose(pipein); } int main(int argc, char** argv) { int fd[2]; pid_t pid; if (pipe(fd)!=0) { perror("Problem with pipe"); } pid=fork(); if (pid==0) { parent(fd); } else if (pid>0) { child(fd); } else { // fork problem } return 0; }