Think about Loose Coupling | |
PerlMonks |
jerryhone has asked for the wisdom of the Perl Monks concerning the following question:
Depending on exact timing, I see that it's possible for the first process to create the semaphore and attempt to lock the other process, but the other one running a fraction behind has not detected the creation so it does its own and sets the semaphore to 1, so revoking it's partner's lock! I can't see a fool proof way of getting around this, so any divine inspiration gratefully received.$sem = IPC::Semaphore->new( 4321, 1, S_IRUSR | S_IWUSR ); if ( $sem ) { # Semaphore already exists so just open it print "Semaphore already exists - just open it\n"; $sem = IPC::Semaphore->new( 4321, 1, S_IRUSR | S_IWUSR ); } else { # The semaphore didn't already exit so create it print "Create semaphore \n"; $sem = IPC::Semaphore->new( 4321, 1, IPC_CREAT | S_IRUSR | S_IWUSR + ); print "Semaphore created\n"; $sem->setval(0,1); print "Semaphore initialised\n"; } print "Locking other threads\n"; $sem->op(0, -1, SEM_UNDO);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Semaphore puzzle
by choroba (Cardinal) on Feb 22, 2021 at 23:02 UTC | |
by jerryhone (Sexton) on Feb 23, 2021 at 09:00 UTC | |
by jeffenstein (Hermit) on Feb 23, 2021 at 10:35 UTC | |
by jerryhone (Sexton) on Feb 23, 2021 at 17:16 UTC | |
Re: Semaphore puzzle
by jcb (Parson) on Feb 23, 2021 at 02:29 UTC | |
Re: Semaphore puzzle
by Corion (Patriarch) on Feb 23, 2021 at 09:12 UTC | |
by jerryhone (Sexton) on Feb 23, 2021 at 09:21 UTC | |
Re: Semaphore puzzle
by jerryhone (Sexton) on Feb 23, 2021 at 09:15 UTC | |
Re: Semaphore puzzle
by Anonymous Monk on Feb 23, 2021 at 17:40 UTC | |
Re: Semaphore puzzle
by Anonymous Monk on Feb 23, 2021 at 01:21 UTC | |
|