PHP Doku:: Aktualisiert eine Variable im Shared Memory - function.shm-put-var.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzErweiterungen zur ProzesskontrolleSemaphore, Shared Memory and IPCSemaphore Funktionenshm_put_var

Ein Service von Reinhard Neidl - Webprogrammierung.

Semaphore Funktionen

<<shm_has_var

shm_remove_var>>

shm_put_var

(PHP 4, PHP 5)

shm_put_varAktualisiert eine Variable im Shared Memory

Beschreibung

bool shm_put_var ( resource $shm_identifier , int $variable_key , mixed $variable )

shm_put_var() aktualisiert die Variable variable mit dem angegebenen variable_key oder fügt sie ein, sofern sie noch nicht vorhanden ist.

Warnungen (auf E_WARNING-Stufe) werden erzeugt, wenn shm_identifier kein gültiger SysV-Shared Memory-Index oder wenn nicht genügend Speicher im angegebenen Segment verfügbar ist.

Parameter-Liste

shm_identifier

Ein Shared Memory-Ressourcehandle, wie es von shm_attach() zurückgegeben wird.

variable_key

Der Variablenschlüssel.

variable

Die Variable. Alle Variablentypen werden unterstützt.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.


6 BenutzerBeiträge:
- Beiträge aktualisieren...
orwellophile at spamtrak dot org
6.04.2010 13:17
Quote: "Will it ever support resource identifiers like pfsockopen() pointers ...  we run PHP as a Apache Module ... no way to have true persistent sockets"

Sorry, but that doesn't make sense to me... the socket is still persistent, if you wish to resume it, simply call pfsockopen() with the same host and port - and you will get the same socket.  There is no need to pass the actual resource variable.

If there is something amazingly special and unique about each socket, you can do the following - and this should apply to any persistent resource:

To differentiate between or obtain a specific resource - simply serialize/store an index of each resource's unique ID, along with the particulars that make that resource unique.

You can get a unique resource identifier as an integer value like so:

<?php
   $rid
= str_replace("Resource id #", "", print_r($fp, true));
  
// $rid = 2
?>

As pfsockopen() uses the hostname and port as a unique key to resume a persistent connection, you can add a DNS wildcard, or a number of manual entries in /etc/hosts (or windows equiv.) as follows:

resource-0.host.com  192.168.100.1
resource-1.host.com  192.168.100.1
resource-2.host.com  192.168.100.1
resource-3.host.com  192.168.100.1

Then, after consulting your serialized list of resources, you can connect to a specific resource by using it's resource id.

eg:  $pf = pfsockopen("resource-$rid.host.com", $port, $timeout);

The new resource will be identical to the original in every way.

For file based stream resources you could do something similar with symlinks, or use the next method...

For URL based or other resources with "paths" (I don't know if there *are* persistent functions that involve such things) you could differentiate between them with using extraneous information in the path.  eg:

  http://host.com/resource-4/../script.php
  http://resource4@host.com/script.php
  /tmp/././././file.txt

In the first example, the extraneous "resource-4" would be ignored by the webserver.

In the second, the superfluous username would be ignored by the webserver.  (Something similar for mysql_pconnect could be done with multiple usernames).

And in the the third example, four sequential occurrences of the "do nothing" string "./" would indicate resource #4.

If this isn't enough, then you can use the fact that PHP shared memory resources are themselves interoperable with those created by their .c counterparts.   That allows you to write a thin .c application to handle the dirty work.

Or you could attempt to reconnect to your own webserver, using persistent streams and the methods outlined above, to achieve the end result.   I can't think of an example of where something so extreme would be necessary, but I'm sure it's not outside the realm of possibility.

I personally use an 117 MB binary database, which is stored in shared memory, both from the command line (using a complied .c application), and from the web (via PHP, and ftok()/shmop_open()/shmop_read()).
ygbr at me dot com
12.10.2009 23:25
Will it ever support resource identifiers like pfsockopen() pointers?

The main problem is that when we run PHP as a Apache Module we never know in which process the next request will bind to, making impossible to have true persistent socket connections unless we can store the pointer to it or directly open the socket inode with fopen() like functions and retrieve the same resource pointer again.

I thought I could use shm, but it seems that shm doesn't allow o store resource pointers... sad... :(
Hendrik Klindworth
2.02.2009 17:28
shm_put_var has no protection against race conditions. If two scripts insert the same key at the same time php might segfault.
jasonrlester at yahoo dot com
25.04.2008 21:51
sadly troy is right

the following script will return:

resource(5) of type (stream)
int(0)

<?php

define
("FOPEN_RESOURCE", 1);
define("FOPEN_FILEPATH", "/path/to/file");

$fopen_resource = fopen(FOPEN_FILEPATH, "w");

var_dump($fopen_resource);

$shm_id = shm_attach(1);
if (
$shm_id === false)
{
    echo
"Fail to attach shared memory.\n";
}

if (!
shm_put_var($shm_id, FOPEN_RESOURCE, $fopen_resource))
{
    echo
"Failed to put var 1 in shared memory $shm_id.\n";
}

$sm_fopen_resource = shm_get_var($shm_id, FOPEN_RESOURCE);
if (
$sm_fopen_resource === false)
{
    echo
"Failed to retreive fopen_resource from Shared memory\r\n";
}

var_dump($sm_fopen_resource);

if(
$shm_id) shm_remove($shm_id);
if(
$fopen_resource) fclose($fopen_resource);

?>
troy
6.03.2008 16:02
This isn't entirely accurate. Not all variable types are supported, you can't put a resource variable into shared memory.

When you try to take it out, it will be a zero.
tomlove at gmail dot com
1.10.2004 1:53
Use as few variable_keys as you can. With large arrays of data, rather make the array multi-dimensional and store under one variable_key than use variable_key as your index. The benefit is especially noticeable when repeated fetching from the end of the array is necessary and updates are less frequent.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)