http://qs321.pair.com?node_id=11128688


in reply to Re^2: Semaphore puzzle
in thread Semaphore puzzle

I think choroba was suggesting this (untested):

$sem = IPC::Semaphore->new( 4321, 1, S_IRUSR | S_IWUSR | IPC_CREAT | I +PC_EXCL ); if ( $sem ) { # New semaphore print "Semaphore created\n"; $sem->setval(0,1); print "Semaphore initialised\n"; } else { # 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 ); + ); } print "Locking other threads\n"; $sem->op(0, -1, SEM_UNDO);

But there is still a race condition, where the second process can attach to and use the semaphore after the first process creates it and before the first process initializes it.

Replies are listed 'Best First'.
Re^4: Semaphore puzzle
by jerryhone (Sexton) on Feb 23, 2021 at 17:16 UTC
    I've implemented a process with the sender creating a semaphore before sending a file request so that the receivers can use it - that all seems to work! :)
    Next issue...with our NDM implementation the sending and receiving IDs are different. My receiver processes are running under the receiver ID and can't remove the semaphore created by the sending ID when they're complete, even though both IDs are in the same group and the semaphore has group read/write permission. Is there an IPC semaphore permission that allows deletion by other than the owner?