/* Small locking test program. Run 2 or more copies on the client * in an NFS mounted directory. * Invoke as "mlock N" to test with N files. I tried both N = 2 * and N = 20 (which produces more conflicts, but the logs are * also harder to track). --okir */ #include #include #include #include #include #include #define MAXFILES 255 static int fds[MAXFILES]; static void lockit(int num, int len, int unlock) { if (lockf(fds[num], unlock? F_ULOCK : F_LOCK, len) < 0) { fprintf(stderr, "Failed to %slock tfile%03d: %s\n", unlock? "un" : "", num, strerror(errno)); exit(1); } } int main(int argc, char **argv) { int nfiles = 0, counter, rounds = 0; if (argc >= 2) nfiles = atoi(argv[1]); if (nfiles <= 0 || nfiles > MAXFILES) nfiles = MAXFILES; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); printf("Opening %d files... ", nfiles); for ( counter = 0; counter < nfiles; counter++ ) { char filename[64]; sprintf( filename, "tfile%03d", counter ); fds[counter] = open(filename, O_CREAT | O_RDWR, 0644); if (fds[counter] < 0) { perror(filename); return 1; } } printf("done.\n"); while (1) { printf("\r%d", rounds++); for (counter = 0; counter < nfiles; counter++) lockit(counter, 0, 0); /* Change 0 to 1 in the lockit call to test for the * lock all/unlock at offset 1 bug */ for (counter = 0; counter < nfiles; counter++) lockit(counter, 0, 1); } exit(0); }