/proc/<pid>/stat各个字段的描述

本文介绍如何解析Linux系统中/proc/pid/stat文件,详细解释了该文件中的各个字段含义及其作用,并提供了一个C语言示例程序,用于获取指定进程的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

typedef struct statstruct_proc {
  int           pid;                      /** 进程 id. **/
  char          exName [_POSIX_PATH_MAX]; /** 可执行文件名**/
  char          state; /** 1 **/          /** R 代表正在运行, S 代表正在休眠, D 代表正处于不可中断的等待状态下的休眠,Z 代表进程未响应, T 代表进程已停止**/
  unsigned      euid,                      /** 有效的用户 id **/
                egid;                      /** 有效的组 id */     
  int           ppid;                     /** 父进程的 pid. **/
  int           pgrp;                     /** The pgrp of the process. **/
  int           session;                  /** 该进程的 session **/
  int           tty;                      /** The tty the process uses **/
  int           tpgid;                    /** (too long) **/
  unsigned intflags;                    /** The flags of the process. **/
  unsigned intminflt;                   /** The number of minor faults **/
  unsigned intcminflt;                  /** The number of minor faults with childs **/
  unsigned intmajflt;                   /** The number of major faults **/
  unsigned int  cmajflt;                  /** The number of major faults with childs **/
  int           utime;                    /** user mode jiffies **/
  int           stime;                    /** kernel mode jiffies **/
  intcutime;                   /** user mode jiffies with childs **/
  int           cstime;                   /** kernel mode jiffies with childs **/
  int           counter;                  /** process's next timeslice **/
  int           priority;                 /** the standard nice value, plus fifteen **/
  unsigned int  timeout;                  /** The time in jiffies of the next timeout **/
  unsigned int  itrealvalue;              /** The time before the next SIGALRM is sent to the process **/
  int           starttime; /** 20 **/     /** Time the process started after system boot **/
  unsigned int  vsize;                    /** Virtual memory size **/
  unsigned int  rss;                      /** Resident Set Size **/
  unsigned int  rlim;                     /** Current limit in bytes on the rss **/
  unsigned int  startcode;                /** The address above which program text can run **/
  unsigned intendcode;                  /** The address below which program text can run **/
  unsigned int  startstack;               /** The address of the start of the stack **/
  unsigned int  kstkesp;                  /** The current value of ESP **/
  unsigned int  kstkeip;                 /** The current value of EIP **/
  intsignal;                   /** The bitmap of pending signals **/
  int           blocked; /** 30 **/       /** The bitmap of blocked signals **/
  int           sigignore;                /** The bitmap of ignored signals **/
  int           sigcatch;                 /** The bitmap of catched signals **/
  unsigned int  wchan;  /** 33 **/        /** (too long) **/
  intsched,   /** scheduler **/
                sched_priority;  /** scheduler priority **/

} procinfo;
int get_proc_info(pid_t pid, procinfo * pinfo)
{
  char szFileName [_POSIX_PATH_MAX],
    szStatStr [2048],
    *s, *t;
  FILE *fp;
  struct stat st;
  
  if (NULL == pinfo) {
    errno = EINVAL;
    return -1;
  }

  sprintf (szFileName, "/proc/%u/stat", (unsigned) pid);
  
  if (-1 == access (szFileName, R_OK)) {
    return (pinfo->pid = -1);
  } /** if **/

  if (-1 != stat (szFileName, &st)) {
  pinfo->euid = st.st_uid;
  pinfo->egid = st.st_gid;
  } else {
  pinfo->euid = pinfo->egid = -1;
  }
  
  
  if ((fp = fopen (szFileName, "r")) == NULL) {
    return (pinfo->pid = -1);
  } /** IF_NULL **/
  
  if ((s = fgets (szStatStr, 2048, fp)) == NULL) {
    fclose (fp);
    return (pinfo->pid = -1);
  }

  /** pid **/
  sscanf (szStatStr, "%u", &(pinfo->pid));
  s = strchr (szStatStr, '(') + 1;
  t = strchr (szStatStr, ')');
  strncpy (pinfo->exName, s, t - s);
  pinfo->exName [t - s] = '';
  
  sscanf (t + 2, "%c %d %d %d %d %d %u %u %u %u %u %d %d %d %d %d %d %u %u %d %u %u %u %u %u %u %u %u %d %d %d %d %u",
  /*       1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33*/
  &(pinfo->state),
  &(pinfo->ppid),
  &(pinfo->pgrp),
  &(pinfo->session),
  &(pinfo->tty),
  &(pinfo->tpgid),
  &(pinfo->flags),
  &(pinfo->minflt),
  &(pinfo->cminflt),
  &(pinfo->majflt),
  &(pinfo->cmajflt),
  &(pinfo->utime),
  &(pinfo->stime),
  &(pinfo->cutime),
  &(pinfo->cstime),
  &(pinfo->counter),
  &(pinfo->priority),
  &(pinfo->timeout),
  &(pinfo->itrealvalue),
  &(pinfo->starttime),
  &(pinfo->vsize),
  &(pinfo->rss),
  &(pinfo->rlim),
  &(pinfo->startcode),
  &(pinfo->endcode),
  &(pinfo->startstack),
  &(pinfo->kstkesp),
  &(pinfo->kstkeip),
  &(pinfo->signal),
  &(pinfo->blocked),
  &(pinfo->sigignore),
  &(pinfo->sigcatch),
  &(pinfo->wchan));
  fclose (fp);
  return 0;
}







转载于:https://my.oschina.net/shou1156226/blog/648300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值