• linux下根据进程名字获取PID,类似pidof(转)


    linux有一个命令行工具叫做pidof,可以根据用户输入的进程名字查找到进程号,但有时候我们需要在程序里实现,不想调用system,在查阅了很多版本的pidof源代码后,没有发现一个自己感觉比较好的,所以就参照linux上的pidof的源代码,改写出了一版,供大家参考使用。

    /***************************************************************************
    * File name    :        findpidbyname.c
    * Function     :        like pidof
    * Author       :        zhangzhao@tass.com.cn
    * 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] = '';
    
                    /* 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] == '') {
                                    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
    ",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
    ", pid_t[i]);
            }
    
            return 0;
    }
  • 相关阅读:
    《那些年,我们拿下FPGA》做笔记
    三种初始化
    hdu4417 Super Mario 树阵离线/划分树
    【设计模式】文章摘要 查找联系人控件
    STL set
    阐述 QUEST CENTRAL FOR DB2 八罪
    使用线程执行堆栈StackTraceElement设计Android日志模块
    苹果iOS苹果公司的手机用户都有权索赔
    Android 4.4 沉浸式透明状态栏与导航栏
    常见的几种RuntimeException
  • 原文地址:https://www.cnblogs.com/sanchrist/p/3939850.html
Copyright © 2020-2023  润新知