Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

shared memory - What does the shmop PHP extension do?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/>
";
 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...