shmop PHP扩展有什么作用? [英] What does the shmop PHP extension do?

查看:71
本文介绍了shmop PHP扩展有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Shmop是一组易于使用的函数,可让PHP读取, 编写,创建和删除Unix共享内存段.

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?

推荐答案

共享内存允许多个进程访问内存中的相同数据.您可以使用它在正在运行的PHP脚本之间共享数据.

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.

不同的进程可以同时访问相同的共享内存,这可能导致竞争条件.在上面的示例中,如果两个进程在共享内存中的任何一个回写之前都读取了共享内存,则该计数将比应有的数量少1.可以通过使用互斥体来防止出现竞争情况,但这不在此问答中.

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.

共享内存用于一种进程间通信,即数据传递.在PHP中可用的其他一些(取决于平台和PHP构建)是:

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:

  • 信号( posix_kill 发送信号,套接字用于数据.套接字可以使用网络,也可以是本地的.
  • 用于数据的管道. posix_mkfifo 用于创建命名管道(又名 FIFO )和标准文件函数用于读取和写入数据.可以使用 proc_open .请注意,无法在任意进程之间创建未命名管道.请注意,某些系统上的管道是单向的:管道句柄可以用于读取或写入,但不能同时用于两者.
  • 信号灯用于消息的消息队列.在PHP中,信号量扩展既提供消息队列,又提供另一组共享内存功能(例如 shm_attach ).还提供了各种消息传递协议的许多其他扩展,包括 SAM STOMP 其他服务,其他人.
  • 网络流包装器以获取数据.在较低的级别上,它们只是套接字,尽管它们提供了不同的接口.它们也用于特定的应用程序级别协议,而套接字则更通用.
  • 网络协议扩展,例如 cURL ,用于消息传递&数据.像流包装一样,它们是(有限的)伪装套接字.
  • Web服务扩展,例如 XML-RPC ,用于远程过程调用(RPC).请注意,尽管这些是基于套接字的,但它们适用于不同类型的IPC(RPC而不是数据).
  • 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.

这篇关于shmop PHP扩展有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆