Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using semaphores 2

Status
Not open for further replies.

esserc

Technical User
Sep 25, 2001
54
US
I haven't been able to find any good examples of using semaphores to handle locking.

I have a script that can be used by multiple users at once. Certain procedures should only be run be one user at a time (seralized, more or less). What I would like to do is use semaphores to communicate between instances of the script and block operations if annother instance holds a lock.

I know I could do this with a lock file, but I think using semaphores would be a cleaner and faster method.

Does anyone have a good example of this?

BTW: I'm using Solaris, so I do support System V IPC.
 
My experience was that semaphore logic is/was not handled the same way in all platforms. What I did was use file 'flock'ing to a zero length file. I coded the cgi programs to flock the file. If the file was already locked the cgi would wait until it's turn came and it could lock it. Of couse you've got to remember to unflock it when done to make it available to the next in line.
Also, using an eval block you can set a timeout value so the flocks won't wait forever.


Works quite well.
 
Thanks for everyone's help. I ended using IPC::Semaphore. My script is only going to be run on Solaris, and I needed a FAST way of locking procedures, and this worked out a lot better than using files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top