forked from dermoth/IPC-Semaphore-Concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.pl
executable file
·45 lines (39 loc) · 1.05 KB
/
example.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
#
# example.pl - Limiting process concurrency using IPC::Semaphore::Concurrency
#
# Author: Thomas Guyot-Sionnest <[email protected]>
#
# This in a perl example of using semaphores to limit concurrent processes
# doing a specific task.
#
# This code is released to the public domain.
#
use strict;
use warnings;
use IPC::Semaphore::Concurrency;
use Errno;
use vars qw($sem_path_name $sem_max);
# Semaphore pathname for key generation (will be created if missing)
$sem_path_name = '/tmp/sem_test_5a76';
# Max concurrency - also used as semaphore proj_id
$sem_max = 4;
my $sem = IPC::Semaphore::Concurrency->new(
path => $sem_path_name,
project => $sem_max,
value => $sem_max,
);
if (@ARGV > 0 && $ARGV[0] eq 'reset') {
$sem->remove();
exit;
}
print "begin val: ".$sem->getval(0)."\n";
if ($sem->acquire(0, 0, 0, 1)) {
print "Do work\n";
sleep 10;
} elsif ($!{EWOULDBLOCK}) {
print "Pass your turn\n";
} else {
die("Unexpected Error: $!");
}
print "end val: ".$sem->getval(0)."\n"; # Value will be re-incremented as soon as the process exits