|
|
 |
XCIV. Funzioni per i semafori, la memoria condivisa ed IPCIntroduzione
Questo modulo fornisce le funzioni relative all'IPC di System V.
Queste includono semafori, memoria condivisa e messaggi tra i processi (IPC).
I semafori possono essere utilizzati per fornire
un accesso esclusivo alle risorse sulla macchina corrente, oppure per limitare
il numero di processi che possono utilizzare simultaneamente una risorsa.
Questo modulo fornisce anche le funzioni per la memoria condivisa a partire dalla gestione della
memoria condivisa di System V. La memoria condivisa può essere utilizzata per fornire
l'accesso a variabili globali. Differenti demoni httpd e anche altri programmi
(tipo Perl, C, ...) sono in grado di accedere a questi dati creando
uno scambio di dati globale. Si ricordi che la memoria condivisa non è garantita
nei confronti di accessi simultanei. Si utilizzino i semafori per la sincronizzazione.
Tabella 1. Limiti della memoria condivisa posti da UNIX | SHMMAX | dimensione massima della memoria condivisa, solitamente 131072 bytes | | SHMMIN | dimensione minima della memoria condivisa, solitamente 1 byte | | SHMMNI |
massimo ammontare dei segmenti di memoria condivisa sul sistema,
solitamente 100
| | SHMSEG |
numero massimo di segmenti di memoria condivisa per processo, solitamente 6
|
Le funzioni relative ai messaggi possono essere usate per inviare e ricevere
messaggi da/per altri processi. Esse permettono un semplice ed efficace metodo di interscambio dati
tra i processi, senza dovere ricorrere ad alternative quali i
socket nel dominio Unix.
Nota: Questo modulo non è
disponibile su piattaforme Windows.
RequisitiNon sono necessarie librerie esterne per utilizzare questo modulo. Installazione
Di default non viene abilitato il supporto per queste funzioni.
Per abilitare il supporto dei semafori di System V, compilare il PHP con l'opzione
--enable-sysvsem.
Per abilitare il supporto della memoria condivisa, compilare il PHP con l'opzione
--enable-sysvshm.
Per abilitare il supporto dei messaggi, compilare il PHP con l'opzione
--enable-sysvmsg.
Configurazione di Runtime
Il comportamento di queste funzioni è influenzato dalle impostazioni di php.ini.
Tabella 2. Opzioni per la configurazione dei Semafori | Nome | Default | Modificabile |
|---|
| sysvmsg.value | "42" | PHP_INI_ALL | | sysvmsg.string | "foobar" | PHP_INI_ALL |
Per maggiori dettagli sulle costanti PHP_INI_* vedere
ini_set().
Costanti predefiniteQuesta estensione non definisce alcuna costante. - Sommario
- ftok --
Converte il percorso e un identificatore di progetto in un chiave IPC di System V
- msg_get_queue --
Crea, o si collega ad una coda di messaggi
- msg_receive --
Riceve un messaggio da una coda
- msg_remove_queue --
Distrugge una coda di messaggi
- msg_send --
Invia un messaggio ad una coda di messaggi
- msg_set_queue --
Valorizza le informazioni nella struttura dati della coda dei messaggi
- msg_stat_queue --
Restituisce informazioni dalla struttura dati della coda
- sem_acquire -- Acquisisce un semaforo
- sem_get -- Ottiene l'id di un semaforo
- sem_release -- Rilascia un semaforo
- sem_remove -- Rimuove un semaforo
- shm_attach -- Crea oppure apre un segmento di memoria condivisa
- shm_detach -- Disconnette da un segmento di memoria condivisa
- shm_get_var -- Restituisce una variabile dalla memoria condivisa
- shm_put_var -- Inserisce o aggiorna una variabile nella
memoria condivisa
- shm_remove_var -- Rimuove una variabile dalla memoria condivisa
- shm_remove -- Rimuove un segmento di memoria condivisa dal sistema Unix
add a note
User Contributed Notes
Funzioni per i semafori, la memoria condivisa ed IPC
hcuevas at galenicom dot com
23-Oct-2003 01:49
Don't use semaphores to serialize access to an undefined number of resources. There is no way (yet) to know before locking if a semaphore is already locked, thus not being able to fully release the semaphore and occupying a semaphore resource for an undefined time.
A possible solution is to build a shared mem pool and store there the current number of locks for a semaphore id.
Cheers,
Horaci Cuevas
david () php net
09-Jul-2003 07:45
Roman Laptev <tmp at laptev dot org>
02-Apr-2003 01:50
If you going to work with semaphore, which was created by some external program, you can try the following code for this program (C example):
#define SVSEM_MODE (SEM_R | SEM_A | SEM_R>>3 | SEM_R>>6) /* 0644 */
#define PHP_SEM_NEED_NUMBER 3
/*.......*/
int semid, semflag = SVSEM_MODE | IPC_CREAT | IPC_EXCL;
struct sembuf semptr;
union semun semopts;
/*.......*/
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, semflag)) >= 0 ) {
semopts.val = 1; /* initial value for sem */
if( semctl( semid, 0, SETVAL, semopts) < 0 ) {/*error*/}
if( semctl( semid, 1, SETVAL, semopts) < 0 ) {/*error*/}
/* PHP wanna zero for its own semget at third sem.
* look at ./PHP_SOURCE_PATH/ext/sysvsem/sysvsem.c
*/
semopts.val = 0;
if( semctl( semid, 2, SETVAL, semopts) < 0 ) {/*error*/}
}
else if(errno == EEXIST) { /* connect only */
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, SVSEM_MODE | IPC_CREAT)) < 0 ) {/*error*/}
}
else {/*error*/}
/*.......*/
/* If you want acquire the sem */
semptr.sem_num = 0;
semptr.sem_op = -1; /* lock it */
semptr.sem_flg = SEM_UNDO;
while( semop(semid, &semptr, 1) < 0 ) {/*error*/}
/*.......*/
Thanks,
Roma
elran70 at hotmail dot com
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";
mikew at php dot net
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.
php at stolt dot de
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.
| |