#include #include #include #include #include #include #include #include #include #include #define DATA_LEN 65536 static unsigned char data[DATA_LEN]; #define SECONDS 10 static atomic_bool done = false; static void *job(void *np) { unsigned long iter = 0; int n = (int)np; char fn[64]; sprintf(fn, "md-%d", n); mkdir(fn, 0755); strcat(fn, "/file.txt"); while (!done) { int h = creat(fn, 0644); if (h < 0) perror("creat"), exit(1); if (write(h, data, DATA_LEN) != DATA_LEN) perror("write"), exit(1); if (close(h)) perror("close"), exit(1); if (unlink(fn)) perror("unlink"), exit(1); iter++; } sprintf(fn, "md-%d", n); if (rmdir(fn)) perror("rmdir"), exit(1); return (void *)iter; } int main(int argc, char *argv[]) { int jobs, i; pthread_t *pths; unsigned long total; if (argc != 3) fprintf(stderr, "args\n"), exit(1); if (chdir(argv[1])) perror("chdir"), exit(1); jobs = atoi(argv[2]); pths = alloca(jobs * sizeof(pthread_t)); for (i = 0; i < jobs; i++) { int e = pthread_create(&pths[i], NULL, job, (void *)i); if (e) errno = e, perror("pthread_create"), exit(1); } sleep(SECONDS); done = true; total = 0; for (i = 0; i < jobs; i++) { void *iter; int e = pthread_join(pths[i], &iter); if (e) errno = e, perror("pthread_join"), exit(1); total += (unsigned long)iter; } printf("%lu\n", total / SECONDS); return 0; }