本文實(shí)例講述了php進(jìn)程(線程)通信基礎(chǔ)之System V共享內(nèi)存。分享給大家供大家參考,具體如下:
PHP默認(rèn)情況沒有開啟功能,要支持該功能在編譯PHP的時(shí)候要加入下面幾個(gè)選項(xiàng) System V消息,--enable-sysvmsg System V信號(hào)量支持,--enable-sysvsem System V共享內(nèi)存支持,--enable-sysvshm
PHP還挺shmop共享內(nèi)存,在編譯的時(shí)候開啟 --enable-shmop
System V共享內(nèi)存的相關(guān)函數(shù):
1: 創(chuàng)建信號(hào)量唯一標(biāo)識(shí)符
$ftok = ftok(__FILE__, 'a');
2: 創(chuàng)建共享內(nèi)存端
$id = shm_attach ( $ftok, 1000 , 0666 )
3: 斷開與共享內(nèi)存段的連接
4: 獲取一個(gè)變量值
$val = shm_get_var ( $id , $key )
5: 檢測(cè)變量是否存在
shm_has_var ( $id , $key )
6: 添加一個(gè)值到共享內(nèi)存里
shm_put_var ( $id , $key , $val )
7: 從共享內(nèi)存中刪除一個(gè)變量
shm_remove_var ( $id , $key )
8: 從系統(tǒng)中刪除共享內(nèi)存
?php
$tmp = tempnam(__FILE__, 'PHP');
$key = ftok($tmp, 'a');
$shmid = shm_attach($key);
$counter = 0;
shm_put_var( $shmid, 1, $counter );
class CounterThread extends Thread {
public $shmid;
public $is_runing = true;
public function __construct($shmid){
$this->shmid = $shmid;
}
public function run() {
$counter = shm_get_var( $this->shmid, 1 );
$counter++;
shm_put_var( $this->shmid, 1, $counter );
printf("Thread #%lu says: %s\n", $this->getThreadId(),$counter);
}
}
for ($i=0;$i10;$i++){
$threads[] = new CounterThread($shmid);
}
for ($i=0;$i10;$i++){
$threads[$i]->start();
}
for ($i=0;$i10;$i++){
$threads[$i]->join();
}
shm_remove( $shmid );
shm_detach( $shmid );
![](http://img.jbzj.com/file_images/article/201911/201911982441662.png?201910982617)
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP進(jìn)程與線程操作技巧總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- 淺談并發(fā)處理PHP進(jìn)程間通信之外部介質(zhì)
- PHP如何限制定時(shí)任務(wù)的進(jìn)程數(shù)量
- PHP基于進(jìn)程控制函數(shù)實(shí)現(xiàn)多線程
- 一文看懂PHP進(jìn)程管理器php-fpm
- php 的多進(jìn)程操作實(shí)踐案例分析
- php 多進(jìn)程編程父進(jìn)程的阻塞與非阻塞實(shí)例分析
- php實(shí)現(xiàn)的簡(jiǎn)單多進(jìn)程服務(wù)器類完整示例
- PHP 進(jìn)程池與輪詢調(diào)度算法實(shí)現(xiàn)多任務(wù)的示例代碼
- 淺談并發(fā)處理PHP進(jìn)程間通信之System V IPC