2015年7月19日 星期日

[C] Get PID of process name


int find_pid_by_name( char* ProcName, int* foundpid)  
{  
        DIR             *dir;  
        struct dirent   *d;  
        int             pid, i;  
        char            *s;  
        int pnlen;  

        i = 0;  
        foundpid[0] = 0;  
        pnlen = strlen(ProcName);  

        /* Open the /proc directory. */  
        dir = opendir("/proc");  
        if (!dir)  
        {  
                printf("cannot open /proc");  
                return -1;  
        }  

        /* Walk through the directory. */  
        while ((d = readdir(dir)) != NULL) {  

                char exe [PATH_MAX+1];  
                char path[PATH_MAX+1];  
                int len;  
                int namelen;  

                /* See if this is a process */  
                if ((pid = atoi(d->d_name)) == 0)       continue;  

                snprintf(exe, sizeof(exe), "/proc/%s/exe", d->d_name);  
                if ((len = readlink(exe, path, PATH_MAX)) < 0)  
                        continue;  
                path[len] = '\0';  

                /* Find ProcName */  
                s = strrchr(path, '/');  
                if(s == NULL) continue;  
                s++;  

                /* we don't need small name len */  
                namelen = strlen(s);  
                if(namelen < pnlen)     continue;  

                if(!strncmp(ProcName, s, pnlen)) {  
                        /* to avoid subname like search proc tao but proc taolinke matched */  
                        if(s[pnlen] == ' ' || s[pnlen] == '\0') {  
                                foundpid[i] = pid;  
                                i++;  
                        }  
                }  
        }  

        foundpid[i] = 0;  
        closedir(dir);  

        return  0;  

}  
Reference:
  1. linux 下根据进程名字获取进程的进程号PID,类似pidof,C函数
  2. Linux and Unix readlink command

0 意見:

張貼留言