(Video materials in preparation)
ulock : Exclusive access control command
Usage : ulock <lock-file>
ulock -w <lock-file> <counter-file>
ulock -r <lock-file> <counter-file> <command>
Option : --timeout=<sec>
--invalid=<sec>
Version : Mon Jan 17 11:50:14 JST 2022
Edition : 1
Ulock command without option -w/-r creates an absolutely exclusive
zone. In this case, ulock creates <lock-file> exclusively.
Ulock command optiwith on -r/-r creates a read-write lock.
When using -w (write lock), ulock creates <lock-file> exclusively
and waits until the link count on <counter-file> becomes 1.
When using -r (read lock), ulock increment the link count of
<counter-file> by one if <lock-file> does not exist, at which time
<command> is executed and the link count of <counter-file> is
decremented by one after <command> finished successfully. The link
count is not decremented if <command> finished unsuccessfully. In
this case, following message is issued.
Error(282)[ulock] : command '...' failed, file '...' is left.
The --timeout option specifies the maximum time to wait for
<command> to run. If you specify -1 it will wait for ever. Default
is -1 (wait for ever).
The --invalid option deletes the <lock-files> which is created
before more tha specified seconds. Default is 60 seconds.
$ cat lock.sh
#!/bin/bash
if ulock lock; then
#
# Read/Write Operation
#
rm -f lock
fi
$ cat writelock.sh
#!/bin/bash
if ulock -w lock counter; then
# Write Operation (Example)
up3 key=1 master tran > master.new
mv master.new master
rm -f lock
fi
$ cat readlock.sh
#!/bin/bash
ulock -r lock counter cat master
-- Write Lock
X-- Get a write lock (Atomic)
X-- Wait for counter to be zero
--- Write Operation
--- Release write lock
-- Read Lock
X-- Increment counter by 1 (Atomic)
X-- Confirm there is no write lock. If there is, decrement by 1 and return.
X-- Read only operation
X-- Decrement counter by 1 (Atomic)
Write locking in ulock uses the uniqueness of creating a hard link
file, and the counter operation uses the number of links (unique)
of the hard link file. In the above algorithms, ulock performs the
functions marked with X.
If lock-file and counter-file are created over NFS, then the read/write
lock is effective for every NFS client hosts.