semaphore example needed

J

j

Hi all,

I tried to use IPC::Semaphore but tried for a long time without success.

What I want to do is like this :
I want to limit a perl script to be run by 10 users concurrently only. I
want to create 10 semaphore and each user before runing the script, check if
any one of the semaphore available. If not then it die. If there is
available, it takes one. After the script ended, they release the semaphore.

Please help.

Thanks.

Perseus
 
G

Greg Bacon

: I tried to use IPC::Semaphore but tried for a long time without
: success.
:
: What I want to do is like this :
: I want to limit a perl script to be run by 10 users concurrently only.
: I want to create 10 semaphore and each user before runing the script,
: check if any one of the semaphore available. If not then it die. If
: there is available, it takes one. After the script ended, they release
: the semaphore.

How does this suit you?

% cat try
#! /usr/local/bin/perl

use warnings;
use strict;

use IPC::SysV qw/ S_IRWXU IPC_CREAT IPC_EXCL IPC_NOWAIT SEM_UNDO /;
use IPC::Semaphore;

sub create_or_get_sem {
my $key = shift;
my $max = shift;

my $sem;

# first we try to create it
$sem = IPC::Semaphore->new($key, 1, 0666 | IPC_CREAT | IPC_EXCL);
if ($sem) {
$sem->setall($max);
}
else {
$sem = IPC::Semaphore->new($key, 1, 0666 | IPC_CREAT);

die "$0: semget: $!" unless $sem;
}

$sem;
}

sub take {
my $sem = shift;
$sem->op(0, -1, SEM_UNDO | IPC_NOWAIT);
}

## main
my $key = 1234; # maybe generate with IPC::SysV::ftok
my $max = 3;

my $sem = create_or_get_sem $key, $max;

die "$0: too many processes; exiting" unless take $sem;

print "$0 [$$]: sleeping...\n";
sleep 10;
print "$0 [$$]: exiting...\n";
% ./try & ; ./try & ; ./try & ; ./try & ; ./try
[1] 998895
[2] 995414
[3] 954410
[4] 982517
./try [998895]: sleeping...
./try [995414]: sleeping...
./try [982517]: sleeping...
./try [1005583]: too many processes; exiting at ./try line 40.
./try [954410]: too many processes; exiting at ./try line 40.
[3] Exit 35 ./try
% ./try [998895]: exiting...
./try [995414]: exiting...
./try [982517]: exiting...

[4] Done ./try
[2] - Done ./try
[1] + Done ./try
%

Hope this helps,
Greg
 
J

j

Thanks for your example but I don't quite understand some points :

1) If more than 1 person is trying to change the semaphore, are they
contending for the same lock to update the semaphore? If yes, will this
become the bottleneck of the script ? Or is there anyway like preparing $max
number of locks which client can wait for either one to reduce contention?

2) It just exit and then it can release the lock ? Or is there ways I could
release the lock in the middle ?

Thanks .

Perseus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,121
Messages
2,570,712
Members
47,283
Latest member
hopkins1988

Latest Threads

Top