in reply to Re: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
in thread Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
If I don't want the processes to block, then would the only changes I needed to make be in
I actually don't want the extraneous ones to block, but to go ahead and exit, because other instances can pick up and process waiting data later-I just don't want to slam the machine with a huge number of processes doing heavy processing at once.
#!/usr/bin/perl -w use strict; use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT SEM_UNDO ftok); use IPC::Semaphore; srand; my $sem = new IPC::Semaphore( ftok( $0, 0 ), 1, S_IRWXU | IPC_CREAT ); my $semval = $sem->getval(0); $semval = 0 unless ( defined($semval) ); exit(0) if ( $semval >= 10 ); $sem->op( 0, 1, SEM_UNDO ); # # 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->op( 0, -1, SEM_UNDO ); } else { $sem->remove; } __END__ # # Sample test execution (in bash): # # index=500 # while [ $index -gt 0 ] # do # perl test.pl & # let "index=index-1" # done #
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re^2: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
by ptkdb (Monk) on Oct 16, 2003 at 12:23 UTC | |
|
Re: Re^2: Effective use of IPC::Semaphore to limit number of concurrent executions of a scxipt
by ptkdb (Monk) on Oct 16, 2003 at 15:03 UTC |