如何查找进程是否在C中运行? [英] How to find if a process is running in C?

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

问题描述

我想找出一个进程是否正在运行.我不想使用任何system(")命令.是否有任何基于C的函数可以让您知道进程是否正在运行?

I would like to find out if a process is running. I DO NOT want to use any system("") commands. Is there any C based function that lets you know if a process is running?

我想提供进程名称,并想知道它是否正在运行.

I would like to provide the process name and want to know if it's running.

谢谢

推荐答案

当然,请使用

Sure, use kill(2):

 #include <sys/types.h>
 #include <signal.h>

 int kill(pid_t pid, int sig);

如果sig为0,则不发送信号,但仍执行错误检查;可以用来检查是否存在进程ID或进程组ID.

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

因此,只需在要检查的进程的进程ID上调用kill(pid, 0),以查看是否收到错误(ESRCH).

So just call kill(pid, 0) on the process ID of the process that you want to check for and see if you get an error (ESRCH).

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

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