sub passeer { my($semaphore) = @_; # this is a filename! my($wait) = 1; # Wait for semaphore: WAIT: while ($wait) { # Wait for semaphore to be released (if set): while (-f $semaphore) { sleep(5); } # When semaphore is gone, create own one: unless (open(SEMAPHORE, ">$semaphore")) { unlink($semaphore); warn "can't write semaphore '$semaphore': $!\n"; next WAIT; } print SEMAPHORE "$$\n"; close(SEMAPHORE); # Check wether semaphore is our's or was created # in the meantime by another process: unless (open(SEMAPHORE, "<$semaphore")) { unlink($semaphore); warn "can't read semaphore '$semaphore': $!\n"; next WAIT; } $_ = ; chop; close(SEMAPHORE); # Loop if semaphore isn't our's: $wait = ($_ != $$); # if ($wait) # { # warn "Semaphore access conflict detected!\n"; # warn "Waiting for other process to terminate.\n"; # } } } sub verlaat { my($semaphore) = @_; # Release semaphore: unlink($semaphore); }