#include #include #include #include #include volatile sig_atomic_t sigpipe_received = 0; void sigpipe_handler(int sig) { sigpipe_received = 1; } enum {PIPE_READ = 0, PIPE_WRITE = 1}; int main() { int pipefds[2]; if (pipe(pipefds) < 0) { perror("pipe"); exit(1); } pid_t child = fork(); if (child < 0) { perror("fork"); exit(1); } if (child == 0) { close(pipefds[PIPE_WRITE]); char buf[32]; ssize_t nbytes = read(pipefds[PIPE_READ], buf, sizeof buf); if (nbytes > 0) { if (write(1, buf, nbytes) < nbytes) exit(1); } exit(0); } close(pipefds[PIPE_READ]); sigignore(SIGPIPE); ssize_t nbytes; do { const char message[] = "Hello!\n"; nbytes = write(pipefds[PIPE_WRITE], message, sizeof message - 1); } while (nbytes > 0); if (errno != EPIPE) perror("write"); close(pipefds[PIPE_WRITE]); wait(0); }