Regarding that thread: bash flock: exit if can't acquire lock
I'll appreciate if someone can explain to me what does the '200' stand for.
I've read about flock and it seems that 200 if to specify a File Descriptor, but what is so good about this number?
The process uses special equipment that electrically charges the flock particles causing them to stand-up. The fibres are then propelled and anchored into the adhesive at right angles to the substrate. The application is both durable and permanent. Flock can be applied to glass, metal, plastic, paper or textiles.
It locks a specified file, which is created (assuming appropriate permissions), if it does not already exist. The second form is conveninent inside shell scripts, and is usually used the following manner: ( flock -s 200.
Locking files with flock. One common way to lock a file on a Linux system is flock . The flock command can be used from the command line or within a shell script to obtain a lock on a file and will create the lock file if it doesn't already exist, assuming the user has the appropriate permissions.
flock(2) it's used to apply advisory locks to open files. it can be used to synchronize access to resources across multiple running processes. While flock(2) does solely act on files (actually, on file handles), the file itself need not be the resource to which access is being controlled.
Theres nothing special about the number 200. It just happens to be the example used in the man page of the flock
command; and it happens to be a large number, so it's unlikely to conflict with the the file descriptor of any other file you open during your script.
In your comment, you ask about:
( flock -e 200 echo "In critical section" sleep 5 ) 200>/tmp/blah.lockfile echo "After critical section"
The parentheses ()
create a subshell; a new process, separate from the parent process. The 200>/tmp/blah.lockfile
causes that process to open up /tmp/blah.lockfile
for writing, on file descriptor 200. The commands inside the parentheses are executed within that shell.
flock -e 200
obtains an exclusive lock on the file pointed to by file descriptor 200. An exclusive lock means that anyone else who tries to obtain a lock on that file, either exclusive or shared, will block (wait) until this lock has been relinquished, or fail if they hit a timeout or asked not to block. So during the remainder of the body of the subshell (the echo
and sleep
commands), the lock will be held by that subshell, and no one else can obtain that lock. Once the subshell finishes, the file will be closed and lock relinquished.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With