Home Backend Development PHP Tutorial 单台服务器的PHP进程之间实现共享内存的方法_php技巧

单台服务器的PHP进程之间实现共享内存的方法_php技巧

May 17, 2016 am 08:42 AM
Shared memory

开发人员要想使php进程实现共享内存的读写,首先就要支持IPC函数,即php编译安装时指定:--enable-shmop  与--enable-sysvsem 两个选项

IPC (Inter-process communication) 是一个Unix标准机制,它提供了使得在同一台主机不同进程之间可以互相的方法。基本的IPC处理机制有3种:它们分别是共享内存、信号量和消息队列。本文中我们主要讨论共享内存和信号量的使用。

在不同的处理进程之间使用共享内存是一个实现不同进程之间相互的好方法。如果你在一个进程中向所共享的内存写入一段信息,那么所有其他的进程也可以看到这段被写入的数据。非常方便。在PHP中有了共享内存的帮助,你可以实现不同进程在运行同一段PHP脚本时返回不同的结果。或实现对PHP同时运行数量的实时查询等等。

共享内存允许两个或者多个进程共享一给定的存储区。因为数据不需要在客户机和服务器之间复制,所以这是最快的一种IPC。使用共享内存的唯一窍门是多个进程对一给定存储区的同步存取。

如何建立一个共享内存段呢?下面的代码可以帮你建立共享内存。

复制代码 代码如下:
$shm_id = shmop_open($key, $mode, $perm, $size);

注意,每个共享内存段都有一个唯一的ID, 在PHP中,shmop_open会把建立好的共享内存段的ID返回,这里我们用$shm_id记录它。而$key是一个我们逻辑上表示共享内存段的Key值。不同进程只要选择同一个Key id就可以共享同一段存储段。习惯上我们用一个串(类似文件名一样的东西)的散列值作为key id. $mode指明了共享内存段的使用方式。这里由于是新建,因此值为'c' –取create之意。如果你是已经建立过的共享内存那么请用'a', 取access之意。$perm参数定义了的权限,8进制,关于权限定义请看UNIX文件系统帮助。$size定义了共享内存的大小。尽管有点象fopen(文件处理)你可不要当它同文件处理一样。后面的描述你将看到这一点。

例如:

复制代码 代码如下:
$shm_id = shmop_open(0xff3, "c", 0644, 100);

这里我们打开了一个共享内存段 键值0xff3 –rw-r—r—格式,大小为100字节。

如果需要已有的共享内存段,你必须在调用shmop_open中设第3、4个参数为0。

在Unix下,你可以用一个命令行程序ipcs查询系统所有的IPC资源状态。不过有些系统要求需要超级用户方能执行。下图是一段ipcs的运行结果。

上图中系统显示了4个共享内存段,注意其中第4个键值为0x00000ff3的就是我们刚刚运行过的PHP程序所创建的。关于ipcs的用法请参考UNIX用户手册。

如何释放共享内存呢

释放共享内存的办法是调用PHP指令:shmop_delete($id)

复制代码 代码如下:
shmop_delete($id);

$id 就是你调用shmop_open所存的shmop_op的返回值。还有一个办法就是用UNIX的管理指令:

ipcrm id, id就是你用ipcs看到的ID.和你程序中的$id不一样。不过要小心,如果你用ipcrm直接删除共享内存段那么有可能导致其他不知道这一情况的进程在引用这个已经不复存在的共享内存器时出现一些不可预测的错误(往往结果不妙)。

如何使用(读写)共享内存呢

使用如下所示函数向共享内存写入数据

复制代码 代码如下:
int shmop_write (int shmid, string data, int offset)

其中shmid是用shmop_open返回的句柄。$Data变量存放了要存放的数据。$offset描述了写入从共享内存的开始第一个字节的位置(以0开始)。

读取操作是:

复制代码 代码如下:
string shmop_read (int shmid, int start, int count)

同样,指明$shmid,开始偏移量(以0开始)、总读取数量。返回结果串。这样,你就可以把共享内存段当作是一个字节数组。读几个再写几个,想干嘛就干嘛,十分方便。

现在,在单独的一个PHP进程中读写、创建、删除共享内存方面上你应该没有问题了。但是,显然实际运行中不可能只是一个PHP进程在运行中。如果在多个进程的情况下你还是沿用单个进程的处理方法,你一定会碰到问题--著名的并行和互斥问题。比如说有2个进程同时需要对同一段内存进行读写。当两个进程同时执行写入操作时,你将得到一个错误的数据,因为该段内存将之可能是最后执行的进程的内容,甚至是由2个进程写入的数据轮流随机出现的一段混合的四不象。这显然是不能接受的。为了解决这个问题,我们必须引入互斥机制。互斥机制在很多操作系统的教材上都有专门讲述,这里不多重复。实现互斥机制的最简单办法就是使用信号灯。信号量是另外一种进程间(IPC)的方式,它同其他IPC机构(管道、FIFO、消息队列)不同。它是一个记数器,用于控制多进程对共享数据的存储。同样的是你可以用ipcs和ipcrm实现对信号灯使用状态的查询和对其实现删除操作。在PHP中你可以用下列函数创建一个新的信号量并返回操作该信号量的句柄。如果该key指向的信号量已经存在,sem_get直接返回操作该信号量的句柄。

复制代码 代码如下:
int sem_get(int key [, int max_acquire [, int perm]])

$max_acquire 指明同时最多可以用几个进程进入该信号而不必等待该信号被释放(也就是最大同时处理某一资源的进程数目,一般该值均为一)。$perm指明了权限。

一旦你成功的拥有了一个信号量,你对它所能做的只有2种:请求、释放。当你执行释放操作时, 系统将把该信号值减一。如果小于0那就还设为0。而当你执行请求操作时,系统将把该信号值加一,如果该值大于设定的最大值那么系统将挂起你的处理进程直到其他进程释放到小于最大值为止。一般情况下最大值设为1,这样一来当一个进程获得请求时其他后面的进程只能等待它退出互斥区后释放信号量才能进入该互斥区并同时设为独占方式。这样的信号量常称为双态信号量。当然,如果初值是任意一个正数就表明有多少个共享资源单位可供共享应用。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to turn off win10gpu shared memory How to turn off win10gpu shared memory Jan 12, 2024 am 09:45 AM

Friends who know something about computers must know that GPUs have shared memory, and many friends are worried that shared memory will reduce the number of memory and affect the computer, so they want to turn it off. Here is how to turn it off. Let's see. Turn off win10gpu shared memory: Note: The shared memory of the GPU cannot be turned off, but its value can be set to the minimum value. 1. Press DEL to enter the BIOS when booting. Some motherboards need to press F2/F9/F12 to enter. There are many tabs at the top of the BIOS interface, including "Main, Advanced" and other settings. Find the "Chipset" option. Find the SouthBridge setting option in the interface below and click Enter to enter.

Application method of shared memory between multiple processes in Golang function Application method of shared memory between multiple processes in Golang function May 17, 2023 pm 12:52 PM

As a highly concurrent programming language, Golang's built-in coroutine mechanism and multi-threaded operations enable lightweight multi-tasking. However, in a multi-process processing scenario, communication and shared memory between different processes have become key issues in program development. This article will introduce the application method of realizing shared memory between multiple processes in Golang. 1. How to implement multi-process in Golang In Golang, multi-process concurrent processing can be implemented in a variety of ways, including fork, os.Process,

PHP shared memory function usage and application PHP shared memory function usage and application Jun 16, 2023 pm 12:27 PM

PHP shared memory function usage and application Shared memory refers to a technology where multiple processes access the same memory space at the same time. In concurrent programming, shared memory can be used for inter-process communication to achieve data sharing between different processes. PHP also provides related shared memory functions. This article will introduce the usage of PHP shared memory functions and discuss some practical application scenarios. Use of shared memory functions PHP provides the shmop extension module, which allows PHP to operate on system shared memory. The functions provided by this extension module

Using shared memory and message queues in C++ Using shared memory and message queues in C++ Aug 22, 2023 pm 04:21 PM

In C++, shared memory and message queues are two commonly used inter-process communication methods. They can help us share data and information between different processes, allowing for more efficient programming. Shared memory is a special memory area that can be shared by multiple processes. Using shared memory avoids the overhead of copying data and reduces the delay in transferring data between processes. To use shared memory in C++, you need to include the <sys/shm.h> header file and use shmget, shmat, sh

Python problems encountered in multi-process programming and their solutions Python problems encountered in multi-process programming and their solutions Oct 08, 2023 pm 04:57 PM

Python problems encountered in multi-process programming and their solutions require specific code examples. In Python, multi-process programming is a commonly used concurrent programming method. It can effectively take advantage of multi-core processors and improve program running efficiency. However, we will also encounter some problems when doing multi-process programming. This article will introduce several common problems and give corresponding solutions and code examples. Question 1: Inter-process communication In multi-process programming, communication between processes is a basic requirement. However, since processes have their own unique

How to use Redis and D language to develop shared memory functions How to use Redis and D language to develop shared memory functions Sep 22, 2023 am 09:57 AM

Overview of how to use Redis and D language to develop shared memory functions: As the complexity of computer applications and the demand for data processing increase, shared memory has become a commonly used method of data exchange. Redis is a high-performance in-memory database that provides rich data structures and support. This article will introduce how to use Redis and D language to develop shared memory functions, and attach specific code examples. Step 1: Install Redis and D language compiler First, you need to install Redis and D language compiler on your computer. Red

PHP multi-threaded programming practice: using shared memory for multi-process communication PHP multi-threaded programming practice: using shared memory for multi-process communication Jun 29, 2023 pm 12:50 PM

PHP is a scripting language widely used in web development. Generally, it is executed in a single thread. However, in some specific scenarios, we may need to use multi-threaded programming to improve program performance and efficiency. This article will introduce how to perform multi-threaded programming in PHP and use shared memory to achieve communication between multiple processes. First, we need to understand what multi-threaded programming and shared memory are. Multithreaded programming is a method of concurrent programming that allows a program to execute multiple threads at the same time, thereby improving program execution

How to create a shared memory Goroutine in Go? How to create a shared memory Goroutine in Go? Jun 02, 2024 am 11:32 AM

Shared memory Goroutines can be implemented through channels: create a channel to specify the element type. Start a Goroutine to write data to the channel. Use a range loop in the main Goroutine to read data from the channel. Completion of writing is indicated by closing the channel.

See all articles