Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

how to use semaphores?

by Ras (Acolyte)
on Mar 18, 2002 at 21:55 UTC ( #152591=perlquestion: print w/replies, xml ) Need Help??

Ras has asked for the wisdom of the Perl Monks concerning the following question:

how do you use semaphores? Can you give a quick example
on how to use it? thanks

Title edit by tye as single-word titles complicate future simple searches

Replies are listed 'Best First'.
Re: semaphores
by perrin (Chancellor) on Mar 18, 2002 at 22:21 UTC
(Ovid) Re: semaphores
by Ovid (Cardinal) on Mar 18, 2002 at 22:08 UTC

    Acting on the assumption that you want to use semaphore files to lock and unlock files with Perl:

    use Fcntl 'LOCK_EX'; =head2 C<sem_lock> Pass C<sem_lock> a file name and will will add a .sem extension, open +the semaphore (creating if necessary), and get an exclusive lock on the fi +le. The function returns a file handle that should be passed to sem_unlock() t +o unlock the file. If the program terminates, the file will be unlocked automa +tically. =cut sub sem_lock { my $sem_file = shift || die "No file specified"; $sem_file .= '.sem'; open my $fh, ">", $sem_file or die "Cannot open semaphore $sem_fil +e: $!"; flock $fh, LOCK_EX or die "Cannot get exclusive lock on $ +sem_file: $!"; return $fh; } =head2 C<sem_unlock> Pass C<sem_unlock> a file handle and it will close it. It returns tru +e or false depending on success or failure. It doesn't I<unlock> the file as thi +s happens automatically when a file is closed. This function is primarily just +to complement C<sem_lock>. =cut sub sem_unlock { my $fh = shift; close $fh or return 0; return 1; }

    Basically, with a given filename, sem_lock the filename and then use the file as normal. After you are done, sem_unlock the filehandle returned by sem_lock. There are variants of this you can use (such as creating a semaphore locking object and calling an unlock method).


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: how to use semaphores?
by mpeppler (Vicar) on Mar 19, 2002 at 01:33 UTC
    If, on the other hand, you are talking about System V semaphores, then there is a nice IPC::Semaphore module available (I think it's even in the core).

    And then reading up on semaphores in a good Unix programming book (Steven's Unix Network Progamming is my favorite) is probably a good idea.


Re: semaphores
by AidanLee (Chaplain) on Mar 18, 2002 at 22:08 UTC
    You would do better looking up references for Unix IPC (inter-process communications) than asking a Perl community, knowledgeable though it may be.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://152591]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-06-25 11:33 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (82 votes). Check out past polls.