当前位置: 技术问答>linux和unix
用c如何完成判断某个服务是否开启 (请各位指点一下!谢谢)
来源: 互联网 发布时间:2015-08-27
本文导语: 用c如何完成判断某个服务是否开启 (请各位指点一下!谢谢) 我现在没有任何头绪或者方法!请各位指点一下!谢谢!或者告诉我大概要看哪些内容! | 如果知道进程名 就可以用下面的代码: =======...
用c如何完成判断某个服务是否开启 (请各位指点一下!谢谢)
我现在没有任何头绪或者方法!请各位指点一下!谢谢!或者告诉我大概要看哪些内容!
我现在没有任何头绪或者方法!请各位指点一下!谢谢!或者告诉我大概要看哪些内容!
|
如果知道进程名 就可以用下面的代码:
=========================================================================
#include "unistd.h"
#include "dirent.h"
#include "sys/param.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "sys/stat.h"
#include "stdio.h"
//-----------------------------------------------------------------------------
#define READ_BUF_SIZE 50
char sts[READ_BUF_SIZE]; //用来保存进程的状态
//-----------------------------------------------------------------------------
void find_status_by_name(char* pidName)
{
DIR *dir;
struct dirent *next;
FILE *file;
char filename[READ_BUF_SIZE];
char buffer[READ_BUF_SIZE];
char name[READ_BUF_SIZE];
dir = opendir("/proc");
if (!dir)
{
fprintf(stderr, "Cannot open /procn");
}
while ((next = readdir(dir)) != NULL)
{
/* Must skip ".." since that is outside /proc */
if (strcmp(next->d_name, "..") == 0)
continue;
/* If it isn't a number, we don't want it */
if (!isdigit(*next->d_name))
continue;
sprintf(filename, "/proc/%s/status", next->d_name);
if (! (file = fopen(filename, "r")) )
{
continue;
}
/* Read first line in /proc/?pid?/status */
fgets(buffer, READ_BUF_SIZE-1, file);
/* Buffer should contain a string like "Name: binary_name" */
sscanf(buffer, "%*s %s", name);
if (strcmp(name, pidName) == 0)
{
fgets(buffer, READ_BUF_SIZE-1, file);
fclose(file);
sscanf(buffer, "%*s %s", sts);
break;
}
}
closedir(dir);
}
==============================================================================
然后查看sts中是什么
R ->runable
S ->Sleep
......
=========================================================================
#include "unistd.h"
#include "dirent.h"
#include "sys/param.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "sys/stat.h"
#include "stdio.h"
//-----------------------------------------------------------------------------
#define READ_BUF_SIZE 50
char sts[READ_BUF_SIZE]; //用来保存进程的状态
//-----------------------------------------------------------------------------
void find_status_by_name(char* pidName)
{
DIR *dir;
struct dirent *next;
FILE *file;
char filename[READ_BUF_SIZE];
char buffer[READ_BUF_SIZE];
char name[READ_BUF_SIZE];
dir = opendir("/proc");
if (!dir)
{
fprintf(stderr, "Cannot open /procn");
}
while ((next = readdir(dir)) != NULL)
{
/* Must skip ".." since that is outside /proc */
if (strcmp(next->d_name, "..") == 0)
continue;
/* If it isn't a number, we don't want it */
if (!isdigit(*next->d_name))
continue;
sprintf(filename, "/proc/%s/status", next->d_name);
if (! (file = fopen(filename, "r")) )
{
continue;
}
/* Read first line in /proc/?pid?/status */
fgets(buffer, READ_BUF_SIZE-1, file);
/* Buffer should contain a string like "Name: binary_name" */
sscanf(buffer, "%*s %s", name);
if (strcmp(name, pidName) == 0)
{
fgets(buffer, READ_BUF_SIZE-1, file);
fclose(file);
sscanf(buffer, "%*s %s", sts);
break;
}
}
closedir(dir);
}
==============================================================================
然后查看sts中是什么
R ->runable
S ->Sleep
......