<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://www.mywiki.cn/Hovercool/index.php?action=history&amp;feed=atom&amp;title=%E6%A0%B9%E6%8D%AE%E8%BF%9B%E7%A8%8B%E5%90%8D%E8%8E%B7%E5%8F%96PID</id>
	<title>根据进程名获取PID - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://www.mywiki.cn/Hovercool/index.php?action=history&amp;feed=atom&amp;title=%E6%A0%B9%E6%8D%AE%E8%BF%9B%E7%A8%8B%E5%90%8D%E8%8E%B7%E5%8F%96PID"/>
	<link rel="alternate" type="text/html" href="https://www.mywiki.cn/Hovercool/index.php?title=%E6%A0%B9%E6%8D%AE%E8%BF%9B%E7%A8%8B%E5%90%8D%E8%8E%B7%E5%8F%96PID&amp;action=history"/>
	<updated>2026-09-01T04:00:50Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://www.mywiki.cn/Hovercool/index.php?title=%E6%A0%B9%E6%8D%AE%E8%BF%9B%E7%A8%8B%E5%90%8D%E8%8E%B7%E5%8F%96PID&amp;diff=806&amp;oldid=prev</id>
		<title>2015年5月6日 (三) 08:31 Hovercool</title>
		<link rel="alternate" type="text/html" href="https://www.mywiki.cn/Hovercool/index.php?title=%E6%A0%B9%E6%8D%AE%E8%BF%9B%E7%A8%8B%E5%90%8D%E8%8E%B7%E5%8F%96PID&amp;diff=806&amp;oldid=prev"/>
		<updated>2015-05-06T08:31:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre class=&amp;quot;prettyprint&amp;quot;&amp;gt;&lt;br /&gt;
#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include&amp;lt;unistd.h&amp;gt;&lt;br /&gt;
#include&amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/time.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/resource.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/types.h&amp;gt;&lt;br /&gt;
#include&amp;lt;sys/stat.h&amp;gt;&lt;br /&gt;
#include&amp;lt;fcntl.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;utils/Log.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;dirent.h&amp;gt;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt; &lt;br /&gt;
&lt;br /&gt;
/*===start:for system_server monitor===*/&lt;br /&gt;
int findPidByName( char* pidName)&lt;br /&gt;
{&lt;br /&gt;
#define READ_BUF_SIZE (256)&lt;br /&gt;
    DIR *dir;&lt;br /&gt;
    struct dirent *next;&lt;br /&gt;
    int pid = 0;&lt;br /&gt;
    int i=0;&lt;br /&gt;
 &lt;br /&gt;
        ///proc中包括当前的进程信息,读取该目录&lt;br /&gt;
    dir = opendir(&amp;quot;/proc&amp;quot;);&lt;br /&gt;
    if (!dir)&lt;br /&gt;
        LOGE(&amp;quot;vivo_daemon,Cannot open /proc&amp;quot;);&lt;br /&gt;
     &lt;br /&gt;
    //遍历&lt;br /&gt;
    while ((next = readdir(dir)) != NULL) {&lt;br /&gt;
        FILE *status;&lt;br /&gt;
        char filename[READ_BUF_SIZE];&lt;br /&gt;
        char buffer[READ_BUF_SIZE];&lt;br /&gt;
        char name[READ_BUF_SIZE];&lt;br /&gt;
 &lt;br /&gt;
        /* Must skip &amp;quot;..&amp;quot; since that is outside /proc */&lt;br /&gt;
        if (strcmp(next-&amp;gt;d_name, &amp;quot;..&amp;quot;) == 0)&lt;br /&gt;
            continue;&lt;br /&gt;
 &lt;br /&gt;
        /* If it isn&amp;#039;t a number, we don&amp;#039;t want it */&lt;br /&gt;
        if (!isdigit(*next-&amp;gt;d_name))&lt;br /&gt;
            continue;&lt;br /&gt;
        //设置进程&lt;br /&gt;
        sprintf(filename, &amp;quot;/proc/%s/status&amp;quot;, next-&amp;gt;d_name);&lt;br /&gt;
        if (! (status = fopen(filename, &amp;quot;r&amp;quot;)) ) {&lt;br /&gt;
            continue;&lt;br /&gt;
        }&lt;br /&gt;
        if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {&lt;br /&gt;
            fclose(status);&lt;br /&gt;
            continue;&lt;br /&gt;
        }&lt;br /&gt;
        fclose(status);&lt;br /&gt;
 &lt;br /&gt;
        //得到进程id&lt;br /&gt;
        /* Buffer should contain a string like &amp;quot;Name:   binary_name&amp;quot; */&lt;br /&gt;
        sscanf(buffer, &amp;quot;%*s %s&amp;quot;, name);&lt;br /&gt;
        if (strcmp(name, pidName) == 0) {&lt;br /&gt;
            //pidList=(long*)realloc( pidList, sizeof(long) * (i+2));&lt;br /&gt;
            //pidList[i++]=strtol(next-&amp;gt;d_name, NULL, 0);&lt;br /&gt;
            pid = strtol(next-&amp;gt;d_name, NULL, 0);&lt;br /&gt;
            break;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    return pid;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
此函数摘自 http://kb.cnblogs.com/a/2360817/ ，不过为了使用更加方便有些许更改，主要是去掉了对同一进程名有多个pid的支持，改为获取到第一个pid则立刻返回，这样改的原因是android在很多状况下其进程都是惟一的，这样更改后搜索速度有所提升，且也更加易用！&lt;/div&gt;</summary>
		<author><name>Hovercool</name></author>
	</entry>
</feed>