Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Semaphore puzzle

by choroba (Archbishop)
on Feb 22, 2021 at 23:02 UTC ( #11128670=note: print w/replies, xml ) Need Help??


in reply to Semaphore puzzle

The race condition is not that the second process doesn't detect the creation. It in fact runs the check before the first process has created the semaphore, but after it checked its existence (and didn't find it). You need to create the semaphore right ahead, if the creation failed, you know you are the second one.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Semaphore puzzle
by jerryhone (Sexton) on Feb 23, 2021 at 09:00 UTC
    Thanks for the suggestion. I have already tried an exclusive creation (IPC_CREAT | IPC_EXCL) which I understand is what you're suggesting. I hit the same issue...the files arrive too closely together that neither create process detects that the semaphore has already been created so they both create successfully!

      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.

        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?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11128670]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2021-04-14 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?