这些获取资源使用的方法有什么区别?
What is the difference between these methods to obtain resource usage?
在Linux中,我们可以使用两种方式找出使用的资源,如时间、页面错误、页面交换、上下文切换。一种方法是使用 getrusage() 函数,另一种方法是使用命令 /usr/bin/time -v [command to check usage].这些查找资源使用情况的方法有什么区别?
当您使用像 time(1) it must use a system call such as getrusage(2) by way of its system library wrapper. This is building a request with the right system call number 这样的命令和结构来指示它需要进程子进程的使用信息时。
为了在 UNIX/POSIX 操作系统之间兼容,选择哪些特定功能来构建命令是根据选项层次结构完成的,以充分涵盖命令运行的操作系统。 (某些操作系统可能无法实现所有功能或有各种怪癖。)
在time's case it will prefer to group waiting for the child and getting its usage into calling wait3 which in turn is implemented as a wrapper around the even more complex wait4, which has its own systemcall number.
wait3/4 和 getrusage 都使用相同的 usage 结构填充信息,并且由于时间只直接调用一个子进程,调用 wait3()
或将其分解为功能较少的调用,如 wait();getrusage(RUSAGE_CHILDREN)
本质上是一样的。 因此,时间有效地显示了与 getrusage 提供的相同数据(连同它从系统收集的一些更通用的数据,例如使用调用 gettimeofday
的实时流逝)。
系统调用包装函数之间的真正区别是:
- getrusage 有另一个参数允许进程查看自己到目前为止。
- wait4 可以只针对一个直系子女和该子女的后代。
- wait3 是对 wait4 或 using
wait();getrusage()
的简化,它的通用性不如两者,但足以满足 time(1) 命令的实现。 (因此 wait3 是在可用的操作系统上使用时间的最简单和最安全的选项。)
要验证它们是否相同,可以将 time
更改为替代版本,重新编译并比较:
while ((caught = wait3 (&status, 0, NULL)) != pid)
{
if (caught == -1) {
getrusage(RUSAGE_CHILDREN, &resp->ru);
return 0;
}
}
在Linux中,我们可以使用两种方式找出使用的资源,如时间、页面错误、页面交换、上下文切换。一种方法是使用 getrusage() 函数,另一种方法是使用命令 /usr/bin/time -v [command to check usage].这些查找资源使用情况的方法有什么区别?
当您使用像 time(1) it must use a system call such as getrusage(2) by way of its system library wrapper. This is building a request with the right system call number 这样的命令和结构来指示它需要进程子进程的使用信息时。
为了在 UNIX/POSIX 操作系统之间兼容,选择哪些特定功能来构建命令是根据选项层次结构完成的,以充分涵盖命令运行的操作系统。 (某些操作系统可能无法实现所有功能或有各种怪癖。)
在time's case it will prefer to group waiting for the child and getting its usage into calling wait3 which in turn is implemented as a wrapper around the even more complex wait4, which has its own systemcall number.
wait3/4 和 getrusage 都使用相同的 usage 结构填充信息,并且由于时间只直接调用一个子进程,调用 wait3()
或将其分解为功能较少的调用,如 wait();getrusage(RUSAGE_CHILDREN)
本质上是一样的。 因此,时间有效地显示了与 getrusage 提供的相同数据(连同它从系统收集的一些更通用的数据,例如使用调用 gettimeofday
的实时流逝)。
系统调用包装函数之间的真正区别是:
- getrusage 有另一个参数允许进程查看自己到目前为止。
- wait4 可以只针对一个直系子女和该子女的后代。
- wait3 是对 wait4 或 using
wait();getrusage()
的简化,它的通用性不如两者,但足以满足 time(1) 命令的实现。 (因此 wait3 是在可用的操作系统上使用时间的最简单和最安全的选项。)
要验证它们是否相同,可以将 time
更改为替代版本,重新编译并比较:
while ((caught = wait3 (&status, 0, NULL)) != pid)
{
if (caught == -1) {
getrusage(RUSAGE_CHILDREN, &resp->ru);
return 0;
}
}