I am looking for a way to limit the number of concurrent executions of a script to some value x, where [1 <= x <= n ]. Limiting to 1 instance appears to be the easy case, but it seems more difficult to limit it to just some n greater than 1.

Because of the manner in which the process may be launched (a filter launched by another process-and no, not email-related), it is conceivable that several thousand instances could be launched in the span of a few minutes; however, I wish to limit this to a much smaller (and processable) number (such as less than 100). Because of the time involved (and other potential issues), I am trying to avoid solutions such as the creation of a large number of lock files (due to speed issues), or ones susceptible to race-conditions (or the like).

One suggestion I encountered suggested the use of IPC::Semaphore as a way, by allowing processes to update a shared semaphore, and exit if the value it contained exceeded my limit. (I believe I understood that correctly-please correct me if I am wrong, however.) I seem to be having difficulties implementing this, although now it seems to be working for the moment. Does it look as if I have this correct, and can anyone suggest a better way, or any issues they may see with my implementation?

#!/usr/bin/perl -w use strict; use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT); use IPC::Semaphore; srand; my $sem = new IPC::Semaphore( 1234, 1, S_IRWXU | IPC_CREAT ); my $semval = $sem->getval(0); $semval = 0 unless ( defined($semval) ); exit(0) if ( $semval >= 10 ); $sem->setval( 0, ( $semval + 1 ) ); # # Do work... or in this case, # print the value of $semval and sleep for a while # print $semval + 1, "\n"; sleep( int( rand(20) + 1 ) ); # # Decrement the semaphore value # $semval = $sem->getval(0); if ($semval) { $sem->setval( 0, ( $semval - 1 ) ); } else { $sem->remove; } __END__ # # Sample test execution (in bash): # # index=30 # while [ $index -gt 0 ] # do # perl test.pl & # sleep 1 # let "index=index-1" # done #

In reply to 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.