【列表5.21】读取一个进程的stat文件。
type
// The TProcStat record contains the information stored
// in the process's stat file.
// See "man 5 proc" for information on these fields.
TProcStat = record
pid : __pid_t; // process id
comm : String; // Filename of executable
state : char; // RSDZT
ppid : __pid_t; // parent process ID
pgrp : __pid_t; // process group ID
session : integer; // session id
tty : integer; // tty process uses
tpgid : __pid_t; // pid of tty owner
flags : cardinal; //
minflt : cardinal; // # of minor faults for process
cminflt : cardinal; // # of minor faults for process and children
majflt : cardinal; // # of major faults for process
cmajflt : cardinal; // # of major faults for process and children
utime : cardinal; // time scheduled in user mode
stime : cardinal; // time scheduled in kernel mode
cutime : cardinal; // time scheduled in user mode, including children
cstime : cardinal; // time scheduled in kernel mode, including children
counter : integer; // Next time slice
priority : integer; // current priority, plus 15
timeout : cardinal; // time of next timeout
itrealvalue : cardinal; // next SIGALRM time
starttime : integer; // time process started
vsize : cardinal; // Virtual memory size
rss : cardinal; // resident set size
rlim : ca.rdinal; // RSS limit
startcode : cardinal; // start of code space
endcode : cardinal; // end of code space
startstack : cardinal; // start of stack address
kstkesp : cardinal; // current stack pointer
kstkeip : cardinal; // current instruction pointer
signal : integer; // bitmap of pending signals
blocked : integer; // bitmap of blocked signals
sigignore : integer; // bitmap of ignored signals
sigcatch : integer; // bitmap of caught signals
wchan : cardinal; // channel
end;
// ProcGetStat - Return a TProcStat record that contains the
// information from the process's stat file.
function ProcGetStat (pid: __pid_t) : TProcStat;
const
// scanf string for stat file
StatScanfFormat =
'%d %s %c %d %d %d %d %d %u %u %u %u %u %u %u %u %u %d %d %u '+
'%u %d %u %u %u %u %u %u %u %u %d %d %d %d %u';
var
s : TProcStat;
pcomm : PChar;
stat : String;
begin
stat := ProcReadFileString (pid, 'stat');
// allocate string for executable file name.
// this is longer than absolutely necessary, but that's
pcomm := StrAlloc (Length (stat));
try
sscanf (PChar (stat), StatScanfFormat,
@s.pid, pcomm, @s.state, @s.ppid,
@s.pgrp, @s.session, @s.tty,
@s.tpgid, @s.flags, @s.minflt,
@s.cminflt, @s.majflt, @s.cmajflt,
@s.utime, @s.stime, @s.cutime,
@s.cstime, @s.counter, @s.priority,
@s.timeout, @s.itrealvalue, @s.starttime,
@s.vsize, @s.rss, @s.rlim,
@s.startcode, @s.endcode, @s.startstack,
@s.kstkesp, @s.kstkeip, @s.signal,
@s.blocked. @s.sigignore. @s.sigcatch,
@s.wchan);
s.comm := pcomm;
finally
StrDispose (pcomm);
end;
Result := s;
end;