/* hellofork.c - Saying Hello World with fork */ #include #include #include #include #define MAXLINE 256 int main(void) { int n, fd[2]; pid_t pid; char line[MAXLINE]; if (pipe(fd) < 0) { fprintf(stderr, "pipe error\n"); exit(1);} if ( (pid = fork()) < 0) { fprintf(stderr, "fork error\n"); exit(1);} else if (pid == 0) { /* child */ close(fd[0]); write(fd[1], "hello world\n", 12); } else { /* parent */ close(fd[1]); n = read(fd[0], line, MAXLINE); write(STDOUT_FILENO, line, n);} return (0); }