Algorithm for Advisory Locking between Parent and Child Processes in a UNIX Environment
Original Publication Date: 1995-Feb-01
Included in the Prior Art Database: 2005-Mar-29
Heyman, JM: AUTHOR [+1]
Disclosed is an algorithm for sharing advisory locks between processes. Unique is the combination of fcntl() read locks and environment variables to allow locks within a task to be shared.
Algorithm for Advisory Locking between Parent and Child
in a UNIX Environment
an algorithm for sharing advisory locks between
processes. Unique is the combination of fcntl() read locks and
environment variables to allow locks within a task to be shared.
algorithm provides a method for parent and child processes
to share file locks while preventing other processes from acquiring
the lock. This is useful when a task is made up of several processes
and it is necessary that one task complete before another task is
started. Using this algorithm, a process can reliably acquire the
lock if it is in the same task or be denied the lock if it is not in
the task which holds the lock.
In a UNIX*
environment a single task may be made up of several
processes. A task may be something like configuring a device where
several processes must be executed before the device is configured.
Generally there is a parent process which spawns child processes.
These child process may spawn other child processes, and so on. For
instances where it was necessary for one task to complete before
another is started, a method was needed by which the parent and child
processes could share file locks and at the same time deny other
processes from gaining the lock. This would ensure that the parent
and all its children process were allowed to complete before another
process could get the lock.
One method is
to have the parent process create a file with a
well known name. The task would create the file when it started and
remove the file when it was done. This way other processes could
check for the existence of the file and if it existed, they knew that
some other process had the lock. The difficulty with this method is
that the parent process could die before the lock file was removed
and some external means would be necessary to remove the file.
Another problem is that the child processes executed within the task
can also be started on their own and thus would also have to check
for the existence of the lock file.
method is to use the fcntl() system call to place a
lock on a well known file. The advantage of using fcntl() locks on a
file instead of just checking for file existence is that the system
will automatically remove a process' fcntl() lock on a file if the
process dies. This way external cleanup is not necessary if a
process abnormally ends. However, just using fcntl() locks is not
enough since locks are not passed to the child when a fork() is done.
Basically there are two types of file locks: read locks (F_RDLCK)
and exclusive locks (F_WRLCK). Any number of processes can have read
locks on the same file but only one process can have an exclusive
lock on a file. If read locks were used, any process could get the
lock and no process would be denied. If exclusive locks were used,
only the parent process could get the lock and the child processes,