文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>通过进程名得到进程ID

通过进程名得到进程ID

时间:2009-05-14  来源:vicegod

static pid_t getpidbyname (char *name)
{
        DIR           *dirHandle;  /* 目录句柄   */
        struct dirent *dirEntry;   /* 单个目录项 */
        psinfo_t      prp;
        pid_t         pid = -1;
        char          strPathName[100];
        FILE          *fp;

        memset(strPathName, 0, 100);

        if( ( dirHandle = opendir( "/proc" ) ) == NULL )
        {
                return( -1 );
        }
        chdir( "/proc" );
        /* 下面使用相对路径打开文件,所以必须进入/proc */
        while( ( dirEntry = readdir( dirHandle ) ) != NULL )
        {
                if( dirEntry->;d_name[0] != '.' && strcmp(dirEntry->;d_name,"sys") != 0)
                {
                        /* 拼加路径 */
                        sprintf(strPathName, "./%s/%s", dirEntry->;d_name, "psinfo");
                        /* 读取文件 */
                        if( ( fp = fopen( strPathName, "rb" ) ) == NULL )
                        {
                                printf("打开文件[%s]失败!\n", strPathName);
                                break;
                        }

                        /* 读取数据 */
                        if(fread(&prp, sizeof(psinfo_t), 1, fp) == -1)
                        {
                                printf("文件读取失败!\n");
                                fclose(fp);
                                break;
                        }
                        if ( strcmp( prp.pr_fname, name) == 0 )
                        {
                                pid = ( pid_t )atoi( dirEntry->;d_name );
                                break;
                        }
                        fclose( fp );
                }
        }  /* end of while */
        closedir( dirHandle );
        return( pid );
}  /* end of getpidbyname */
相关阅读 更多 +
排行榜 更多 +
山雾搜剧 1.0.0

山雾搜剧 1.0.0

系统软件 下载
漫千绘

漫千绘

浏览阅读 下载
快找资源plu

快找资源plu

系统软件 下载