/* $Id: fifo_xu.c,v 1.3 2012/08/04 21:06:01 kostik Exp kostik $ */ #include #include #include #include #include #include #include #include #include #include #define FIFO "myfifo" #define BIGDATASIZE (1024 * 1024 * 16) sig_atomic_t got_sigpipe; static void handler(int sig) { got_sigpipe = 1; } static void xerr(const char *fmt, ...) { va_list ap; int err; err = errno; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, ": %s", strerror(err)); exit(1); } int main(void) { char *pbuf, buf[512]; struct sigaction sa; pid_t pid; int errn, fd, ret; if (unlink(FIFO) == -1 && errno != ENOENT) xerr("unlink"); if (mkfifo(FIFO, 0664) == -1) xerr("mkfifo"); pid = fork(); if (pid == 0) { fd = open(FIFO, O_RDONLY); if (fd == -1) xerr("child: can not open %s", FIFO); ret = read(fd, buf, sizeof(buf)); printf("child: read %d bytes\n", ret); close(fd); return (0); } /* parent */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = handler; if (sigaction(SIGPIPE, &sa, NULL) == -1) xerr("sigaction"); fd = open(FIFO, O_WRONLY); if (fd == -1) xerr("parent: can not open %s", FIFO); pbuf = malloc(BIGDATASIZE); got_sigpipe = 0; ret = write(fd, pbuf, BIGDATASIZE); errn = errno; printf("parent: first write return %d sigpipe %d", ret, got_sigpipe); if (ret == -1) printf(" error %d %s", errn, strerror(errn)); putchar('\n'); got_sigpipe = 0; ret = write(fd, pbuf, BIGDATASIZE); errn = errno; printf("parent: second write return %d sigpipe %d", ret, got_sigpipe); if (ret == -1) printf(" error %d %s", errn, strerror(errn)); putchar('\n'); return (0); }