【linux】 /proc/PID/stat

//proc/9744$ cat statushtml

Name: gedit /*進程的程序名*/
State: S (sleeping) /*進程的狀態信息,具體參見http://blog.chinaunix.net/u2/73528/showart_1106510.html*/
Tgid: 9744 /*線程組號*/
Pid: 9744 /*進程pid*/
PPid: 7672 /*父進程的pid*/
TracerPid: 0 /*跟蹤進程的pid*/
Uid: 1000    1000    1000    1000 /*uid euid suid fsuid*/
Gid: 1000    1000    1000    1000 /*gid egid sgid fsgid*/
FDSize: 256 /*文件描述符的最大個數,file->fds*/
Groups: 0 4 20 24 25 29 30 44 46 107 109 115 124 1000 /*啓動該進程的用戶所屬的組的id*/
VmPeak: 60184 kB /*進程地址空間的大小*/
VmSize: 60180 kB /*進程虛擬地址空間的大小reserved_vm:進程在預留或特殊的內存間的物理頁*/
VmLck: 0 kB /*進程已經鎖住的物理內存的大小.鎖住的物理內存不能交換到硬盤*/
VmHWM: 18020 kB /*文件內存映射和匿名內存映射的大小*/
VmRSS: 18020 kB /*應用程序正在使用的物理內存的大小,就是用ps命令的參數rss的值 (rss)*/
VmData: 12240 kB /*程序數據段的大小(所佔虛擬內存的大小),存放初始化了的數據*/
VmStk: 84 kB /*進程在用戶態的棧的大小*/
VmExe: 576 kB /*程序所擁有的可執行虛擬內存的大小,代碼段,不包括任務使用的庫 */
VmLib: 21072 kB /*被映像到任務的虛擬內存空間的庫的大小*/
VmPTE: 56 kB /*該進程的全部頁表的大小*/
Threads: 1 /*共享使用該信號描述符的任務的個數*/
SigQ: 0/8183 /*待處理信號的個數/目前最大能夠處理的信號的個數*/
SigPnd: 0000000000000000 /*屏蔽位,存儲了該線程的待處理信號*/
ShdPnd: 0000000000000000 /*屏蔽位,存儲了該線程組的待處理信號*/
SigBlk: 0000000000000000 /*存放被阻塞的信號*/
SigIgn: 0000000000001000 /*存放被忽略的信號*/
SigCgt: 0000000180000000 /*存放被俘獲到的信號*/
CapInh: 0000000000000000 /*能被當前進程執行的程序的繼承的能力*/
CapPrm: 0000000000000000 /*進程可以使用的能力,能夠包含CapEff中沒有的能力,這些能力是被進程本身臨時放棄的*/
CapEff: 0000000000000000 /*是CapPrm的一個子集,進程放棄沒有必要的能力有利於提升安全性*/
Cpus_allowed: 01 /*能夠執行該進程的CPU掩碼集*/
Mems_allowed: 1 /**/
voluntary_ctxt_switches: 1241 /*進程主動切換的次數*/
nonvoluntary_ctxt_switches: 717 /*進程被動切換的次數*/安全

/proc/PID/stat各個字段的描述session

Is there a pgm that will interpret all the fields that are printed by
        
        cat /proc/PID/stat ( or statm, or any of the info on a per process basis
        )

See if the following simple function and the associated structure
that I put together sometime ago while checking out some threads-
related stuff on Linux/Alpha is of use. Also note that the format 
characters as given in the man page for proc are not all correct 
on Alpha. I just when to the sources to get them right (RTFS?:-))

typedef struct statstruct_proc {
  一、int           pid;                      /** The process id. **/
  二、char          exName [_POSIX_PATH_MAX]; /** The filename of the executable **/
  三、char          state; /** 1 **/          /** R is running, S is sleeping, 
			   D is sleeping in an uninterruptible wait,
			   Z is zombie, T is traced or stopped **/
  四、unsigned      euid,                      /** effective user id **/
                egid;                      /** effective group id */					     
  五、int           ppid;                     /** The pid of the parent. **/
  六、int           pgrp;                     /** The pgrp of the process. process group**/
  七、int           session;                  /** The session id of the process. **/
  八、int           tty;                      /** The tty the process uses **/
  九、int           tpgid;                    /** (too long) **/
  十、unsigned int	flags;                    /** The flags of the process. **/
  十一、unsigned int	minflt;                   /** The number of minor faults **/
  十二、unsigned int	cminflt;                  /** The number of minor faults with childs **/
  1三、unsigned int	majflt;                   /** The number of major faults **/
  1四、unsigned int  cmajflt;                  /** The number of major faults with childs **/
  1五、int           utime;                    /** user mode jiffies **/
  1六、int           stime;                    /** kernel mode jiffies **/
  1七、int		    cutime;                   /** user mode jiffies with childs **/
  1八、int           cstime;                   /** kernel mode jiffies with childs **/
  1九、int           counter;                  /** process's next timeslice **/
  20、int           priority;                 /** the standard nice value, plus fifteen **/
  2一、unsigned int  timeout;                  /** The time in jiffies of the next timeout **/
  2二、unsigned int  itrealvalue;              /** The time before the next SIGALRM is sent to the process **/
  2三、int           starttime; /** 20 **/     /** Time the process started after system boot **/
  2四、unsigned int  vsize;                    /** Virtual memory size **/
  2五、unsigned int  rss;                      /** Resident Set Size **/
  2六、unsigned int  rlim;                     /** Current limit in bytes on the rss **/
  2七、unsigned int  startcode;                /** The address above which program text can run **/
  2八、unsigned int	endcode;                  /** The address below which program text can run **/
  2九、unsigned int  startstack;               /** The address of the start of the stack **/
  30、unsigned int  kstkesp;                  /** The current value of ESP **/
  3一、unsigned int  kstkeip;                 /** The current value of EIP **/
  3二、int		signal;                   /** The bitmap of pending signals **/
  3三、int           blocked; /** 30 **/       /** The bitmap of blocked signals **/
  3四、int           sigignore;                /** The bitmap of ignored signals **/
  3五、int           sigcatch;                 /** The bitmap of catched signals **/
  3六、unsigned int  wchan;  /** 33 **/        /** (too long) **/
  3七、int		sched, 		  /** 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;
}
相關文章
相關標籤/搜索