linux 识别僵尸进程,检测Linux上的pid是否是僵尸

您需要使用proc(5)文件系统.访问其中的文件(例如/ proc / 1234 / stat …)非常快(它不涉及任何物理I / O).

你可能想要/ proc / 1234 / stat中的第三个字段(每个人都可以读取,但是你应该按顺序读取它,因为它是不可见的.).如果该字段是Z,那么pid 1234的过程是僵尸.

无需分叉进程(例如withpopen或system),在C中你可能编写代码

pid_t somepid;

// put the process pid you are interested in into somepid

bool iszombie = false;

// open the /proc/*/stat file

char pbuf[32];

snprintf(pbuf, sizeof(pbuf), "/proc/%d/stat", (int) somepid);

FILE* fpstat = fopen(pbuf, "r");

if (!fpstat) { perror(pbuf); exit(EXIT_FAILURE); };

{

int rpid =0; char rcmd[32]; char rstatc = 0;

fscanf(fpstat, "%d %30s %c", &rpid, rcmd, &rstatc);

iszombie = rstatc == 'Z';

}

fclose(fpstat);

还要考虑procps和libproc,请参阅this answer.

(您还可以阅读/ proc / 1234 / status的第二行,但这可能更难以在C或C代码中解析)

顺便说一下,我发现/ proc /中的stat文件有一种奇怪的格式:如果你的可执行文件恰好在其名称中包含空格和括号(这很恶心,但允许),解析/ proc / * / stat文件变得棘手.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值