Using fcntl for advisory file locking: An example

Suppose you have the executable
      fcntlother
obtained from the files fcntlother.c, fcntl.c, and fcntl.h with the command
      cc -o fcntlother fcntlother.c fcntl.c
Then you can run it with the command
      fcntlother self i
where self is an integer you use to represent this process and i is a lock on the file mylock (the index of the byte being locked)

If you run on a machine the command

      fcntlother 1 1 &
      fcntlother 2 2 &
and on a second machine sharing the file mylock with the previous machine you run
      fcntlother 3 1 &
      fcntlother 4 2 &
you will see messages on the two systems that indicate that the locks are working properly, i.e. processes 1 and 3 are mutually exclusive as are processes 2 and 4, while processes 1 and 3 can execute concurrently with processes 2 and 4.