linux c 获取指定进程 父进程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]='\0';
return rpid;
}
还没有评论,来说两句吧...