update page now

sem_acquire

(PHP 4, PHP 5, PHP 7, PHP 8)

sem_acquireFordert Zugriff auf einen Semaphor an

Beschreibung

sem_acquire(SysvSemaphore $semaphore, bool $non_blocking = false): bool

Standardmäßig blockiert sem_acquire() (falls nötig) bis ein Zugriff auf den Semaphor möglich ist. Ein Prozess, der Zugriff auf einen Semaphor anfordert, auf den er bereits Zugriff hat, wird für immer blockieren, wenn dieser erneute Zugriff die maximalen Zugriffe auf diesen Semaphor überschreiten würde.

Nach Abarbeitung einer Anfrage werden alle noch vom Prozess gehaltenen Semaphore automatisch freigegeben und entsprechende Warnungen erzeugt.

Parameter-Liste

semaphore

semaphore ist ein Semaphor, der von sem_get() zurückgegben wurde.

non_blocking

Gibt an, ob der Prozess nicht auf den Zugriff auf den Semaphor warten soll. Bei true wird der Aufruf unmittelbar false zurückgeben, wenn der Semaphor nicht sofort reserviert werden kann.

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Changelog

Version Beschreibung
8.0.0 semaphore erwartet nun eine SysvSemaphore-Instanz; vorher wurde eine resource erwartet.

Siehe auch

add a note

User Contributed Notes 3 notes

up
3
gladd at trash dot eris dot qinetiq dot com
21 years ago
Just to clarify what is meant by "process" above:

On the Apache webserver, many PHP requests will be executed within the same process space because it is multithreaded. However, any semaphores got and acquired by a script and not released and removed will still be automatically cleaned up by the PHP interpreter each time the script terminates.

Remove any trash before emailing!
up
6
Pinky
13 years ago
sem_acquire() is blocking, meaning that subsequent calls with the same semaphore will block indefinitely until the semaphore is released. This ensures serialization, but it is not very practical if all you want to do is check if you should proceed or not. Unfortunately, PHP does not yet support any method of querying the state of a semaphore in a non-blocking manner.

It may seem possible to put together such a mechanism by hand, using shared memory (shm_ functions). However, be warned that it is not trivial and ultimately non-productive. You cannot, for example, simply pick a shared mem var, store the semaphore key and query it. Such an operation would be non-transactional and non-atomic ie. it is possible for two or more parallel processes to manage to read "not locked" from the shared mem var before one of them manages to mark it "locked". You would have to use a (blocking) semaphore to serialize access to the shared mem var, thus recreating the very problem you are trying to solve.

In other words, if non-blocking queries are crucial to you, you need to either request that this issue be solved by the PHP designers, or pick another mechanism to do your locking, one that already has this feature.
up
2
Sander Backus
12 years ago
Note that when you reset $sem_identifier the semaphore won't block anymore!

This code does NOT work: 
    $key     = ftok(__FILE__,'m');
    $a        = sem_get($key);
    sem_acquire($a);
    $a = false;

while this one does: 

    $key     = ftok(__FILE__,'m');
    $a        = sem_get($key);
    sem_acquire($a);
    //$a = false;

So: use unique var names for your identifier!
To Top