IPC and counters

Paul Makepeace paulm at paulm.com
Tue Oct 14 23:39:22 BST 2008


Isn't this exactly what semaphores are for?

http://perldoc.perl.org/IPC/Semaphore.html
(http://en.wikipedia.org/wiki/Semaphores)

I'm sure you know all this so I suspect I've misread your question :-)

P

On Tue, Oct 14, 2008 at 11:21 PM, Simon Wistow <simon at thegestalt.org> wrote:
>
> I have an IPC problem.
>
> I have a parent process with a bunch of forked children.
>
> The parent sits in a loop waiting for messages and occasionally passes
> them onto the children which are also sitting in their own loops
> listening for messages and then doing stuff with them.
>
> Still with me?
>
> Now occasionally, if a Child gets sent a message then it doesn't want
> any of the other Children to do anything.
>
> So what I think is something like this.
>
>    Parent:
>
>    my $n  = scalar(@children);
>    my $id = $message->id;
>    create_ipc_counter("$id-lock", 0);
>    foreach my $child (@children) {
>        $child->send($message);
>    }
>
>    Child:
>
>    my $message = get_message();
>    my $id      = $message->id;
>
>    # If we're the first ...
>    if (inc_ipc_counter("$id-lock")==1) {
>        do_something($message);
>    }
>    # ... otherwise ignore it
>
>    # If we're the last then clean up
>    if (dec_ipc_count("$id-count")==0) {
>        destroy_ipc_counter("$id-count");
>        destroy_ipc_counter("$id-lock");
>    }
>
> Is there some sort of module which will do similar? Or am I smoking huge
> quanities of crack?
>
> Is there another way to do this?
>
> Simon
>
>


More information about the london.pm mailing list