php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39168 Semaphore acquire timeout
Submitted: 2006-10-16 16:55 UTC Modified: 2018-06-18 13:18 UTC
Votes:14
Avg. Score:3.9 ± 1.4
Reproduced:12 of 13 (92.3%)
Same Version:3 (25.0%)
Same OS:1 (8.3%)
From: s dot s at terra dot com dot br Assigned:
Status: Open Package: Semaphore related
PHP Version: 5.1.6 OS: Freebsd 6
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: s dot s at terra dot com dot br
New email:
PHP Version: OS:

 

 [2006-10-16 16:55 UTC] s dot s at terra dot com dot br
Description:
------------
Should be great if there was an optional parameter (default = 0) in function sem_acquire to pass a timeout (in seconds).

Maybe send a warning no semaphore timeout ;)

Reproduce code:
---------------
Something like

<?php
$key = ftok(__FILE__, 'R');
$sem = sem_get($key);
if(sem_acquire($sem, 10)) {
  echo 'Semaphore acquire failed. Timeout?';
}
else {
  echo 'Semaphore acquired :)';
  sem_release($sem);
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-23 21:08 UTC] jani@php.net
-Package: Feature/Change Request +Package: Semaphore related
 [2012-12-09 05:16 UTC] jhill9693 at gmail dot com
You can do this with pcntl_alarm() and pcntl_signal() to send yourself the SIGALRM POSIX signal after a 
number of seconds.

<?php

declare(ticks = 1);

function handle_timeout() {
  throw new Exception('Semaphore acquire timeout');
}

pcntl_signal(SIGALRM, 'handle_timeout');

$key = ftok(__FILE__, 'R');
$sem = sem_get($key);

pcntl_alarm(10);
sem_acquire($sem);
pcntl_alarm(0);

echo 'Semaphore acquired :)';
sem_release($sem);
 [2018-05-05 11:04 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2018-05-05 11:04 UTC] cmb@php.net
As of PHP 5.6.1 the $nowait parameter is available, which appears
to provide an even more flexible solution to the timeout problem.

Do you agree?
 [2018-06-18 13:13 UTC] dnt at gmx dot gom
@jhill9693: The suggested workaround with pcntl_alarm doesn't work; probably because of #49340 (https://bugs.php.net/bug.php?id=49340).

@cmb: $nowait would stop trying to acquire the semaphore immediately. In my case I want to wait for the semaphore a few seconds and fail if it couldn't be acquired. To repeatedly call sem_acquire with $nowait until the semaphore is either acquired or a specified time period is elapsed would be not a good solution.
 [2018-06-18 13:18 UTC] cmb@php.net
-Status: Feedback +Status: Open -Assigned To: cmb +Assigned To:
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 07:01:23 2019 UTC