Main Menu

friends

Latest articles

Upgrade your Fedora System to a new major release with preupgrade
08/03/2011 | mad mad mod

If you plan to upgrade your Fedora system to the latest release, you should have a look at PreUpgrade. It downloads all the necessary packages required to upgrade to a newer release of Fedora.I upgrad [ ... ]


No Sound from speaker or headphones on my HTC Desire HD with Android OS
03/03/2011 | mad mad mod

I have a HTC Desire HD mobile phone and recently I stopped getting sound out of the speakers or headphones. I've checked everything and the only thing that fixed the problem was removing the battery f [ ... ]


Other Articles
Facebook MySpace Twitter Digg Delicious Stumbleupon Google Bookmarks 

Designed by:
SiteGround web hosting Joomla Templates
using lockfiles in shell scripts (BASH) E-mail
programming

using lockfiles in shell scripts (BASH)

 

If you want to be 100% sure that only 1 process accesses a resource at the same time, you can use the command "lockfile". Lockfile creates a semaphore file which you have to remove at the end with "rm -f". Lockfile waits 8 seconds and then retries if the file already exists.

 

example (without lockfile):

#!/bin/bash

if [ -e number.txt ]; then
echo "File exists"
else
echo "File does not exists. Create file now."
echo 1 > number.txt
fi

a=$(/usr/bin/tail -n 1 number.txt)

if [ $a -gt 1 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a - 1 >> number.txt
fi

if [ $a -lt 2 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a + 1 >> number.txt
fi

 

The file "number.txt" should only contain the numbers 1 and 2. But, if you now run the script 1000 times in parallel

# for a in {1..1000}; do ./script.sh & done

You will end up with something like:

# cat number.txt |sort -n|uniq
-1
0
1
2
3
4

 

Some people might be tempted to do something like a hand-made lockfile:

#!/bin/bash

if [ -e number.txt ]; then
echo "File exists"
else
echo "File does not exists. Create file now."
echo 1 > number.txt
fi
lock=0;
if [ -e script.lock ];then
lock=1
fi

while [ $lock -eq 1 ]
do
if [ -e script.lock ];then
lock=1
else
lock=0
fi
done

touch script.lock


a=$(/usr/bin/tail -n 1 number.txt)

if [ $a -gt 1 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a - 1 >> number.txt
fi

if [ $a -lt 2 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a + 1 >> number.txt
fi

rm -f script.lock

 

In our example this does not really work. Because if there is a context switch between the test (if [ -e script.lock ]) and the actual lock (touch script.lock) you will still end up with a mess in number.txt.

 

solution with lockfile:

  

#!/bin/bash

if [ -e number.txt ]; then
echo "File exists"
else
echo "File does not exists. Create file now."
echo 1 > number.txt
fi


lockfile script.lock

a=$(/usr/bin/tail -n 1 number.txt)

if [ $a -gt 1 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a - 1 >> number.txt
fi

if [ $a -lt 2 ]; then
a=$(/usr/bin/tail -n 1 number.txt)
/usr/bin/expr $a + 1 >> number.txt
fi

rm -f script.lock

 

pros:

  • 100% sure that no more than one script is allowed to access the file
  • less code
Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."

 
mad mad mod, Powered by Joomla! and designed by SiteGround web hosting