惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
F
Fortinet All Blogs
D
Docker
博客园 - 司徒正美
腾讯CDC
Recent Announcements
Recent Announcements
The Cloudflare Blog
B
Blog RSS Feed
GbyAI
GbyAI
T
Tailwind CSS Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志

WangDeer

非机动车交通规则测验 微软 50 周年壁纸是在 macOS上设计的? 「转」西二旗折叠:蜂巢经济学启示录 难得空明 Ubuntu Clean ApidocJs apigroup Support Chinese int(10)里面的10是什么意思? 5-3 会话 2021-03-25 「PHP架构师」面试准备 Laravel 上传 Docx 文件,表单验证不通过 4-4 发送信号 4-3 信号集 4-2 编写中断信号处理程序 2022-03-10 面试复盘 4-1 什么是中断信号(软中断) 3-7 进程查看 3-6 SUID特权进程 3-5多进程编写 3-3进程exec 3-2进程退出和回收 3-1进程标识与fork 2-3解释器文件 2-2命令行参数与环境参数表 2-1程序与进程 1-1课程介绍 终极复盘 第八章 传承篇 第七章 自由篇 第六章 品牌篇 第五章 作品篇
3-4进程调度
王二 · 2022-03-05 · via WangDeer

pcntl_fork 创建了一个子进程,这个时候就会存在父进程和子进程,cpu先调度哪个进程?

pcntl 封装了可以控制进程优先级的函数 pcntl_setprioritysetpriority),修改任意进程的优先级,pcntl_getprioritygetpriority)获取任意进程的优先级。

进程的观察命令:top

top - 23:28:59 up 13:13,  1 user,  load average: 0.30, 1.20, 1.61
任务: 386 total,   1 running, 385 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.8 us,  1.7 sy,  0.1 ni, 95.3 id,  0.0 wa,  0.0 hi,  0.2 si,  0.0 st
MiB Mem :   5855.6 total,    664.6 free,   3875.4 used,   1315.7 buff/cache
MiB Swap:    976.0 total,    364.7 free,    611.3 used.   1379.6 avail Mem 

进程号 USER      PR  NI    VIRT    RES    SHR    %CPU  %MEM     TIME+ COMMAND                                                                        
7305  wanger    20   0  4956504   454380 52432 S  4.6   7.6  28:24.62 gnome-shell

...

在 Linux 系统中,一般把进程/线程称为任务Task。

  • PR priority 进程的优先级

  • NI nice 进程的nice值 nice值越小,则优先级越高

进程的nice越小,则进程的PR优先级越高,cpu就先运行这个进程。

示例:

<?php

$nice = $argv[1];
$start = time();
$count = 0;

$pid = pcntl_fork();

if ($pid == 0){

    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);