Using PHP shmop Functions for Shared Memory: Overview and Practical Examples
This article explains PHP's two shared memory interfaces, shm and shmop, details the primary shmop functions, provides code examples for creating, reading, writing, and deleting shared memory segments, demonstrates testing with Linux commands, and includes a practical class‑based usage example.
PHP provides two interfaces for shared memory: shm , which serializes variables for storage, and shmop , a lower‑level Linux/Windows compatible API.
The main shmop functions are:
shmop_open //创建或打开共享内存块
shmop_write //向共享内存块中写入数据
shmop_read //从共享内存块中读取数据
shmop_size //获取共享内存块的大小
shmop_close //关闭共享内存块
shmop_delete //删除共享内存块A basic usage example shows how to create a memory segment, read data, write JSON‑encoded data, obtain the actual used size, and close the segment:
<?php
//创建一块共享内存
$shm_key = 0x4337b101;
$shm_id = @shmop_open($shm_key, 'c', 0644, 1024);
//读取并写入数据
$data = shmop_read($shm_id, 0, 1024);
shmop_write($shm_id, json_encode($data), 0);
$size = shmop_size($shm_id); //获取内存中实际数据占用大小
//关闭内存块,并不会删除共享内存,只是清除 PHP 的资源
shmop_close($shm_id);
?>shmop_open requires four parameters: a system ID (key), an access mode (similar to fopen ), permission bits (octal), and the segment size in bytes. The function returns a numeric ID used by subsequent calls; it returns FALSE on failure.
shmop_write works like fwrite : it takes the segment ID, the data to store, and an optional offset (default 0). It returns the number of bytes written or FALSE on error.
shmop_read reads data from a segment. It needs the segment ID, the start offset (usually 0), and the number of bytes to read. It behaves similarly to fread .
shmop_size returns the actual amount of data stored in the segment, which may be less than the allocated size.
shmop_delete marks a segment for deletion; the segment is removed only after all processes have closed it.
shmop_close releases the PHP resource for a segment, analogous to fclose for files.
A simple test on a Linux system can be performed with the ipcs -m command to list shared memory segments:
# ipcs -m
[root@bogon ~]# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 0 gdm 600 393216 2 dest
0x00000000 32769 gdm 600 393216 2 dest
0x4337b101 884750 nobody 644 1024 0The columns show the key, segment ID, owner, permissions, size in bytes, attachment count, and status (e.g., dest for pending deletion).
A practical project example demonstrates using shmop within a CodeIgniter model to cache a large skill list:
<?php
/**
* 将领技能
*/
class Generalskill_model extends CI_Model {
private $_memory_key = 0x4337b001; //共享内存地址key
private $_memory_size = 1048576; //开辟共享内存大小
public function __construct() {
parent::__construct();
}
public function get_skill_list() {
$data = [];
$shmid = @shmop_open($this->_memory_key, 'a', 0644, $this->_memory_size);
if ($shmid === FALSE) {
$shmid = @shmop_open($this->_memory_key, 'c', 0644, $this->_memory_size);
$data = $this->return_skill_list();
shmop_write($shmid, json_encode($data), 0);
@shmop_close($shmid);
return $data;
}
$data = json_decode(preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim(shmop_read($shmid, 0, $this->_memory_size))), true);
@shmop_close($shmid);
return $data;
}
public function return_skill_list() {
// 这里返回一个大数组,示例中省略具体内容
return [];
}
}
?>The example notes that when the underlying data changes, the segment should be deleted with shmop_delete and recreated. For more complex concurrent access, semaphores may be required.
At the end of the article, a promotional notice advertises a PHP training camp, providing enrollment links, schedule, and contact information.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.