Advertisement
ptkrisada

advisory record locking

Mar 12th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1. /****************************************************************************
  2.  * This program is intended to demonstrate "advisory record locking".       *
  3.  * This program provides only related functions without existence of        *
  4.  * main(). So it doesn't compile, unless explicit -c option is added to     *
  5.  * gcc or clang.                                                            *
  6.  *                                                                          *
  7.  * Imagine, what if two processes or more trying to access the same file?   *
  8.  * There must be a criteria, which process will take precedence.            *
  9.  * Many processes can read a file simultaneously. But only one process can  *
  10.  * write at once. We call this advisory record locking, which prevents the  *
  11.  * other processes to gain access to the file.                              *
  12.  * For simplicity, follow this scenario.                                    *
  13.  * 1 Only one process can lock a region of a file for "write" access at one *
  14.  *   time. The other processes trying to gain read or write access will be  *
  15.  *   pending. Until the process currently locking the file, releases the    *
  16.  *   lock.                                                                  *
  17.  * 2 Many processes can lock a region of a file for "read" access.          *
  18.  *   The other processes trying to gain read access will be also allowed.   *
  19.  *   As many processes can read the same file at the same time.             *
  20.  *   But the processes trying to gain write access will be pending.         *
  21.  *   Until all the processes currently locking the file release the lock.   *
  22.  ****************************************************************************/
  23.  
  24. #include <fcntl.h>  /* fcntl(2) */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27.  
  28. /* This function is intended to lock the region of the file.    */
  29. int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
  30. {
  31.     /* fd is an integer file descriptor         *
  32.      * cmd can be:                              *
  33.      * F_GETLK: get lock status                 *
  34.      * F_SETLK: set read lock                   *
  35.      * F_SETLKW: set write lock                 *
  36.      *                                          *
  37.      * type can be:                             *
  38.      * F_RDLOCK: set read lock                  *
  39.      * F_WRLOCK: set write lock                 *
  40.      * F_UNLOCK: unlock                         *
  41.      *                                          *
  42.      * offset is the offset from whence         *
  43.      *                                          *
  44.      * whence can be:                           *
  45.      * SEEK_SET: at the beginning of the file   *
  46.      * SEEK_CUR: at the current file position   *
  47.      * SEEK_END: at the last byte of the file   *
  48.      *                                          *
  49.      * len is the lenge of the lock being       *
  50.      * applied. 0 means locks to EOF.           */
  51.     struct flock lock;
  52.  
  53.     lock.l_type = type;
  54.     lock.l_start = offset;
  55.     lock.l_whence = whence;
  56.     lock.l_len = len;
  57.     return fcntl(fd, cmd, &lock);   /* set the lock */
  58. }
  59.  
  60. /* This function is intended to test if there is a lock on the file.    *
  61.  * If so, process ID (pid) currently locking the file will be returned. *
  62.  * Actually this function is subset of lock_reg() above but with        *
  63.  * cmd being F_GETLK.                                                   */
  64. pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
  65. {
  66.     struct flock lock;
  67.  
  68.     lock.l_type = type;
  69.     lock.l_start = offset;
  70.     lock.l_whence = whence;
  71.     lock.l_len = len;
  72.     if (fcntl(fd, F_GETLK, &lock) == -1) {  /* get the lock status */
  73.         perror("fcntl");
  74.         exit(EXIT_FAILURE);
  75.     }
  76.     if (lock.l_type == F_UNLCK)
  77.         return 0;       /* Region is not locked by another process, return 0. */
  78.     return lock.l_pid;  /* Else return pid of lock owner. */
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement