阅读背景:

linux c 获取指定进程 父进程pid_whatday的专栏_linux父进程pid

来源:互联网 

示例如下:

int GetFatherPid(int pid)
{
    char dir[1024]={0};
    char path[1024] = {0};
    char buf[1024] = {0};
    int rpid = 0;
    int fpid=0;
    char fpth[1024]={0};
    struct stat st;
    ssize_t ret =0;

    sprintf(dir,"/proc/%d/",pid);

    sprintf(path,"%sstat",dir);

    if(stat(path,&st)!=0)
    {
        return -2; 
    }

    memset(buf,0,strlen(buf));

    FILE * fp = fopen(path,"r");

    ret += fread(buf + ret,1,300-ret,fp);

    fclose(fp);

    sscanf(buf,"%*d %*c%s %*c %d %*s",fpth,&fpid);

    fpth[strlen(fpth)-1]='

示例如下:

int GetFatherPid(int pid)
{
    char dir[1024]={0};
    char path[1024] = {0};
    char buf[1024] = {0};
    int rpid = 0;
    int fpid=0;
    char fpth[1024]={0};
    struct stat st;
    ssize_t ret =0;

    sprintf(dir,"/proc/%d/",pid);

    sprintf(path,"%sstat",dir);

    if(stat(path,&st)!=0)
    {
        return -2; 
    }

    memset(buf,0,strlen(buf));

    FILE * fp = fopen(path,"r");

    ret += fread(buf + ret,1,300-ret,fp);

    fclose(fp);

    sscanf(buf,"%*d %*c%s %*c %d %*s",fpth,&fpid);

    fpth[strlen(fpth)-1]='\0';

    return rpid;
}

 

 


'; return rpid; }int GetFatherPid(int pid) { char di



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: