linux下根據進程名字擷取PID,類似pidof

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   使用   io   for   ar   

 

 

linux有一個命令列工具叫做pidof,可以根據使用者輸入的進程名字尋找到進程號,但有時候我們需要在程式裡實現,不想調用system,在查閱了很多版本的pidof原始碼後,沒有發現一個自己感覺比較好的,所以就參照linux上的pidof的原始碼,改寫出了一版,供大家參考使用。

 

/**************************************************************************** File name    :        findpidbyname.c* Function     :        like pidof* Author       :        [email protected]* Date         :        2012/12/* Version      :             v1.0* Description  :           Find process‘s pid by name in linux.* ModifyRecord :****************************************************************************/#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <stdlib.h>#include <string.h>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;}int main(int argc, char *argv[]){        int i, rv, pid_t[128];        if ( argc != 2 )        {                fprintf(stdout,"Usage %s procname\n",argv[0]);                return 0;        }        rv = find_pid_by_name( argv[1], pid_t);        if(!rv) {                for(i=0; pid_t[i] != 0; i++)                        printf("%d\n", pid_t[i]);        }        return 0;}

 

linux下根據進程名字擷取PID,類似pidof(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.