使用 procf//status 了解流程状态<pid>
Knowing the process status using procf/<pid>/status
我正在研究Solaris。
我知道如果有一个进程正在运行,就会有一个名为 /proc/<PID>/status
的文件,其中<PID>
是进程 ID,它包含一个名为 state
的字段。
例如,我使用了我的 shell 进程:
> ps
PID TTY TIME CMD
18671 0:01 tcsh
其进程 ID 为 18671。
我写了一个简单的C程序来提取这些信息:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/procfs.h>
#include <sys/fcntl.h>
static void get_status (pid_t pid)
{
char procpath[100];
char buf[100];
int pfd;
char State[100];
char Name[100];
prstatus_t * pms;
FILE *proc;
sprintf(procpath, "/proc/%d/status", pid);
proc = fopen(procpath,"r");
if (proc) {
printf("Open Successfuln");
fgets(buf,256,proc); sscanf(buf,"Name:t%s",Name);
fgets(buf,256,proc); sscanf(buf,"State:t%c",State);
}
printf("%s",Name);
printf("%s",State);
}
int main(int argc, char **argv)
{
get_status(18671);
}
它不产生任何输出:
> ./a.out
Open Successful
>
procfs 的在线材料说,我们可以简单地在proc/<pid>/status
上做一只猫并检查过程的状态。
但就我而言,它是一个二进制文件。 我从来没有看到它在任何地方提到它是二进制的。
有没有办法让我使用一个简单的 C 程序来获取当前进程的状态?
C++解决办法也是可以接受的。
这是
你应该从/proc/pid
/status 中读取的结构:
typedef struct pstatus {
int pr_flags; /* flags (see below) */
int pr_nlwp; /* number of active lwps in the process */
pid_t pr_pid; /* process id */
pid_t pr_ppid; /* parent process id */
pid_t pr_pgid; /* process group id */
pid_t pr_sid; /* session id */
id_t pr_aslwpid; /* historical; now always zero */
id_t pr_agentid; /* lwp id of the /proc agent lwp, if any */
sigset_t pr_sigpend; /* set of process pending signals */
uintptr_t pr_brkbase; /* address of the process heap */
size_t pr_brksize; /* size of the process heap, in bytes */
uintptr_t pr_stkbase; /* address of the process stack */
size_t pr_stksize; /* size of the process stack, in bytes */
timestruc_t pr_utime; /* process user cpu time */
timestruc_t pr_stime; /* process system cpu time */
timestruc_t pr_cutime; /* sum of children's user times */
timestruc_t pr_cstime; /* sum of children's system times */
sigset_t pr_sigtrace; /* set of traced signals */
fltset_t pr_flttrace; /* set of traced faults */
sysset_t pr_sysentry; /* set of system calls traced on entry */
sysset_t pr_sysexit; /* set of system calls traced on exit */
char pr_dmodel; /* data model of the process (see below) */
char pr_pad[3];
taskid_t pr_taskid; /* task id */
projid_t pr_projid; /* project id */
int pr_nzomb; /* number of zombie lwps in the process */
zoneid_t pr_zoneid; /* zone id */
int pr_filler[15]; /* reserved for future use */
lwpstatus_t pr_lwp; /* status of the representative lwp */
} pstatus_t;
请注意,它是在头文件 procfs.h 中定义的。声明一个pstatus_t
变量并将sizeof(pstatus_t)
字节读入该变量。
提示:也不能通过ls
获得,您也可以使用/proc/self/psinfo
来读取自我进程的psinfo。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 使用 VID、PID、+SN 查找 USB 端口号
- 对于四轴飞行器,PID中I控制器的理想值应该是多少
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- 两个进程可以通过跟踪附加到同一个 PID 吗?
- 如何获取进程的整数PID?
- Linux:获取孙子的pid C++
- 如何显式调用运算符<<
- 在 C 中获取背景外壳命令的 PID
- 通过 pid 窗口判断进程是否存在
- PID 不使用 fork() 定义类型
- 我想获取我在C++中创建的每个进程的 PID,并在特定时间后终止每个进程
- 从某些 PID C++ 开始
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<