当前位置: 技术问答>linux和unix
Linux下的一个C程序,获取子进程的时间与内存状况等信息。
来源: 互联网 发布时间:2016-09-07
本文导语: 导师给我说让我用 fork 和 getrusage这两个系统调用。 可我C学的不好,unix就没用过。 大家能给我写段简单的例子么。 就是用getrusage取的子进程的时间等信息。 | include ... int who = RUSAGE_SELF; struct rusage...
导师给我说让我用 fork 和 getrusage这两个系统调用。
可我C学的不好,unix就没用过。
大家能给我写段简单的例子么。
就是用getrusage取的子进程的时间等信息。
可我C学的不好,unix就没用过。
大家能给我写段简单的例子么。
就是用getrusage取的子进程的时间等信息。
|
include
...
int who = RUSAGE_SELF;
struct rusage usage;
int ret;
ret = getrusage(who, &usage);
这里,usage就是用于存储你获得的信息。
主要是参数who.
If the value of the who argument is RUSAGE_SELF, information shall be returned about resources used by the current process. If the value of the who argument is RUSAGE_CHILDREN, information shall be returned about resources used by the terminated and waited-for children of the current process. If the child is never waited for (for example, if the parent has SA_NOCLDWAIT set or sets SIGCHLD to SIG_IGN), the resource information for the child process is discarded and not included in the resource information provided by getrusage().
当 who 是RUSAGE_SELF,返回的是当前进程的信息。
当who 是RUSAGE_CHILDREN.则返回的是当前进程的终止掉的子进程和等待的子进程。
下面是rusage的结构:
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss;
#define ru_first ru_ixrss
long ru_ixrss; /* XXX: 0 */
long ru_idrss; /* XXX: sum of rm_asrss */
long ru_isrss; /* XXX: 0 */
long ru_minflt; /* any page faults not requiring I/O */
long ru_majflt; /* any page faults requiring I/O */
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 " */
#define ru_last ru_nivcsw
};
...
int who = RUSAGE_SELF;
struct rusage usage;
int ret;
ret = getrusage(who, &usage);
这里,usage就是用于存储你获得的信息。
主要是参数who.
If the value of the who argument is RUSAGE_SELF, information shall be returned about resources used by the current process. If the value of the who argument is RUSAGE_CHILDREN, information shall be returned about resources used by the terminated and waited-for children of the current process. If the child is never waited for (for example, if the parent has SA_NOCLDWAIT set or sets SIGCHLD to SIG_IGN), the resource information for the child process is discarded and not included in the resource information provided by getrusage().
当 who 是RUSAGE_SELF,返回的是当前进程的信息。
当who 是RUSAGE_CHILDREN.则返回的是当前进程的终止掉的子进程和等待的子进程。
下面是rusage的结构:
struct rusage {
struct timeval ru_utime; /* user time used */
struct timeval ru_stime; /* system time used */
long ru_maxrss;
#define ru_first ru_ixrss
long ru_ixrss; /* XXX: 0 */
long ru_idrss; /* XXX: sum of rm_asrss */
long ru_isrss; /* XXX: 0 */
long ru_minflt; /* any page faults not requiring I/O */
long ru_majflt; /* any page faults requiring I/O */
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 " */
#define ru_last ru_nivcsw
};
|
呵呵。貌似很正常,这个东西。
现在学校就是这样。导师一般都会很突兀的整个貌似很高深的课题,给学生。
想当年,我刚刚那个接触嵌入式的时候,嵌入式第一节课,老师啥技术也没讲。
然后下课就说:同学们,给大家布置个作业,就是在linux下编译RTEMS,然后把它在PC的虚拟机上启动起来。然后把里面的挖地雷游戏也编译了能够运行
那才叫汗。。。。
连linux都是第一天接触。RTEMS,什么都不知道,貌似当时还不晓得什么叫交叉编译。
额的神呀。。。
|
STFW………………
在Linux下man文档是很好的资源啊
在Linux下man文档是很好的资源啊