Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************************************************************
- * This program is intended to demonstrate "advisory record locking". *
- * This program provides only related functions without existence of *
- * main(). So it doesn't compile, unless explicit -c option is added to *
- * gcc or clang. *
- * *
- * Imagine, what if two processes or more trying to access the same file? *
- * There must be a criteria, which process will take precedence. *
- * Many processes can read a file simultaneously. But only one process can *
- * write at once. We call this advisory record locking, which prevents the *
- * other processes to gain access to the file. *
- * For simplicity, follow this scenario. *
- * 1 Only one process can lock a region of a file for "write" access at one *
- * time. The other processes trying to gain read or write access will be *
- * pending. Until the process currently locking the file, releases the *
- * lock. *
- * 2 Many processes can lock a region of a file for "read" access. *
- * The other processes trying to gain read access will be also allowed. *
- * As many processes can read the same file at the same time. *
- * But the processes trying to gain write access will be pending. *
- * Until all the processes currently locking the file release the lock. *
- ****************************************************************************/
- #include <fcntl.h> /* fcntl(2) */
- #include <stdio.h>
- #include <stdlib.h>
- /* This function is intended to lock the region of the file. */
- int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
- {
- /* fd is an integer file descriptor *
- * cmd can be: *
- * F_GETLK: get lock status *
- * F_SETLK: set read lock *
- * F_SETLKW: set write lock *
- * *
- * type can be: *
- * F_RDLOCK: set read lock *
- * F_WRLOCK: set write lock *
- * F_UNLOCK: unlock *
- * *
- * offset is the offset from whence *
- * *
- * whence can be: *
- * SEEK_SET: at the beginning of the file *
- * SEEK_CUR: at the current file position *
- * SEEK_END: at the last byte of the file *
- * *
- * len is the lenge of the lock being *
- * applied. 0 means locks to EOF. */
- struct flock lock;
- lock.l_type = type;
- lock.l_start = offset;
- lock.l_whence = whence;
- lock.l_len = len;
- return fcntl(fd, cmd, &lock); /* set the lock */
- }
- /* This function is intended to test if there is a lock on the file. *
- * If so, process ID (pid) currently locking the file will be returned. *
- * Actually this function is subset of lock_reg() above but with *
- * cmd being F_GETLK. */
- pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
- {
- struct flock lock;
- lock.l_type = type;
- lock.l_start = offset;
- lock.l_whence = whence;
- lock.l_len = len;
- if (fcntl(fd, F_GETLK, &lock) == -1) { /* get the lock status */
- perror("fcntl");
- exit(EXIT_FAILURE);
- }
- if (lock.l_type == F_UNLCK)
- return 0; /* Region is not locked by another process, return 0. */
- return lock.l_pid; /* Else return pid of lock owner. */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement