Shell quiz: implement a mutex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell quiz: implement a mutex
# 8  
Old 05-21-2009

Code:
progname=${0##*/}
lockdir=/tmp/$progname.lockdir
if mkdir -m 400 "$lockdir" 2>/dev/null
then
  trap 'rmdir "$lockdir"' EXIT
else
  printf "%s is already running\n" "$progname" >&2
  exit 1
fi
##  script follows
sleep 20

# 9  
Old 05-21-2009
Quote:
Originally Posted by cfajohnson
Code:
if mkdir -m 400 "$lockdir" 2>/dev/null

Yup, this is the standard solution.
Check lock and set lock is an atomic operation.
Simple and working... except of course when a SIGKILL hits.

I am puzzled about -m 400, what is the pourpose of it?
If the directory already exists, the mkdir command would fail regardless of the mode bits.
# 10  
Old 05-21-2009
Quote:
Originally Posted by colemar
Ok, now I got it: the serialization protocol dictates that the pid appearing in line 1 of the interlock file is the one whose process is allowed to go on, while the other processes are inside a wait-and-check loop (well, perhaps it is smarter to just wait $pid1 where pid1 is read from line 1); when the process ends, it removes its pid; then another pid is in line 1... and so on, until the pid queue is empty.
Nah, there are still problems.
To remove its pid from the top, a shell script has to rewrite the whole interlock file. What if in the meantime another instance is appending its pid?
This issue is basically a consequence of the fact that unix provides no automatic exclusive write access to files.

I believe it would go away if we make the following substitutions:
  • interlock file <= directory
  • lines of interlock file <= filenames inside the directory
# 11  
Old 05-21-2009
Quote:
Originally Posted by colemar
Yup, this is the standard solution.
Check lock and set lock is an atomic operation.
Simple and working... except of course when a SIGKILL hits.

Anyone throwing SIGKILLs around should expect to have to do some manual cleanup!
Quote:
I am puzzled about -m 400, what is the pourpose of it?
If the directory already exists, the mkdir command would fail regardless of the mode bits.

It's intended to prevent anyone putting a file in there and preventing rmdir from working. rm -rf "$lockdir" would fix that, too.
# 12  
Old 05-21-2009
Quote:
Originally Posted by cfajohnson
mkdir
Let see if I can add something useful:

Code:
progname=${0##*/}
lockdir=/tmp/$progname.lockdir
if mkdir -m 755 "$lockdir" 2>/dev/null
then
  : > "$lockdir/$$"
  trap 'rm -rf "$lockdir"' EXIT
else
  pid=$(echo "$lockdir"/*);  pid=${pid##*/}
  printf "%s is already running with pid %s\n" "$progname" $pid >&2
  exit 1
fi
##  script follows
sleep 20

And a version that doesn't drop the ball:

Code:
progname=${0##*/}
lockdir=/tmp/$progname.lockdir
trap '[[ -f "$lockdir/$$" ]] && rm -rf "$lockdir"' EXIT
until mkdir -m 755 "$lockdir" 2>/dev/null
do
  pid=$(echo "$lockdir"/*);  pid=${pid##*/}
  printf "%s is already running with pid %s\n" "$progname" $pid >&2
  sleep 1
done
: > "$lockdir/$$"

##  script follows
sleep 20


Last edited by colemar; 05-22-2009 at 04:44 AM.. Reason: A small fix to pid=
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple choice quiz im shell not working

Hi, I have to create a quiz im form of bash-shell-script. The problem is: I don't find a way to bring multiple choice questions to work. As long as someone selects only one of the possible answers everything is fine, but if one selects two or more options it doesn't work. The code I used is... (4 Replies)
Discussion started by: Naky
4 Replies

2. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

3. Shell Programming and Scripting

Need to implement one check in shell script

Hi, I am facing one issue.I am working on one script where I want to implement one check PSS/TFM/Coupon/trunk/CouponProject/CouponService/src/test/java/com/travelport/service/pss/coupon/validator/CouponValidationReqValidatorTest.java Now my problem is or requirement is if the above... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

4. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

5. Programming

Shell programing to implement SQL package

hi all, i have a table with employee details like his name,addr,DOB etc. i need a shell program which takes one date as input from user and print the employees whose DOB is less than that date. in sql package the select query is written (2 Replies)
Discussion started by: vidyaj
2 Replies

6. Shell Programming and Scripting

Select query implement in a shell

I have been asked to create a shell script that accepts a number of SQL select queries as input, runs them in sequence, spools the output to an EXCEL workbook, where, each worksheet is an output of a Select statement run above. The workbook should be in a .XLS format. If a particular select... (2 Replies)
Discussion started by: ShellNovice1
2 Replies

7. Shell Programming and Scripting

Shell quiz: emulate an associative array

Most shells flavors do not have associative arrays a.k.a. maps. How would you emulate an associative array? I had this problem once and found a working solution, but I don't want to spoil the game hence I wont tell it. Wonder if anyone comes up with something better. (5 Replies)
Discussion started by: colemar
5 Replies

8. Shell Programming and Scripting

batch file not running after implement dummy shell

Hi, I want to ask if anyone has any ideas, why the batch file i created to transfer files on my desktop doesn't run after i put this line in /etc/passwd and any idea to overcome it ? Thanks user:!:xxx:xxx::/home/user:/usr/local/bin/ssh-dummy-shell (2 Replies)
Discussion started by: DarReNz
2 Replies

9. Shell Programming and Scripting

mutex in shell programing

A shell in crontab per 5 min write a file B shell in crontab per 6 min read a file how to lock the share file a ;avioid confilict in write and read? Thx : -) (1 Reply)
Discussion started by: zz_xm
1 Replies
Login or Register to Ask a Question