#include #include #include #include #include #include #include #define BLKSIZ (65536*4) #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 (int wlen=1024; wlen<=BLKSIZ; wlen *= 1.7) { FD_ZERO(&wfds); FD_SET(fd[1], &wfds); if (select(fd[1]+1, NULL, &wfds, NULL, NULL) > 0 && FD_ISSET(fd[1], &wfds)) { ssize_t len = write(fd[1], buf, wlen); printf("%d/%d\n", len, wlen); if (len < 0 && errno == EAGAIN) printf("w", i); if (len < 0 && errno == EAGAIN) i --; } } close(fd[1]); } else { 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(1000000); /* Delay to start reader */ clock_gettime(CLOCK_MONOTONIC, &tv0); for (;;) { FD_ZERO(&rfds); FD_SET(fd[0], &rfds); if (select(fd[0]+1, &rfds, NULL, NULL, NULL) > 0 && FD_ISSET(fd[0], &rfds)) { ssize_t len = read(fd[0], buf, sizeof(buf)); if (len < 0 && errno == EAGAIN) printf("r"); 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: %dKB in %f second, %fKB/s\n", total/(1<<10), elasped, total/(1<<10)/elasped); waitpid(pid, NULL, 0); } return 0; }