#include #include #include #include #include #include #include #include #define FIFO_NAME "/tmp/fifo1" #define BLKSIZ 65536 #define NBLK (100*(1<<20)/BLKSIZ) int main(int argc, char *argv[]) { pid_t pid; int nonblock = 0; int skip_select = 0; if (argc >= 2 && (atoi(argv[1]) & 1)) nonblock = 1; if (argc >= 2 && (atoi(argv[1]) & 2)) skip_select = 1; mkfifo("/tmp/fifo1", 0644); if (!(pid = fork ())) { int fifo; char buf[BLKSIZ] = {0,}; int i; fifo = open(FIFO_NAME, O_WRONLY); if (nonblock) { int flags; flags = fcntl(fifo, F_GETFL); flags |= O_NONBLOCK; fcntl(fifo, F_SETFL, flags); } fd_set wfds; for (i=0; i 0 && FD_ISSET(fifo, &wfds)) { errno = 0; ssize_t len = write(fifo, buf + total, sizeof(buf) - total); if (len <= 0 && errno == EAGAIN) printf("w", i); if (len > 0) total += len; } } } close(fifo); } else { int fifo; char buf[BLKSIZ] = {0,}; int total = 0; fd_set rfds; struct timespec tv0, tv1; double elasped; fifo = open(FIFO_NAME, O_RDONLY); if (nonblock) { int flags; flags = fcntl(fifo, F_GETFL); flags |= O_NONBLOCK; fcntl(fifo, F_SETFL, flags); } clock_gettime(CLOCK_MONOTONIC, &tv0); for (;;) { FD_ZERO(&rfds); FD_SET(fifo, &rfds); if (skip_select || select(fifo+1, &rfds, NULL, NULL, NULL) > 0 && FD_ISSET(fifo, &rfds)) { errno = 0; ssize_t len = read(fifo, buf, sizeof(buf)); if (len <= 0 && errno == EAGAIN) printf("r"); else if(len <= 0) break; else total += len; } } clock_gettime(CLOCK_MONOTONIC, &tv1); close(fifo); elasped = (tv1.tv_sec - tv0.tv_sec) + (tv1.tv_nsec - tv0.tv_nsec)*1e-9; printf("\nTotal: %dMB in %f second, %fMB/s\n", total/(1<<20), elasped, total/(1<<20)/elasped); if (total != NBLK*BLKSIZ) { printf("Error: %d/%d (%f)\n", total, NBLK*BLKSIZ, (double)(total-NBLK*BLKSIZ)/BLKSIZ); } waitpid(pid, NULL, 0); unlink(FIFO_NAME); } return 0; }