#include #include #include /** flockdemo.c - shows how to lock an entire file **/ /** usage: flockdemo filename {shared|exclusive|nonblock} **/ main(int ac, char *av[]) { int rv; int fd; int operation; if ( ac != 3 ){ fprintf(stderr,"usage: flockdemo file {s|e|n}\n"); exit(1); } if ( *av[2] == 's' ) operation = LOCK_SH; else if ( *av[2] == 'e' ) operation = LOCK_EX; else if ( *av[2] == 'n' ) operation = LOCK_NB; if ( ( fd = open(av[1],O_RDWR)) == -1 ) perror(av[1]); else { rv = flock(fd, operation); printf("flock returned %d. Press return..", rv); getchar(); } }