-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshare_memory.php
83 lines (73 loc) · 2.12 KB
/
share_memory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Created by PhpStorm.
* User: sibenx
* Date: 16/11/14
* Time: 上午3:05
*/
function demo(array $phoneList){
global $shareMemory;
global $signal;
$cnt = count($phoneList); //测试数组大小
$slice = 3; //需要调用的进程数量
$childList = [];
while($slice >= 0)
{
$pid = pcntl_fork();
if($pid > 0){
$childList[$pid] = 1;
//父进程什么都不用做
}elseif($pid == 0){
while(true)
{
// 标记信号量,这里被我承包了
sem_acquire($signal);
//检测共享内存是否存在
if (shm_has_var($shareMemory,SHARE_KEY)){
//从共享内存中拿数据
$val = shm_get_var($shareMemory,SHARE_KEY);
if($val>=$cnt)
{
sem_release($signal);
break;
}else
{
printf("Slice id:%s,phone:%s \r\n",$slice,$phoneList[$val]);
$val ++;
//再将数据写入共享内存
shm_put_var($shareMemory,SHARE_KEY,$val);
}
}else{
// 无值会,先初始化
shm_put_var($shareMemory,SHARE_KEY,0);
}
// 用完释放
sem_release($signal);
}
exit();
}else
{
//程序发生错误也需要关闭程序
exit();
}
$slice--;
}
// 等待所有子进程结束后回收资源
while(!empty($childList)){
$childPid = pcntl_wait($status);
if ($childPid > 0){
unset($childList[$childPid]);
}
}
}
const SHARE_KEY = 1;
// 创建一块共享内存
$shm_id = ftok(__FILE__,'a');
$shareMemory = shm_attach($shm_id);
// 创建一个信号量
$sem_id = ftok(__FILE__,'b');
$signal = sem_get($sem_id);
demo(range(0,900));
// 释放共享内存与信号量
shm_remove($shareMemory);
sem_remove($signal);