I came across the damdest little story in a C/OS book...

Five philosophers sit down to eat rice. There's only 5 chopsticks, so ... really, at most 2 philosophers can eat at a time.

In the C book, we make a typedef to an int * called a semaphore. Then we fork 5 kids. This way we can keep track of which philosophers have chopsticks and when they intend to put one down.

How could we do the semaphore in perl?

Replies are listed 'Best First'.
Re: chopsticks5;
by Shendal (Hermit) on Sep 26, 2000 at 22:02 UTC
    Have you tried searching the site for semaphore (that's an 'a' not an 'e')? I see three man pages and one discussion on it. Perhaps that's a good place to start?
    Cheers,
    Shendal
Re: chopsticks5;
by BigJoe (Curate) on Sep 26, 2000 at 22:18 UTC
    I say philosophers and using a language like perl would:

    Toss the sticks because they are not efficient.
    Put bowl to mouth and
    chomp;

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.
Re: chopsticks5;
by Adam (Vicar) on Sep 26, 2000 at 22:04 UTC
    That's a good question. I want to know the answer too. My best guess would be that you can't, because a semaphore must be handled in an un-interupt-able manner. Usually talk of semaphores involves the functions P and V which request and release the semaphore respectivly (usually by incrementing and decrementing a counter.) The problem is the functions must also test the state of the semaphore: increment if 0, decrement if 1. And that has to be done atomically. I think Perl might be too abstracted for that level of control... maybe an Inline C statement could do it though...

    Update: Sweet: Shendal points out three different modules that do the work for you. When in doubt... :-)

    That's what I get for ignoring my own advice and not searching.