从/ proc /&LT获取PID等过程信息; PID> /状态 [英] Getting pid and other process information from /proc/<pid>/status

查看:155
本文介绍了从/ proc /&LT获取PID等过程信息; PID> /状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要得到一些信息(PID仅仅是一个例子,我知道它很容易得到它在许多其他方面)从 / proc /进程/状态

我试图做这种方式:

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&;&string.h中GT;
#包括LT&;&fcntl.h GT;
#包括LT&; SYS / procfs.h>
#包括LT&; SYS / signal.h中>
#包括LT&; SYS / syscall.h>
#包括LT&;了sys / param.h>诠释主(){
        焦炭BUF [BUFSIZ],缓冲[10];
        炭pathbase [20],pathdir [20];
        FILE * FP;
        prstatus_t状态;        的printf(进程ID:%d个\\ N,GETPID());
        的printf(父进程ID:%d个\\ N,getppid());
        的printf(组ID:%d个\\ N的getpgrp());
        的printf(会话ID:%d个\\ N,GETSID(0));        的strcpy(pathbase的/ proc /);
        的sprintf(缓冲区,%d个,GETPID());
        strcat的(pathbase,缓冲区);        的strcpy(pathdir,pathbase);
        strcat的(pathdir/状态);        如果((FP = FOPEN(pathdir,R))== NULL)PERROR(FOPEN);
        FREAD(安培;状态的sizeof(prstatus_t),1,FP);        的printf(PROCES ID:%d个\\ N,status.pr_pid);
        的printf(PROCES PPID数:%d \\ n,(INT)status.pr_ppid);
        FCLOSE(FP);
}

和它显然是错误的,导致的结果我得到的是:

 进程ID:5474
父进程ID:3781
组ID:5474
会话ID:3781
PROCES ID:1735289198
PROCES PPID:1733560873


解决方案

事情是的/ proc / [PID] /状态是一个文本文件。所以,你的 FREAD 正在复制文本到结构状态 - 所以一切看起来像废话

您可以逐行读取状态文件中的行,或者您可以使用 / proc中包含在一行相同的信息(/ [PID] / STAT 文件状态是供人食用,而统计是程序单耗)。要获取进程ID(或任何其他信息),你只需要记号化的单行。

I need to get some information (pid is just an example, i know its much easier to get it in many other ways) from /proc/PID/status

I have tried to do it this way:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <sys/procfs.h>
#include <sys/signal.h>
#include <sys/syscall.h>
#include <sys/param.h>

int main(){
        char buf[BUFSIZ], buffer[10];
        char pathbase[20], pathdir[20];
        FILE *fp;
        prstatus_t status;

        printf("Process ID: %d\n", getpid());
        printf("Parent process ID: %d\n", getppid());
        printf("Group ID: %d\n", getpgrp());
        printf("Session ID: %d\n", getsid(0));

        strcpy(pathbase,"/proc/");
        sprintf(buffer, "%d", getpid());
        strcat(pathbase, buffer);

        strcpy(pathdir, pathbase);
        strcat(pathdir,"/status");

        if((fp = fopen(pathdir, "r")) == NULL) perror("fopen");
        fread(&status, sizeof(prstatus_t), 1, fp);

        printf("Proces id: %d\n", status.pr_pid);
        printf("Proces ppid: %d\n", (int)status.pr_ppid);
        fclose(fp);
}

and it obviously wrong, cause the result i get is:

Process ID: 5474
Parent process ID: 3781
Group ID: 5474
Session ID: 3781
Proces id: 1735289198
Proces ppid: 1733560873

解决方案

The thing is /proc/[pid]/status is a text file. So your fread is copying text into the struct status - so everything will look like gibberish.

You could read the status file line by line or you could use the /proc/[pid]/stat file which contains the same information on a single line (status is for human consumption while stat is for program consumtion). To get the process id (or any other information) you would just have to tokenize that single line.

这篇关于从/ proc /&LT获取PID等过程信息; PID&GT; /状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆