P
P
pqgg7nwkd42018-06-23 20:56:41
linux
pqgg7nwkd4, 2018-06-23 20:56:41

How to organize an atomic lock based on a lock file?

And a few conditional processes with names that want to acquire the lock from time to time: Foo, Bar, ... .
Processes are conditional, so the lock must persist even if the process that acquired it dies and even if the computer is rebooted. Commands must be atomic (or using the equivalent of flock).
It is proposed to organize in the form of a certain lock file (my.lock):
1. If the file is missing or empty, then there is no lock.
2. If the file contains the string Foo, then the lock is held by the Foo process.
Actually questions:
1. What team of bash to carry out capture of blocking?
The command should return: 0 if the lock has already been acquired by this process before or just now; 1 if the lock is held by someone else.
2. What is the bash command to release the lock?
The command should return: 0 if the lock was acquired by this process and released; 1 if the lock is held by someone else; 2 if there was no lock.
3. What bash command can a process check if a lock is held or not?
The command should return: 0 if the lock was acquired by this process; 1 if the lock is held by someone else; 2 if there is no lock.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor Taran, 2018-06-25
@shambler81

1. the presence of a command filetest
2. grep and && or ||depending on the required code.
2. exit if or delete
3. ls
In general, everything depends on the size and complexity of the script, I wrote you the easiest way.

D
Denis, 2018-06-24
@notwrite

Use a directory as a lock file (there is a directory - locked) Write the details of the lock to a file in this directory. mkdir grep rmdir commands

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question