#include #include #include #include #include #include #include #include char data[] = "Lorem ipsum dolor sit amet"; int main( int argc, char *argv[] ) { char fname[] = "fcntl.test.XXXXXX"; int fd = mkstemp(fname); write(fd, data, sizeof(data)); struct flock lock; lock.l_type = F_RDLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 1; lock.l_pid = getpid(); struct flock unlock; unlock.l_type = F_UNLCK; unlock.l_whence = SEEK_SET; unlock.l_start = 0; unlock.l_len = 1; unlock.l_pid = getpid(); int c = 0; while (c++ < 100000000) { errno = 0; int res = fcntl(fd, F_SETLK, &lock); if (res < 0) { printf("F_SETLK F_RDLCK res = %d (%s)\n", res, strerror(errno)); break; } errno = 0; res = fcntl(fd, F_SETLK, &unlock); if (res < 0) { printf("F_SETLK F_UNLCK res = %d (%s)\n", res, strerror(errno)); break; } } printf("Successful cycles: %d\n", c - 1); close(fd); unlink(fname); }