PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ftok> <SDO_DAS_Relational::executeQuery
Last updated: Sat, 24 Mar 2007

view this page in

CXLV. Semaphore, Shared Memory and IPC Functions

Úvod

Toto rozšíření poskytuje rozhraní k rodině IPC funkcí Systemu V. To zahrnuje semafory, sdílenou paměť a meziprocesové zprávy (IPC).

Semafory se dají používat k poskytování exkluzivního přístupu k prostředkům na daném systému, nebo k omezení počtu procesů, které mohou současně používat určitý prostředek.

Toto rozšíření také poskytuje funkce pro práci se sdílenou pamětí využívající System V sdílenou paměť. Sdílená pmět se dá používat k poskytování přístupu ke globálním proměnným. Různí httpd-daemoni a dokonce i jiné programy (např. Perl, C, ...) mohou k těmto datům přistupovat, a vytvořit tak globální výměnu dat. Pamatujte, že sdílená paměť není chráněna proti simultáním přístupům. K synchronizaci použijte semafory.

Tabulka 247. Omezení sdílené paměti systémem Unix

SHMMAXmax. velikost sdílené paměti, normálně 131072 bytů
SHMMINmin. velikost sdílené paměti, normálne 1 byte
SHMMNImax. počet segmentů sdílené paměti, normálně 100
SHMSEGmax. počet segmentů sdílené paměti na proces, normálně 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.

Poznámka: Toto rozšíření není k dispozici na platformách Windows.

Požadavky

Tyto funkce jsou k dispozici jako součást standardního modulu, který je vždy dostupný.

Instalace

Podpora těchto funkcí není ve výchozím nastavení zapnuta. Pro zapnutí podpory semaforů Systemu V zkompilujte PHP s volbou --enable-sysvsem. Pro zapnutí podpory sdílené paměti Systemu V zkompilujte PHP s volbou --enable-sysvshm. Pro zapnutí podpory zpráv Systemu V zkompilujte PHP s volbou --enable-sysvmsg.

Konfigurace běhu

Chování těchto funkcí je ovlivněno nastavením parametrů v php.ini.

Tabulka 248. Konfiguraèní volby pro Semaphore

NázevVýchozí hodnotaLze zmìnit
sysvmsg.value"42"PHP_INI_ALL
sysvmsg.string"foobar"PHP_INI_ALL

Pro bli¾¹í podrobnosti a definici PHP_INI_* konstant viz ini_set().

Typy prostředků

Předdefinované konstanty

Toto rozšíření nemá definovány žádné konstanty.

Obsah

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 — Získat semafor
sem_get — Získat id semaforu
sem_release — Uvolnit semafor
sem_remove — Remove a semaphore
shm_attach — Vytvořit nebo otevřít segment sdílené paměti
shm_detach — Odpojit se od segmentu sdílené paměti
shm_get_var — Vrátit proměnnou ze sdílené paměti
shm_put_var — Vložit nebo modifikovat proměnnou do sdílené paměti
shm_remove_var — Odstranit proměnnou ze sdílené paměti
shm_remove — Odstranit sdílenou paměť ze systému


ftok> <SDO_DAS_Relational::executeQuery
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
Semaphore, Shared Memory and IPC Functions
mr dot smaon at gmail dot com
25-Jan-2007 09:08
Here is a simple Mutex class implementation, using semaphore on Linux and flock on windows.
Filename is optionnal, but you can provide it anyway. This way the file will be created on windows, not on linux as it is not needed.
It's fast written and certainly lacks error checking code.

<?php

   
class Mutex
   
{
        private
$id;
        private
$sem_id;
        private
$is_acquired = false;
        private
$is_windows = false;
        private
$filename = '';
        private
$filepointer;

        function
__construct()
        {
            if(
substr(PHP_OS, 0, 3) == 'WIN')
               
$this->is_windows = true;
        }

        public function
init($id, $filename = '')
        {
           
$this->id = $id;

            if(
$this->is_windows)
            {
                if(empty(
$filename)){
                    print
"no filename specified";
                    return
false;
                }
                else
                   
$this->filename = $filename;
            }
            else
            {
                if(!(
$this->sem_id = sem_get($this->id, 1))){
                    print
"Error getting semaphore";
                    return
false;
                }
            }

            return
true;
        }

        public function
acquire()
        {
            if(
$this->is_windows)
            {
                if((
$this->filepointer = @fopen($this->filename, "w+")) == false)
                {
                    print
"error opening mutex file<br>";
                    return
false;
                }
               
                if(
flock($this->filepointer, LOCK_EX) == false)
                {
                    print
"error locking mutex file<br>";
                    return
false;
                }
            }
            else
            {
                if (!
sem_acquire($this->sem_id)){
                    print
"error acquiring semaphore";
                    return
false;
                }
            }

           
$this->is_acquired = true;
            return
true;
        }

        public function
release()
        {
            if(!
$this->is_acquired)
                return
true;

            if(
$this->is_windows)
            {
                if(
flock($this->filepointer, LOCK_UN) == false)
                {
                    print
"error unlocking mutex file<br>";
                    return
false;
                }

               
fclose($this->filepointer);
            }
            else
            {
                if (!
sem_release($this->sem_id)){
                    print
"error releasing semaphore";
                    return
false;
                }
            }

           
$this->is_acquired = false;
            return
true;
        }

        public function
getId()
        {
            return
$this->sem_id;
        }
    }

?>

Example use:

<?php

    $mutex
= new Mutex();
   
$mutex->init(1, "mutex_file.txt");
   
$mutex->acquire();

   
//Whatever you want single-threaded here...
   
$mutex->release();

?>
hekkwan at gmail dot com
14-Nov-2006 06:15
I have been trying to get a php console script and a C application to use a common semaphore for a while. I just got it working, so I thought Id paste the code here incase anyone needs to do this, however, this is not the place for long code examples

I used c code from the php implementation to set up the semaphore set and then mimic the way the php interpreter implements a mutext type locking scheme, using a common semop call.

One has to do the process in the same way as its done in the php implementation, otherwise you run the risk of the php interpreter resetting the semaphore set for you.

The basic idea is.
1) sem_get - use a three semaphore set
1.1) increment the first sem
1.2) check the usage count (sem 3), if only one, set the max_attach using sem 2 for mutex behaviour to sem 3
2) decrement sem 1
3) for locking / unlocking use the first semaphore, but always call the above from your c-code.

If you want a copy of my code, email me and I'll happily send it to you !
chrissavery at removeme dot gmail dot com
02-Jun-2006 01:18
I was confused by two things that caused strange behaviour in my use of semaphores with php scripts running under apache.

Often enough page requests will end up being filled by the same process as other simultaneous requests. So semaphores will block when you may not have expected.

Also note that sem_remove() will remove it for all processes, not just the calling one. So you have to be sure that the last process running removes the semaphore and none before. I thought there was some failures occurring when my child processes were dropping out with errors.

So you can't just use get, acquire, release, remove in one script that will be hit by a web user. (1) They may end up in the same process and will wait on the other, and (2) the first one to finish will destroy the semaphore for others.

I left out the remove call, and it works ok, but I still wonder if the semaphore is removed by php when the last script that did a get finishes? Also creating a child process to do the work using proc_open works to ensure seperate processes but to be careful you would want to limit the number somehow as well.
Denis Gerasimov
02-Nov-2005 04:19
Many (most?) developers use Win32 platform for PHP Web applications development while production servers mostly run Unix/Linux OS. Below is the stub code I use to make it possible to write scripts on Win32 that use semaphores:

<?php

if (substr(PHP_OS, 0, 3) == 'WIN') {  // if Windows OS detected

   
function ftok($pathname, $proj)
    {
        if (empty(
$pathname) || !file_exists($pathname)) {  // an error occured
           
return -1;
        }
       
       
$pathname = $pathname . (string) $proj;
       
$key = array();
        while (
sizeof($key) < strlen($pathname)) {
           
$key[] = ord(substr($pathname, sizeof($key), 1));
        }
       
        return
dechex(array_sum($key));
    }
   
    function
sem_acquire($sem_identifier)
    {
        return
true;
    }

    function
sem_get($key, $max_acquire = null, $perm = null, $auto_release = null)
    {
        return
true;
    }
   
    function
sem_release($sem_identifier)
    {
        return
true;
    }
   
    function
sem_remove($sem_identifier)
    {
        return
true;
    }
   
}

?>

Of course, there is no way to test semaphores until you have no Unix/Linux test server.
p4nzer at yahoo dot com
01-Nov-2005 05:55
Actually, the way to lock a semaphore from C code appears to be:
<?
    struct sembuf semptr[2];

    if( (semid = semget(SEM_KEY, PHP_SEM_NEED_NUMBER, 0666 | IPC_CREAT)) < 0 ) {
        perror("semget");
        return 1;
    }

    semptr[0].sem_num = 0;
    semptr[0].sem_op = -1;
    semptr[0].sem_flg = SEM_UNDO;
    semptr[1].sem_num = 1;
    semptr[1].sem_op =  1;
    semptr[1].sem_flg = SEM_UNDO;

    if( semop(semid, &semptr[0], 2) < 0 ) {
    perror("semop");
    }
?>

PS: Disregard the <? and ?>, it's just for pretty printing... this is C code!
hcuevas at galenicom dot com
23-Oct-2003 07: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 01:45
Here is a quick utility to dump the contents of a shm_ format memory segment:

http://www.davidc.net/php/shm/
Roman Laptev <tmp at laptev dot org>
02-Apr-2003 06: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 10: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
31-May-2001 09: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
20-Sep-2000 11: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.

ftok> <SDO_DAS_Relational::executeQuery
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites