惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
A
Arctic Wolf
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
Cyberwarzone
Cyberwarzone
博客园 - Franky
V2EX - 技术
V2EX - 技术
The Hacker News
The Hacker News
量子位
TaoSecurity Blog
TaoSecurity Blog
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
D
DataBreaches.Net
T
Troy Hunt's Blog
T
Threat Research - Cisco Blogs
美团技术团队
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
D
Docker
C
Check Point Blog
G
GRAHAM CLULEY
H
Heimdal Security Blog
IT之家
IT之家
博客园 - 叶小钗
The Cloudflare Blog
H
Help Net Security
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Archives - TechRepublic
Security Archives - TechRepublic
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
I
Intezer
P
Proofpoint News Feed
S
Security Affairs
P
Privacy International News Feed
U
Unit 42
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
Know Your Adversary
Know Your Adversary
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 飘啊飘

一个精简的vi源码(2000行) dtruss 粗糙的翻译 gdb常用命令[转] gdb中信号的处理[转] busybox0.60.3源码学习开始 pygame做的贪吃蛇 python中使用struct模块处理二进制数据 使用PYGAME开发的坦克游戏[代码][思路] 一篇很好的讲/etc/inittab的文章[转] 哲学家吃空心粉问题 《代码整洁之道》笔记之函数 使用面向对象概念优化条件判断语句的一个小应用 python生成文件树的代码 深入理解软件包的配置、编译与安装[转] pygame学习之对象移动 pywin32重启电脑 - 飘啊飘 - 博客园 解决LINUX和WINDOWS时间不一置 通过状态机实现的一个配置读取函数 UNIX基础知识--《APUE》第一章笔记
在linux中通过进程名获得进程id
飘啊飘 · 2012-02-21 · via 博客园 - 飘啊飘

当需要kil一个进程时,需要提供一个pid(使用kill命令)或提供一个进程名(使用pkill命令)。
pkill是如何通过进程名得到进程id的?
在linux中进程是通过文件来表示的,信息都存储在/proc/pid目录中。
在/proc/pid/status文件的第一行,保存有进程名,和用户输入的进行比对,如果一致,就添加到动态数组中,最后返回。


附代码:

long* find_pid_by_name( char* pidName)
{
	DIR *dir;
	struct dirent *next;
	long* pidList=NULL;
	int i=0;

        ///proc中包括当前的进程信息,读取该目录
	dir = opendir("/proc");
	if (!dir)
		perror_msg_and_die("Cannot open /proc");
	
        //遍历
	while ((next = readdir(dir)) != NULL) {
		FILE *status;
		char filename[READ_BUF_SIZE];
		char buffer[READ_BUF_SIZE];
		char name[READ_BUF_SIZE];

		/* 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 (! (status = fopen(filename, "r")) ) {
			continue;
		}
		if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
			fclose(status);
			continue;
		}
		fclose(status);

                //得到进程id
		/* Buffer should contain a string like "Name:   binary_name" */
		sscanf(buffer, "%*s %s", name);
		if (strcmp(name, pidName) == 0) {
			pidList=realloc( pidList, sizeof(long) * (i+2));
			pidList[i++]=strtol(next->d_name, NULL, 0);
		}
	}

	if (pidList) {
		pidList[i]=0;
	}
	return NULL;
}

 注:该代码在busybox0.6.3/libbb/find_pid_by_name.c