/* fpipe.c * * Parent writing to child * */ #include #include #include #include #include #define MAX_LINE 80 int main(int argc, char **argv) { pid_t pid; int stat_loc; int pfds[2]; int ret; char buf[MAX_LINE+1]; const char *testbuf = { "I'm a parent and I'm writing to you" }; if (pipe(pfds) == 0) { pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } if (!pid) { close(pfds[1]); /* child will not be writing anything */ ret = read(pfds[0], buf, MAX_LINE); buf[ret] = 0; printf("Child read: %s\n", buf); } else { close(pfds[0]); /* parent will not be reading anything */ ret = write(pfds[1], testbuf, strlen(testbuf)); ret = waitpid(pid, &stat_loc, WCONTINUED); } } else { perror("pipe"); exit(EXIT_FAILURE); } close(pfds[0]); close(pfds[1]); return 0; }