Gorby has asked for the wisdom of the Perl Monks concerning the following question:

Hello Wise Monks. In the code below, is there any chance at all that "some code" will be executed more than once in the same instance assuming 2 instances of this code are run at exactly the same time? My expectation is that one instance of the program will have to wait for the other to release the lock before executing "some code". (I'm using red had linux 7.1)
open(SEM, ">semaphore_file") || die "Cannot create semaphore $!"; flock(SEM, LOCK_EX) || die "Lock failed: $!"; #some code close(SEM);

Replies are listed 'Best First'.
Re: flock LOCK_EX
by Zaxo (Archbishop) on Jan 13, 2004 at 07:37 UTC

    Run shared-threaded or under mod_perl, instance 1 gets the lock. Instance 2 opens the global handle *SEM. Instance 1's handle is closed and its lock gone, but since it knows the global handle only by name, it never learns of it.

    Update: The two don't know about each other, are sharing a filehandle, and are executing the same code at maybe the same time. Globals are extra evil in such an environment.

    After Compline,
    Zaxo

      I don't understand your 2nd sentence. Which never learns of what, and does that make "some code" run simultaneously by both instances? Thanks.