/* Aaron Knister Program to acquire a bunch of byte range locks in a file */ #include #include #include #include #include #include #include int main(int argc, char **argv) { char *filename; int fd; struct stat statBuf; int highRand; int lowRand; unsigned int l_start = 0; unsigned int l_len; int openMode; int lockType; struct flock lock; unsigned int stride; filename = argv[1]; stride = atoi(argv[2]); l_len = atoi(argv[3]); if ( argc > 4 ) { openMode = O_WRONLY; lockType = F_WRLCK; } else { openMode = O_RDONLY; lockType = F_RDLCK; } printf("Opening file '%s' in %s mode. stride = %d. l_len = %d\n", filename, (openMode == O_WRONLY) ? "write" : "read", stride, l_len); assert( (fd = open(filename, openMode)) >= 0 ); assert( fstat(fd, &statBuf) == 0 ); while(1) { if ( l_start >= statBuf.st_size ) { break; l_start = 0; } highRand = rand(); lowRand = rand(); lock.l_type = lockType; lock.l_whence = 0; lock.l_start = l_start; lock.l_len = l_len; if (fcntl(fd, F_SETLKW, &lock) != 0) { fprintf(stderr, "Non-zero return from fcntl. errno = %d (%s)\n", errno, strerror(errno)); abort(); } lock.l_type = F_UNLCK; lock.l_whence = 0; lock.l_start = l_start; lock.l_len = l_len; assert(fcntl(fd, F_SETLKW, &lock) != -1); l_start += stride; } }