#include #include #include #include #include #include #include #include #define nthreads 32 #define pagecount 16384 #define nrounds 10000 #define nchunks 10 #define PAGE_SIZE 4096 int fd; unsigned long v; void *threadf(void *ptr) { unsigned long *addr; int i, j; for (j = 0; j < nrounds; j++) { for (i = 0; i < nchunks; i++) { addr = mmap(NULL, PAGE_SIZE * pagecount, PROT_READ, MAP_SHARED, fd, i * PAGE_SIZE); if (addr == MAP_FAILED) { fprintf(stderr, "mmap\n"); exit(1); } v += *addr; munmap(addr, PAGE_SIZE * pagecount); } } } int main(int argc, char *argv[]) { pthread_t thr[nthreads]; int i; if (argc < 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } fd = open(argv[1], O_RDONLY); if (fd < 0) { fprintf(stderr, "open\n"); exit(1); } for (i = 0; i < nthreads; i++) { if(pthread_create(&thr[i], NULL, threadf, NULL)) { fprintf(stderr, "pthread_create\n"); exit(1); } } for (i = 0; i < nthreads; i++) { if(pthread_join(thr[i], NULL)) { fprintf(stderr, "pthread_join\n"); exit(1); } } return 0; }