检查用C正在运行的进程 [英] Check running processes in C

查看:75
本文介绍了检查用C正在运行的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人请告诉我如何检查是否与给定的进程ID UNIX进程是一个C程序中运行。我知道我可以调用系统(),并使用ps命令,但我不想调用系统()。

Can some one please tell me how to check if a unix process with a given process id is running inside a C program. I know I can call system() and use the ps command but I dont want to call the system().

推荐答案

使用杀(2)

if (kill(pid, 0) == 0) {
    /* process is running or a zombie */
} else if (errno == ESRCH) {
    /* no such process with the given pid is running */
} else {
    /* some other error... use perror("...") or strerror(errno) to report */
}

这篇关于检查用C正在运行的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆