随笔 - 13,  文章 - 0,  评论 - 0,  阅读 - 3291

思路:

1、 遍历/proc文件夹下所有数字开头的文件夹,访问其status文件,读取Name那项的信息,如果跟要杀的进程名一致,则说明该文件夹名字就是进程PID。

2、通过signal发送SIGINT消息(模拟Ctrl+C)

3、如果进程还存在,发送SIGKILL消息

 

头文件代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef  _My_KILL_Process_H
#define _My_KILL_Process_H
 
 
 
/* 尝试杀掉进程
0:  完成。使用SIGINT 杀掉进程
1:完成。使用SIGKILL 杀掉进程
-1: 无法打开/proc文件夹
-2: 找不到进程
-3: 杀进程失败
-4: 无法杀掉进程
 
//如果错误,可以获取错误字符串.
//errStr
*/
int  TryKillProcess(char* pname ,char *errStr);
 
  
#endif // ! _My_KILL_Process_H

 

 

实现文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include<stdio.h>
#include <stdlib.h>
#include <string.h> 
#include<sys/types.h>
#include<signal.h>
#include <errno.h> 
#include <dirent.h>
#include <unistd.h>
#include"myKillProcess.h"
 
  
//根据进程名称获取PID 。
//如果失败返回-1,没找到返回0
static pid_t GetPidByName(const char *pName)
{
    DIR *pdir = NULL;
    struct dirent *pde = NULL;
    FILE *pf = NULL;
    char buff[500];
     pid_t pid;
     char szName[256];
  
    // 遍历/proc目录下所有pid目录
    pdir = opendir("/proc");
    if (!pdir)
    {
        return -1;
    }
     
    while ((pde = readdir(pdir)))
    {
        if ((pde->d_name[0] < '0')|| (pde->d_name[0] > '9'))
        {
            continue;
        }
   
        // 读取 /proc/$pid/status文件
        sprintf(buff, "/proc/%s/status", pde->d_name);
        pf = fopen(buff, "r");
        if (pf)
        {
            fgets(buff,sizeof(buff),pf);
            fclose(pf);
            sscanf(buff,"%*s %s",szName); // 跳过Name:字段,获取进程名称
            pid=atoi(pde->d_name);
            if(strcmp(szName,pName)==0)
            {
                closedir(pdir);
                return pid;
            }
        }
    }
    closedir(pdir);
    return 0;
}
 
/*
0:  完成。使用SIGINT 杀掉进程
1:完成。使用SIGKILL 杀掉进程
-1: 无法打开/proc文件夹
-2: 找不到进程
-3: 杀进程失败
-4: 无法杀掉进程
*/
int  TryKillProcess(char* pname ,char *errstr)
{
    //第一步:查进程对应的PID
    pid_t mypid = GetPidByName(pname);
    if(mypid<0)
    {
        strcpy(errstr,"无法打开/proc文件夹\n");
        return -1;
    }
 
    if(mypid==0){
        sprintf(errstr, "找不到进程%s",pname);
        return -2;
    }
 
    //第二步:尝试用SIGINT方式杀进程
    int err1 = kill(mypid,SIGINT);
    if(err1!=0){
        sprintf(errstr, "杀进程失败[%s]",strerror(errno)); 
        return -3;
    }
 
    //wait 10ms
    usleep(10000);
 
    mypid = GetPidByName(pname);
    if(mypid<0)
    {
        strcpy(errstr,"无法打开/proc文件夹\n");
        return -1;
    }
 
    if(mypid==0){
        strcpy(errstr,"成功通过SIGINT杀掉进程");
        return 0;
    }
 
    //第三步:尝试用SIGKILL方式
    err1 = kill(mypid,SIGKILL);
    if(err1!=0){
        sprintf(errstr, "杀进程失败[%s]",strerror(errno)); 
        return -3;
    }
 
    //wait 10ms
    usleep(10000);
 
    mypid = GetPidByName(pname);
    if(mypid<0)
    {
        strcpy(errstr,"无法打开/proc文件夹\n");
        return -1;
    }
 
    if(mypid==0){
        strcpy(errstr,"成功通过SIGKILL杀掉进程");
        return 1;
    }
 
    sprintf(errstr, "无法杀掉进程[%s]",pname); 
    return  -4;
  
}

 

测试文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include"myKillProcess.h"
 
int main(int argc, char **argv)
{
    if(argc!=2) {
        perror("缺少要杀的进程名。\n");
        return -1;
    }
    char errstr[256]={0};
    int ret = TryKillProcess(argv[1],errstr);
    printf("结果:%s\n",errstr);
    return 0;
}

 

posted on   Muniar  阅读(408)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示