Reference Unix Network Programming Vol 2, Interprocess Communications by W. Richard Stevens.

A semaphore is basically an atomic counter. It guarantees that only one counter operation(increment or decrement) will occur no matter how many processes or threads are trying to operate on it simultaneously. Also, the counter may not be decremented lower than 0. Any attempt to decrement the counter when it is zero will 'block' , unless the IPC_NOWAIT flag is set on your operations, in which case it will fail with an errno set to EAGAIN or EWOULDBLOCK. The process will block until someone increments that semaphore. Once that happens if there is more than one process waiting to decrement it back to zero, only 1 will succeed.

Typically semaphores are used sort of like mutexes to prevent two processes from touching the same piece of shared memory at the same time.

However, your use for them is fine as well. The only thing is that you don't want to be incrementing it using getval setval. You want to use the 'op' method instead. Your code should look something this like this:

use IPC::SysV qw/SEM_UNDO IPC_CREAT ftok/ ; use IPC::Semaphore ; # start script $flags = PERMISSIONS ; $sem = new IPC::Semaphore(ftok($0, 0), 1, $flags) ; unless($sem) { # we must be the first one $sem = new IPC::Semaphore($id, 1, $flags | IPC_CREAT) $sem->setval(0, 100) ; } ## ## Decrement the #0 semaphore by 1 ## $sem->op(0, -1, SEM_UNDO) ; # blocks if semaphore is zero # # DO SCRIPT STUFF # ## ## Increment #0 semaphore by 1 ## $sem->op(0, 1, SEM_UNDO) ; # once past this point, any script waiting can proceed # done
NOTES:
  • ftok generates a unique identifier based on the path that you give it. If the path doesn't exist you'll get a null
  • SEM_UNDO performs a nice service for you, if you're script 'croaks' before the script increments the semaphore, the kernel will 'undo' the collective operations on that semaphore the process performed. Ref Unix Network Programming Vol 2 pgs 174, 286-287
  • Although you probably have already, get familiar with the commands 'ipcs' and 'ipcrm'.
  • Semaphores can also be allocated in blocks of more than one to provide more sophisticated uses. You would you use 'nsems' to specify the number of semaphores you want
  • the op method can have several different operations on the block of semaphores. However in order for 'op' to succeed(i.e. not block) all of them have to occur atomically
  • At the request of the IAEA, I've been asked to explain that atomic in this context DOES NOT refer to any operation of fission, but rather refers to the quality of 1 at a time.
  • Would it be blasphemy for me to request a separate shrine for Mr Stevens?(author of the referenced material)

    In reply to Re: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt by ptkdb
    in thread Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt by atcroft

    Title:
    Use:  <p> text here (a paragraph) </p>
    and:  <code> code here </code>
    to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.