suche nach in der

sem_release> <sem_acquire
Last updated: Fri, 18 May 2012

view this page in

sem_get

(PHP 4, PHP 5)

sem_getZugriff auf ein Semaphor anfordern

Beschreibung

resource sem_get ( int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] )

sem_get() liefert eine ID die für den Zugriff auf das System V Semaphor mit dem gegebenen key benutzt werden kann.

Weitere Aufrufe von sem_get() für den gleichen Key liefern unterschiedliche Semaphor IDs, diese greifen aber auf den gleichen darunterliegenden Semaphor zu.

Parameter-Liste

key

max_acquire

Die Anzahl der Prozesse die gleichzeitig Zugriff auf den Semaphor erhalten können wird durch den Parameter max_acquire festgelegt

perm

Die Zugriffsrechte für den Semaphore. Der Wert wird nur gesetzt wenn der aktuelle Prozess der einzige ist der in diesem Augenblick mit dem Semaphor verknüpft ist.

auto_release

Legt fest ob der Semaphor am Skriptende automatisch freigegeben werden soll.

Rückgabewerte

Gibt einen positiven Semaphor Identifier zrück oder FALSE bei Fehlern.

Changelog

Version Beschreibung
4.3.0 Der auto_release Parameter wurde hinzugefügt.

Siehe auch

  • sem_acquire() - Zugriff auf Semaphor anfordern
  • sem_release() - Semaphor freigeben
  • ftok() - Erzeugt aus einem Dateipfad und einem Projektbezeichner einen System V IPC Schlüssel



add a note add a note User Contributed Notes
sem_get
ein at anti-logic dot com
26-Jun-2007 01:21
Be aware that there is no way to ensure that you have exclusive access to a lock, despite setting max_acquire=1.

In example,
<?
$fp
= sem_get(fileinode('lock_file', 100);
sem_acquire($fp);

$fp2 = sem_get(fileinode('lock_file', 1);
sem_acquire($fp2);
?>

This will not block on the second sem_aquire.  Therefore, if you have functions or processes that utilize shared locks (>1 max_acquire) you will still need to provide a seperate lock mechanism (ie flock) for write access, making the sem_ functions useless.

Some more info, in flock, each reference to the lock file has it's own options (can be shared exclusive blocking non blocking etc), but apparently php's sem functions only support these options per semaphore, not per semaphore-reference.
neofutur
28-Aug-2006 09:49
with gentoo php5 you will need to add the USE flag :
sysvipc

see :
 http://forums.gentoo.org/viewtopic-t-464175-highlight-semget+php.html

and also :
 http://overlays.gentoo.org/proj/php/
joeldg AT listbid.com
02-May-2003 08:39
Heh, actually the above comment I added is not technically correct, it was more of an idea to display the function.

$SHM_KEY = ftok("/home/joeldg/homeymail/shmtest.php", 'R');
$shmid = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data = shm_attach($shmid, 1024);
// we now have our shm segment

// lets place a variable in there
shm_put_var ($data, $inmem, "test");
// now lets get it back. we could be in a forked process and still have
// access to this variable.
printf("shared contents: %s\n", shm_get_var($data, $inmem));

shm_detach($data);
joeldg at listbid.com
02-May-2003 09:36
<?
// thanks to
// http://www.ecst.csuchico.edu/~beej/guide/ipc/shmem.html
$SHM_KEY = ftok("/home/joeldg/homeymail/shmtest.php", 'R');
$shmid = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data = shm_attach($shmid, 1024);

$data = "test";
printf("shared contents: %s\n", $data);

shm_detach($data);
?>

sem_release> <sem_acquire
Last updated: Fri, 18 May 2012