当前位置: 技术问答>linux和unix
怎么知道LINUX系统某个特定的程序在运行.并且把它杀掉.用C实现.谢谢了.
来源: 互联网 发布时间:2015-12-17
本文导语: 如上 | #include #include #include #include /* typedef int bool; #define true 1 #define false 0 */ char *get_ps_name(char *string, int *pid) { // printf(string); char seps[] = " "; char *token; int i = 0; token = st...
如上
|
#include
#include
#include
#include
/*
typedef int bool;
#define true 1
#define false 0
*/
char *get_ps_name(char *string, int *pid)
{
// printf(string);
char seps[] = " ";
char *token;
int i = 0;
token = strtok(string, seps); i++;
*pid = atoi(token); //pid在第一列
while(token != NULL)
{
// printf("%d: %sn", i++, token);
if(i == 4)return token; //进程名在第4列
token = strtok(NULL, seps); i++;
}
return NULL;
}
bool IsExist(char* processname, char* filename, int *pid)
{
*pid = -1;
bool ret = false;
FILE *stream;
if( (stream = fopen(filename, "r")) == NULL)
{
printf("open %s failed.", filename);
return false;
}
char line[1024];
char *ch;
ch = fgets(line, 1024, stream);//略去第一行
while(ch != NULL)
{
ch = fgets(line, 1024, stream);
line[strlen(line)-1] = '';
char *ps = get_ps_name(line, pid);
if( ps != NULL && strcmp(processname, ps) == 0)//找到了进程名
{
//确认进程存在
char buf[64];
sprintf(buf, "/proc/%d", *pid);
if( access(buf, F_OK) == 0)
ret = true;
break;
// printf("%d: %sn", *pid, ps);
}
// printf("%d: %sn", pid, get_ps_name(line, &pid));
}
/*
if(feof(stream))
printf("end filen");
else
printf("errorn");
*/
fclose(stream);
return ret;
}
int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("usage: find_ps_name processnamen");
exit(1);
}
int pid;
char *command = "ps -A > tmp";
system(command);
char *name = argv[1] /*"rpc.rquotad"*/;
if(IsExist(name, "tmp", &pid))
{
printf("%d: %sn", pid, name);
char buf[100];
sprintf(buf, "kill %d", pid);
if(system(buf)
#include
#include
#include
/*
typedef int bool;
#define true 1
#define false 0
*/
char *get_ps_name(char *string, int *pid)
{
// printf(string);
char seps[] = " ";
char *token;
int i = 0;
token = strtok(string, seps); i++;
*pid = atoi(token); //pid在第一列
while(token != NULL)
{
// printf("%d: %sn", i++, token);
if(i == 4)return token; //进程名在第4列
token = strtok(NULL, seps); i++;
}
return NULL;
}
bool IsExist(char* processname, char* filename, int *pid)
{
*pid = -1;
bool ret = false;
FILE *stream;
if( (stream = fopen(filename, "r")) == NULL)
{
printf("open %s failed.", filename);
return false;
}
char line[1024];
char *ch;
ch = fgets(line, 1024, stream);//略去第一行
while(ch != NULL)
{
ch = fgets(line, 1024, stream);
line[strlen(line)-1] = '';
char *ps = get_ps_name(line, pid);
if( ps != NULL && strcmp(processname, ps) == 0)//找到了进程名
{
//确认进程存在
char buf[64];
sprintf(buf, "/proc/%d", *pid);
if( access(buf, F_OK) == 0)
ret = true;
break;
// printf("%d: %sn", *pid, ps);
}
// printf("%d: %sn", pid, get_ps_name(line, &pid));
}
/*
if(feof(stream))
printf("end filen");
else
printf("errorn");
*/
fclose(stream);
return ret;
}
int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("usage: find_ps_name processnamen");
exit(1);
}
int pid;
char *command = "ps -A > tmp";
system(command);
char *name = argv[1] /*"rpc.rquotad"*/;
if(IsExist(name, "tmp", &pid))
{
printf("%d: %sn", pid, name);
char buf[100];
sprintf(buf, "kill %d", pid);
if(system(buf)