当前位置: 技术问答>linux和unix
怎么样在Linux编程中查看某个指定的程序是否在运行?
来源: 互联网 发布时间:2015-07-21
本文导语: 在程序中查某个指定的程序在运行,或不在运行。如果在运行,怎么样得到它的进程ID? | #include #include #include #include #include // get pid by program name int main (int argc, char* argv[]) { DI...
在程序中查某个指定的程序在运行,或不在运行。如果在运行,怎么样得到它的进程ID?
|
#include
#include
#include
#include
#include
// get pid by program name
int main (int argc, char* argv[])
{
DIR *pdir = NULL;
struct dirent *pde = NULL;
FILE *pf = NULL;
char buff[128];
char buff2[128];
char *pstr = NULL;
int pid, ppid;
int n;
int i;
pdir = opendir("/proc");
if (!pdir)
{
perror("open /proc fail.n");
return -1;
}
while ((pde = readdir(pdir)))
{
if ((pde->d_name[0] d_name[0] > '9'))
{
continue;
}
sprintf(buff, "/proc/%s/status", pde->d_name);
pf = fopen(buff, "r");
if (pf)
{
n = fread(buff, 1, 127, pf);
close(pf);
buff[n] = 0;
for (i = 0; i
#include
#include
#include
#include
// get pid by program name
int main (int argc, char* argv[])
{
DIR *pdir = NULL;
struct dirent *pde = NULL;
FILE *pf = NULL;
char buff[128];
char buff2[128];
char *pstr = NULL;
int pid, ppid;
int n;
int i;
pdir = opendir("/proc");
if (!pdir)
{
perror("open /proc fail.n");
return -1;
}
while ((pde = readdir(pdir)))
{
if ((pde->d_name[0] d_name[0] > '9'))
{
continue;
}
sprintf(buff, "/proc/%s/status", pde->d_name);
pf = fopen(buff, "r");
if (pf)
{
n = fread(buff, 1, 127, pf);
close(pf);
buff[n] = 0;
for (i = 0; i