#include #include #include #include #include #include #include #define BLKSIZ 16384 #define NBLK (100*(1<<20)/BLKSIZ) int main() { int fd[2]; pid_t pid; int nonblock = 1; pipe(fd); if (!(pid = fork ())) { int i; char buf[BLKSIZ] = {0,}; close(fd[0]); if (nonblock) { int flags; flags = fcntl(fd[1], F_GETFL); flags |= O_NONBLOCK; fcntl(fd[1], F_SETFL, flags); } fd_set wfds; for (i=0; i 0 && FD_ISSET(fd[1], &wfds)) { ssize_t len = write(fd[1], buf, sizeof(buf)); if (len <= 0 && errno == EAGAIN) printf("w", i); if (len <= 0 && errno == EAGAIN) i --; } } close(fd[1]); } else { int i; char buf[BLKSIZ] = {0,}; int total = 0; fd_set rfds; struct timespec tv0, tv1; double elasped; close(fd[1]); if (nonblock) { int flags; flags = fcntl(fd[0], F_GETFL); flags |= O_NONBLOCK; fcntl(fd[0], F_SETFL, flags); } usleep(100000); /* Delay to start reader */ clock_gettime(CLOCK_MONOTONIC, &tv0); for (i=0; i 0 && FD_ISSET(fd[0], &rfds)) { ssize_t len = read(fd[0], buf, sizeof(buf)); if (len <= 0 && errno == EAGAIN) printf("r"); if (len <= 0 && errno == EAGAIN) i --; else if(len < 0) break; else total += len; } } clock_gettime(CLOCK_MONOTONIC, &tv1); close(fd[0]); elasped = (tv1.tv_sec - tv0.tv_sec) + (tv1.tv_nsec - tv0.tv_nsec)*1e-9; printf("Total: %dMB in %f second, %fMB/s\n", total/(1<<20), elasped, total/(1<<20)/elasped); waitpid(pid, NULL, 0); } return 0; }