LINUX下如何获取当前进程的占用页数和使用的内存区间数
现需要以代码的形式将当前进程的信息某些信息输入至proc目录下的某个自建的文件下,其中进程的pid和ppid等信息均有线程的get函数可以直接使用,但请问如何得到当前进程所占用的页数和使用的内存区间数?
------解决方案--------------------想知道占页数。。。
其实有一个系统调用: getrusage 可以做到
Synopsis
#include <sys/time.h>
#include <sys/resource.h>
int getrusage(int who, struct rusage *usage);
Description
getrusage() returns current resource usages, for a who of either RUSAGE_SELF or RUSAGE_CHILDREN. The former asks for resources used by the current process, the latter for resources used by those of its children that have terminated and have been waited for.
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims */
long ru_majflt; /* page faults */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* messages sent */
long ru_msgrcv; /* messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
Return Value
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
Errors
EFAULT
usage points outside the accessible address space.
EINVAL
who is invalid.