Creating another file is more of a problem than anything. Create a directory and check the result of creation. The Unix manual states that only one task can succeed when creating the directory, and the other can fail if the directory already exists, including the case when 2 tasks tried at the same time. The OS itself handles the problem, so you don't need to.
If not for possible locks, this is all you would need to do. However, everything happens, programs are interrupted and do not always remove the lock. So the implementation can be a little more complicated.
In the script, I often used the code below. It automatically processes obsolete locks. You can implement the same in C. Check the man page:
man -s 2 mkdir
EXECUTION_CONTROL_FILE: the name of PATH and Dir, something like / usr / tmp / myAppName
second_of_now: return the current time in seconds (see below)
LOCK_MAX_TIME: how long in seconds a lock can exist before it is considered deprecated.
sleep 5: it is always believed that the castle will do something short and sweet. If not, perhaps your sleep cycle should be longer.
LockFile() { L_DIR=${EXECUTION_CONTROL_FILE}.lock L_DIR2=${EXECUTION_CONTROL_FILE}.lock2 ( L_STATUS=1 L_FILE_COUNT=2 L_COUNT=10 while [ $L_STATUS != 0 ]; do mkdir $L_DIR 2>/dev/null L_STATUS=$? if [ $L_STATUS = 0 ]; then
Use this:
cpu
source share