#!/bin/sh
function lock
{
while ! mkdir $1 2> /dev/null;
do
dd bs=1 count=1 <& 3 > /dev/null 2>/dev/null;
done;
}
function unlock
{
if ! rmdir $1;
then
echo "bad call unlock";
fi;
dd bs=1 count=1 >& 4 < /dev/zero 2>/dev/null;
}
function gotter
{
let use_idx=0;
exec 3<_sleep;
exec 4>_wakeup;
lock _lock;
export use_idx=`cat _refcount`;
while [ $use_idx -lt 1000 ];
do
echo $(($use_idx+1)) > _refcount;
unlock _lock;
echo $1.lock $use_idx;
sleep 1;
lock _lock;
export use_idx=`cat _refcount`;
done;
rmdir $1.lock;
unlock _lock;
exec 4>&-;
exec 3<&-;
echo $1.lock is finish
}
rm -f _lock;
echo 0 > _refcount;
mkfifo _wakeup _sleep;
for i in a b c d e f;
do
mkdir $i.lock;
(gotter $i &);
done;
exec 3>_sleep;
exec 4<_wakeup;
for i in a b c d e f;
do
while [ -d $i.lock ];
do
dd bs=1 count=1 >&3 <&4 2>/dev/null;
done;
done;
exec 4<&-
exec 3<&-
rm _sleep _wakeup;
|