进程调度
# 进程调度
使用pcntl_fork
创建了一个子进程,这个时候就会存在父进程和子进程,CPU
先调度哪个进程,由操作系统来决定。
一般我们会在父进程,使用sleep
或者pcntl_wait
阻塞住,目的都是让子进程先运行,父进程后运行。
在pcntl
扩展中,封装了一个可以控制进程优先级的函数。
先说一下进程观察的命令:
top
命令
top - 15:29:00 up 25 days, 17:54, 1 user, load average: 0.14, 0.11, 0.09
Tasks: 133 total, 1 running, 131 sleeping, 0 stopped, 1 zombie
%Cpu(s): 1.2 us, 0.5 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3880176 total, 184884 free, 2252832 used, 1442460 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 1325140 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1
2
3
4
5
6
7
2
3
4
5
6
7
在 linux 系统中,一般把进程/线程称为任务
Task
这边主要关注一个1 zombie
僵尸进程,我这边有一个僵尸进程,是一个python
的,过多会导致负载变大。
- PID:进程号
- USER:进程是谁启动的
- TIME+COMMAND:是用的什么命令启动的在什么时候
- PR:(
priority
)优先级的意思 - NI:(
nice
)进程的nice
值nice
值越小,则优先级越高 - CPU:占了多少 CPU 的资源
进程的
nice
值越小,则进程的PR
优先级就越高,CPU
就先运行这个进程。
可以使用pcntl
的扩展函数:pcntl_setpriority
函数来设置函数的优先级
<?php
$nice = $argv[1];
$start = time();
$count = 0;
$pid = pcntl_fork();
if (0 == $pid) {
// 子进程
fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
pcntl_setpriority($nice, getmypid(), PRIO_PROCESS);
fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
while (1) {
$count++;
if (time() - $start > 5) {
break;
}
}
} else {
// 父进程
fprintf(STDOUT, "parent process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
// pcntl_setpriority($nice, getmypid(), PRIO_PROCESS);
// fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
while (1) {
$count++;
if (time() - $start > 5) {
break;
}
}
}
fprintf(STDOUT, "pid=%d, nice=%d, count=%d\n", posix_getpid(), pcntl_getpriority(), $count);
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
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
[root@jb51 process]# php demo10.php
parent process pid=22887, nice=0
child process pid=22888, nice=0
child process pid=22888, nice=0
pid=22888, nice=0, count=38142766
pid=22887, nice=0, count=38222392
1
2
3
4
5
6
2
3
4
5
6
它的PR
和NI
都是 20 和 0,还有对应的CPU
,22887
的要稍微高一点点。
再次运行,加上参数,调整nice
值
[root@jb51 process]# php demo10.php 20
parent process pid=24058, nice=0
child process pid=24059, nice=0
child process pid=24059, nice=19
pid=24059, nice=19, count=38367900
pid=24058, nice=0, count=40976526
1
2
3
4
5
6
2
3
4
5
6
再次调整,现在不设置子进程的nice
值,现在设置父进程的nice
值
<?php
$nice = $argv[1];
$start = time();
$count = 0;
$pid = pcntl_fork();
if (0 == $pid) {
// 子进程
fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
// pcntl_setpriority($nice, getmypid(), PRIO_PROCESS);
// fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
while (1) {
$count++;
if (time() - $start > 5) {
break;
}
}
} else {
// 父进程
fprintf(STDOUT, "parent process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
pcntl_setpriority($nice, getmypid(), PRIO_PROCESS);
fprintf(STDOUT, "parent process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority());
while (1) {
$count++;
if (time() - $start > 5) {
break;
}
}
}
fprintf(STDOUT, "pid=%d, nice=%d, count=%d\n", posix_getpid(), pcntl_getpriority(), $count);
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
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
通过调整之后,子进程的调度的概率就更多了,父进程就很少。
如果都设置nice
值,最终的结果还是系统会偏向一点父进程,别的都差不多。
编辑 (opens new window)
上次更新: 2022/04/10, 17:31:37