PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previoussql_regcaseftoknext
Last updated: Tue, 09 Jul 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

XC. S�maphores et gestion de la m�moire partag�e

Ce module fourni un syst�me de s�maphore. Ce syst�me utilise les s�maphores System V. Les s�maphores peuvent �tre utilis�s pour fournir un acc�s exclusif � certaines ressources de la machine, ou pour limiter le nombre de processus qui utilisent en m�me temps une ressource.

Ce module fournit aussi un syst�me de m�moire partag�e, qui utilise la m�moire partag�e System V. Cette m�moire partag�e permet d'acc�der � des variables globales. Les diff�rents d�mons httpd et m�mes d'autres programmes (tels que Perl, C, ...) permettent un tel �change de donn�es global. N'oubliez pas que la m�moire partag�e n'est pas prot�g�es contre l'acc�s simultan�. Il vous faudra utiliser les s�maphores pour assurer la synchronisation.

Tableau 1. Limites de la m�moire partag�e sous Unix OS

SHMMAX Taille maximale de m�moire partag�e, par d�faut, 131072 octets.
SHMMIN Taille minimale de m�moire partag�e, par d�faut, 1 octet.
SHMMNI Nombre maximal de segment de m�moire partag�, par d�faut 100.
SHMSEG Taille maximale de m�moire partag�e par processus, par d�faut 6.

Table des mati�res
ftok --  Convert a pathname and a project identifier to a System V IPC key
msg_get_queue --  Create or attach to a message queue
msg_receive --  Receive a message from a message queue
msg_remove_queue --  Destroy a message queue
msg_send --  Send a message to a message queue
msg_set_queue --  Set information in the message queue data structure
msg_stat_queue --  Returns information from the message queue data structure
sem_acquire -- R�serve un s�maphore.
sem_get -- Retourne un identifiant de s�maphore.
sem_release -- Lib�re un s�maphore.
sem_remove -- Remove a semaphore
shm_attach --  Cr�e ou ouvre un segment de m�moire partag�e.
shm_detach -- Lib�re un segment de m�moire partag�e.
shm_get_var --  Lit une variable dans la m�moire partag�e.
shm_put_var --  Ins�re ou modifie une variable de la m�moire partag�e.
shm_remove_var --  Efface une variable de la m�moire partag�e.
shm_remove --  Supprime un segment de m�moire partag�e sous Unix.
User Contributed Notes
S�maphores et gestion de la m�moire partag�e
add a note about notes
[email protected]
21-Sep-2000 05:58

The integer keys for sem_get() and shm_attach() have to be systemwide unique. There is no method to ensure that no other process on the system will use your specific key (security! and possible malfunction). Also shared memory is very seldom used there are possibilities for conflicts! To see the used id's you can use the program 'ipcs' (at least under SuseLinux;) ). Thanks Christian C.
[email protected]
01-Jun-2001 03:46

As for security, please look at the perm argument to shm_get. Shared Memory blocks has the same permission semantics as unix user/group/other file permissions. As long as your webserver is running as a user that no other users can script to.. and as long as the permissions are set to 600, you should be fine and have no security concerns.
[email protected]
02-May-2002 12:30

If only Sys V message queues were supported, I would be happy.
[email protected]
22-Jun-2002 04:54

Samlpe code for using most of the functions here:

$MEMSIZE = 512;// size of shared memory to allocate
$SEMKEY = 1; // Semaphore key
$SHMKEY = 2; // Shared memory key

echo "Start.\n";
// Get semaphore
$sem_id = sem_get($SEMKEY, 1);
if ($sem_id === false)
{
echo "Fail to get semaphore";
exit;
}
else
echo "Got semaphore $sem_id.\n";

// Accuire semaphore
if (! sem_acquire($sem_id))
{
echo "Fail to aquire semaphore $sem_id.\n";
sem_remove($sem_id);
exit;
}
else
echo "Success aquire semaphore $sem_id.\n";

$shm_id = shm_attach($SHMKEY, $MEMSIZE);
if ($shm_id === false)
{
echo "Fail to attach shared memory.\n";
sem_remove($sem_id);
exit;
}
else
echo "Success to attach shared memory : $shm_id.\n";

// Write variable 1
if (!shm_put_var($shm_id, 1, "Variable 1"))
{
echo "Fail to put var 1 on shared memory $shm_id.\n";
sem_remove($sem_id);
shm_remove ($shm_id);
exit;
}
else
echo "Write var1 to shared memory.\n";

// Write variable 2
if (!shm_put_var($shm_id, 2, "Variable 2"))
{
echo "Fail to put var 2 on shared memory $shm_id.\n";
sem_remove($sem_id);
shm_remove ($shm_id);
exit;
}
else
echo "Write var2 to shared memory.\n";

// Read variable 1
$var1 = shm_get_var ($shm_id, 1);
if ($var1 === false)
{
echo "Fail to retrive Var 1 from Shared memory $shm_id, return value=$var1.\n";
}
else
echo "Read var1=$var1.\n";

// Read variable 1
$var2 = shm_get_var ($shm_id, 2);
if ($var1 === false)
{
echo "Fail to retrive Var 2 from Shared memory $shm_id, return value=$var2.\n";
}
else
echo "Read var2=$var2.\n";

// Release semaphore
if (!sem_release($sem_id))
echo "Fail to release $sem_id semaphore.\n";
else
echo "Semaphore $sem_id released.\n";

// remove shared memory segmant from SysV
if (shm_remove ($shm_id))
echo "Shared memory successfully removed from SysV.\n";
else
echo "Fail to remove $shm_id shared memory from SysV.\n";

// Remove semaphore
if (sem_remove($sem_id))
echo "semaphore removed successfully from SysV.\n";
else
echo "Fail to remove $sem_id semaphore from SysV.\n";
echo "End.\n";

add a note about notes
previoussql_regcaseftoknext
Last updated: Tue, 09 Jul 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat Aug 31 06:19:44 2002 CEST