|
|
XC. Semaphore, Shared Memory and IPC FunctionsBevezet�s
This module provides wrappers for the System V IPC family of functions.
It includes semaphores, shared memory and inter-process messaging (IPC).
Semaphores may be used to provide exclusive access to
resources on the current machine, or to limit the number of
processes that may simultaneously use a resource.
This module provides also shared memory functions using System V
shared memory. Shared memory may be used to provide access to
global variables. Different httpd-daemons and even other programs
(such as Perl, C, ...) are able to access this data to provide a
global data-exchange. Remember, that shared memory is NOT safe
against simultaneous access. Use semaphores for synchronization.
T�bl�zat 1. Limits of Shared Memory by the Unix OS SHMMAX | max size of shared memory, normally 131072 bytes | SHMMIN | minimum size of shared memory, normally 1 byte | SHMMNI |
max amount of shared memory segments on a system,
normally 100
| SHMSEG |
max amount of shared memory segments per process, normally 6
|
The messaging functions may be used to send and receive messages to/from
other processes. They provide a simple and effective means of exchanging
data between processes, without the need for setting up an alternative
using unix domain sockets.
Megjegyz�s:
These functions do not work on Windows systems.
K�vetelm�nyekAz itt le�rt f�ggv�nyek a standard modulban
tal�lhat�ak, ami mindig rendelkez�sre �ll. Telep�t�s
Support for this functions are not enabled by default.
To ennable System V semaphore support compile PHP with the option
--enable-sysvsem.
To enable the System V shared memory support compile PHP with the option
--enable-sysvshm.
Fut�sidej� be�ll�t�sokEz a kiterjeszt�s semmilyen konfigur�ci�s
be�ll�t�sokat nem defini�l. El�re defini�lt �lland�kEz a kiterjeszt�s semmilyen konstans
�rt�ket nem defini�l.
User Contributed Notes Semaphore, Shared Memory and IPC Functions |
|
[email protected]
21-Sep-2000 06: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 04: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 01:30 |
|
If only Sys V message queues were supported, I would be happy.
|
|
[email protected]
22-Jun-2002 05: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";
|
|
|
| |