15

http://www.php.net/manual/en/intro.shmop.php

Shmop is an easy to use set of functions that allows PHP to read, write, create and delete Unix shared memory segments.

I don't understand, what exactly is the purpose of this extension? What is it used for?

Alex
  • 66,732
  • 177
  • 439
  • 641

2 Answers2

39

Shared memory allows multiple processes to access the same data in memory. You can use it to share data among running PHP scripts.

 $shm = shmop_open(0xF00, "c", 0644, 4);

 $count = unpack('L', shmop_read($shm, 0, 4));
 $count = reset($count);
 var_dump($count);
 echo "count: ", $count++, "<br/>\n";
 shmop_write($shm, pack('L', $count), 0);

When the computer restarts, anything in shared memory is lost.

Different processes can access the same shared memory at the same time, which can lead to race conditions. In the example above, if two processes read the shared memory before either writes back to it, the count will be 1 less than it should be. Race conditions can be prevented by using a mutex, but that's outside the scope of this Q&A.

Shared memory is used for one type of inter-process communication, namely data passing. Some others available in PHP (depending on the platform and PHP build) are:

  • Signals (posix_kill to send a signal, pcntl_signal to set up a signal handler), a limited type of message passing. Signals aren't particularly useful in scripted pages, as each script should run for a very short time.
  • Sockets for data. Sockets can use a network, or can be local.
  • Pipes for data. posix_mkfifo is used to create named pipes (aka FIFOs), and the standard file functions are used to read and write data. Unnamed (aka anonymous) pipes can be created between parent and child processes using popen or proc_open. Note unnamed pipes cannot be created between arbitrary processes. Note that pipes on some systems are unidirectional: a pipe handle can be used either to read or write, but not both.
  • Semaphores for synchronization.
  • Message queues for messaging. In PHP, the Semaphore extension offers both message queues and another set of shared memory functions (e.g. shm_attach). Many other extensions for various messaging protocols are also available, including SAM, STOMP and AMQP. See "Other Services" in the PHP manual for, well, others.
  • Network stream wrappers for data. At a lower level, these are just sockets, though they provide a different interface. They are also for specific application level protocols, whereas sockets are more general.
  • Network protocol extensions, such as cURL, for messaging & data. Like stream wrappers, these are (limitd) sockets in disguise.
  • Web service extensions, such as SOAP and XML-RPC, for remote procedure calls (RPC). Note that while these are socket based, they're for a different type of IPC (RPC rather than data).

While sockets (and anything based on them, such as stream wrappers) and pipes can be used to pass data between processes, their abilities with more than two processes are limited. Sockets can only connect two processes; to handle more than two, multiple sockets need to be opened (which is where a client-server architecture usually comes into it). With pipes, only one process can read given data; once it has, that data won't be available to other readers, though they can read other data (which will then become unavailable to all but the reader). An arbitrary number of processes can open the same shared memory region.

Community
  • 1
  • 1
outis
  • 75,655
  • 22
  • 151
  • 221
9

When a running process requests memory, the system provides a slice of memory which can only be accessed by the allocated process. Sometimes you run multiple threads and want to share data amongst them.

"Sharing data" can be done by:

  • Passing data via sockets/pipes
  • Shared Memory (threads, processes)

Since passing data is not very handy in some cases, one might want to use Shared memory.

The functions in question provide functionality to handle shared memory segments in PHP.

outis
  • 75,655
  • 22
  • 151
  • 221
markusschmitz
  • 676
  • 5
  • 18
  • So it's basically useless then? I mean if you can use global variables for that, there's no point in messing with those shmop_* functions – Alex Dec 25 '11 at 22:33
  • 3
    @Alex: PHP doesn't really have "global variables" like that. It's a misnomer in PHP, as you always have separated process spaces. -- Think of shmop more like memcache, except that it's a POSIX/Unix system feature, intended for sharing between PHP and C apps (if it weren't for the binary representation mismatch, can't find that duplicate). You can also imagine it as a ramdisk, in fact shmop usually creates a file entry in `/etc/shm/*` – mario Dec 25 '11 at 22:36
  • Shared Memory is managed by the operating system. They have way more protection and control services compared to the limited usage of global variables. Shared memory is mostly used for interprocess-communication. If you don't explicitly need this, stick to variables. – markusschmitz Dec 25 '11 at 22:40
  • 1
    PHP has global variables. Global variables are only global to the process they belong to. They can't be accessed in other processes. – outis Dec 25 '11 at 22:41
  • @Fuzzy: what exactly do you mean by "passing variables"? Environment variables? Session variables? Request data (the query string and request body, accessibly as `$_GET` and `$_POST` in PHP)? Something else? – outis Dec 25 '11 at 22:44
  • Passing Variables from one method to another during runtime. I am talking about communication between methods. As mario mentioned before, shmop could be used for communication between C and PHP but between PHP methods passing a variable would do the job as well as shared memory (but this would be a little "oversized" ...) ;) – markusschmitz Dec 25 '11 at 22:56
  • In addition to my post: two (or more) threads of one process might use global variables instead of shared memory. – markusschmitz Dec 25 '11 at 22:59
  • @Fuzzy: formal parameters (i.e. passed values), global variables and shared memory have different purposes. Neither formal parameters nor global variables are viable alternatives to shared memory, as neither can be accessed outside of a single process; formal parameters can't even be directly accessed outside of their scope (the function they're defined on, and any functions defined within that function). – outis Dec 26 '11 at 05:07
  • RE: globals and threads–[globals are bad](http://c2.com/cgi/wiki?GlobalVariablesAreBad). Following the connectivity property of [capabilities programming](http://www.erights.org/elib/capability/ode/ode-capabilities.html) (which is actually a property of pure OOP), the shared data needed by a thread should be passed to it when it's created. In any case, PHP doesn't have threading built-in, so that's not relevant to a question about SHM in PHP. – outis Dec 26 '11 at 05:18
  • @outis: I know ;). I tried adding some extra information on the topic (thats why I didn't point out the pros/cons) but I totally agree. – markusschmitz Dec 26 '11 at 10:50
  • @Fuzzy: if you know, why do you say in your answer that passing variables can be used to share data between processes? – outis Dec 26 '11 at 11:24
  • Oh, I edited it but forgot the word "processes". Mea culpa ;) – markusschmitz Dec 26 '11 at 18:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6146/discussion-between-fuzzy-and-outis) – markusschmitz Dec 26 '11 at 18:25