Last active
January 22, 2021 22:00
-
-
Save AquariusPower/c5fe6fcc55f1c55753176c05f9a15ca4 to your computer and use it in GitHub Desktop.
stress test flock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
LC_NUMERIC=en_US.UTF-8 | |
# HELPERS: | |
#trash /tmp/flock.tst.log;for((i=0;i<20;i++));do flock.tst.sh&:;done #run this on shell | |
#trash /tmp/flock.tst.log;iSpawns=20;while true;do for((i=0;i<$iSpawns;i++));do flock.tst.sh&:;done; sleep $((60*(iSpawns+2)));done #run this on shell to make the test run forever (untill killed), remove the outer loop to prevent endless test. | |
#pkill -fe flock.tst.sh #end all concurrent children trying to acquire the lock | |
#while true;do date;lslocks |grep flock;sleep 1;done #use this to check if there is more than one lock acquired, check also the log file to confirm it, and if there is two subsequent WORK on the terminal log, it means a problem happened too | |
: ${bDaemonizing:=false} | |
: ${bReport:=false} #help use this to show more log, but will be harder to read it. | |
: ${bCheck:=false} #help this will let the script check if there is more than one instance working, but it may be slow and make it more difficult to let the problem happen | |
if ! $bDaemonizing;then | |
echo "this IS a daemon script, only one instace runnable" | |
flSelf="`realpath $0`" | |
#set -x | |
while ! bDaemonizing=true flock --timeout=$(bc <<< "0.2+0.0$RANDOM") "$flSelf" "$flSelf" "$@";do | |
if $bCheck;then | |
strParents="$(nice -n 19 pgrep -f "^flock --timeout=.* $flSelf $flSelf" |tr '\n' ',' |sed -r 's"(.*),"\1"')" | |
if [[ -n "$strParents" ]];then | |
anDaemonPid=( $(nice -n 19 pgrep --parent "$strParents" -f "$flSelf") ) | |
if((${#anDaemonPid[*]}>1));then echo "ERROR: more than one daemon, flock failed!? :(";ps --no-headers -o ppid,pid,cmd -p "${anDaemonPid[@]}";fi | |
if $bReport && ((${#anDaemonPid[*]}==1));then echo "$$:Wait daemon stop running or 'kill ${anDaemonPid[0]}'";fi #could be: "already running, exiting.", but the new instance may have updated parameters that work as intended now. | |
fi | |
fi | |
done | |
exit #returns w/e flock does | |
fi | |
echo "$$:work:`date`" | |
for((i=0;i<10;i++));do | |
echo "`\ | |
echo $i;\ | |
date;\ | |
ps --no-headers -o ppid,pid,stat,state,pcpu,rss,cmd -p "$PPID";\ | |
ps --no-headers -o ppid,pid,stat,state,pcpu,rss,cmd -p "$$";\ | |
`" >>/tmp/flock.tst.log | |
sleep 1 | |
done | |
echo "$$:DONE:`date`" | |
Just do not hit Ctrl+s on the script that is also the locked file. It will create a new inode and release the lock...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my notebook (ubuntu 20.04), the problem wont happen!
On my desktop (ubuntu 20.04), it may happen randomly.
After I run it for a few minutes, flock may fail.
It will allow more than one execution, preventing the exclusive/unique lock to work as intended.
I am starting to think there is a memory or other hardware problem on my desktop...
I think the way I coded it is not wrong, can someone confirm that?