/* * Bench program to exercice multi threads using open()/close()/read()/lseek() calls. * Usage : * bench [-t XX] [-l len] * XX : number of threads * len : bench time in seconds * -s : small fdset : try to use embedded sruct fdtable */ #include #include #include #include #include pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; char *file = "/etc/passwd"; int sflag; /* small fdset */ int end_prog; unsigned long work_done; void *perform_work(void *arg) { int fd, i; unsigned long units = 0; char c; /* force this program to open more than 64 fds */ if (!sflag) for (i = 0 ; i < 64 ; i++) open("/dev/null", O_RDONLY); while (!end_prog) { fd = open(file, O_RDONLY); read(fd, &c, 1); lseek(fd, 10, SEEK_SET); read(fd, &c, 1); lseek(fd, 20, SEEK_SET); read(fd, &c, 1); lseek(fd, 30, SEEK_SET); read(fd, &c, 1); lseek(fd, 40, SEEK_SET); read(fd, &c, 1); close(fd); units++; } pthread_mutex_lock(&mut); work_done += units; pthread_mutex_unlock(&mut); return 0; } void usage(int code) { fprintf(stderr, "Usage : bench [-s] [-t threads] [-l duration]\n"); exit(code); } int main(int argc, char *argv[]) { int i, c; int nbthreads = 2; unsigned int length = 10; pthread_t *tid; while ((c = getopt(argc, argv, "st:l:")) != -1) { if (c == 't') nbthreads = atoi(optarg); else if (c == 'l') length = atoi(optarg); else if (c == 's') sflag = 1; else usage(1); } tid = malloc(nbthreads*sizeof(pthread_t)); for (i = 0 ; i < nbthreads; i++) pthread_create(tid + i, NULL, perform_work, NULL); sleep(length); end_prog = 1; for (i = 0 ; i < nbthreads; i++) pthread_join(tid[i], NULL); pthread_mutex_lock(&mut); printf("%d threads, %u seconds, work_done=%lu\n", nbthreads, length, work_done); pthread_mutex_unlock(&mut); return 0; }