Karim Vaes

Why chop at leaves, when one must dig at roots
  • Home
  • About me
    • Curriculum Vitae
  • Scripts
    • OWA Most Popular
  • Search
  • License
  • Contact

bash : locking issues – example with setlock

Something that kept me busy for a while. An internal (bash) framework sometimes (Isn’t that great! -sigh-) gave unexpected results. Sometimes the timings were too long, and sometimes data was missing. In the end, it was due to a locking issue. A program tried to execute something while forcing an internal lock. Yet the program was waiting until the lock was released. But when in some cases when two programs do this, this causes a deadlock.

The code below is a way to get around this deadlock. It’s an example using “setlock”, yet it shows the concept. Instead of waiting for the program to release the lock (default behaviour with “setlock”), you can script a small loop that comes back after a few seconds. Yet you have to setup the locking mechanism in a manner that it won’t wait for the lock (option -n for “setlock”).

SUME="bash -c "
SETLOCK="path-to-setlock"
EXECUTE="your-script"
PARAMETERS="your-parameters"
ID="your-unique-id-for-locking"
# usage of a control structure with non-wait locking
# this is done to avoid locking issues ^^
var0=0
LIMIT=3
SNOOZE=5
while [ "$var0" -lt "$LIMIT" ]
do
  var0=`expr $var0 + 1`
  eval $SUME \"$SETLOCK -n $TMPDIR/lock_$ID $EXECUTE $PARAMETERS 2>/dev/null \"  2>/dev/null
  EC=$?
  if [ $EC -eq 0 ] ; then
     var0=`expr $LIMIT + 1`
  else
     sleep $SNOOZE
  fi
done
exit $EC
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Slashdot
  • LinkedIn
  • RSS
  • StumbleUpon
  • Twitter
Categories
Bash, Unix/Linux
Comments rss
Comments rss
Trackback
Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

« “hsperfdata_”-files in /tmp (or $TMPDIR) – jstat my own setlock source code »

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Posts

  • Treating the root cause to cancer
  • Is “entrepreneur” a bad word?
  • Where physics meets marketing
  • Where USSR meets IT?
  • Accepting our limitations, is giving us the opportunity to overcome them!

Similar Posts

  • my own setlock source code
  • bash getopts
  • My own setlock program written in oldskool C
  • WordPress widget : Most popular posts
  • The octaldump of the unix2dos / dos2unix

Recent Comments

  • Karim Vaes on Accept every offer!
  • Danny on The teachings of Budo in Business
  • Pascal on The Service Catalog
  • Shubert on WordPress widget : Most popular posts
  • Dermana Agrı Kesici Krem on OWA Most Popular

Commercial

Categories

2.0 Ads Agile Bash Blogroll Book Brain Business Career Change CIO Collaboration Communication Corner Creative CRM Culture Desktop Development Dreambox Drupal Education Entrepreneur F5 Firefox Food Freelance Fun General Green Growth High Availability Human Resources Idea Infrastructure Insightful Interesting IT ITIL Java Lesson License Life Malware Management Mind Model Motivation MythTV Network NLP OpenSource Performance PHP PM Presentation Project Proverb Quote Remote Scrum Security SEO Social Spam Storage Stress Tactical Team Tech TED Time Management Tip Tool TV Ubuntu Unix/Linux Vids Vim Virtual VmWare Voip Web Wordpress

Archives

  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
rss Comments rss valid xhtml 1.1 design by jide licensed as Creative Commons Attribution