#include #include #include #include #include int main(void) { pid_t child = fork(); int pipefd[2]; int ret; struct sigaction sa; ret = pipe(pipefd); if (ret < 0) { perror("pipe"); return 1; } sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_restorer = NULL; ret = sigaction(SIGPIPE, &sa, NULL); if (ret < 0) { perror("sigaction"); return 1; } if (child < 0) { /* error */ perror("fork"); return 1; } else if (child == 0) { /* child */ close(pipefd[0]); dup2(pipefd[1], fileno(stdout)); sleep(2); ret = printf("Hello world\n"); if (ret < 0) { perror("printf"); } fflush(stdout); errno = 0; ret = fflush(stdout); if (ret < 0) { perror("fflush"); } if (ferror(stdout)) { perror("error writing to stdout"); return 1; } return 0; } else { /* parent */ close(pipefd[1]); close(pipefd[0]); waitpid(child, NULL, 0); return 0; } }