如何取得进程及其子进程的CPU资源

ys_wang168 2008-10-18 12:07:08
问题: 在进程中需要调用其它程序,统计该程序的CPU时间。
使用下面的几种方法,得到不同的结果:
(1) if ((pid=fork())==0)
{
if (execv(command, NULL)==-1)
cout<<"child process exec error."<<endl;
exit(0);
}
else if (pid>0)
{
wait3(&status,0,&ru);
if (limit.statics) PrintStatics(ru, true);
}
输出ru的用户时间和系统时间
(2)用时钟差:
clock_t _timer=clock();
if ((pid=fork())==0)
{
if (execv(command, NULL)==-1)
cout<<"child process exec error."<<endl;
exit(0);
}
else if (pid>0)
{
wait3(&status,0,&ru);
if (limit.statics) PrintStatics(ru, true);
}

clock_t ticks = clock () - _timer;
long s = ticks / CLOCKS_PER_SEC;
_sec += s;
_msec += (ticks - s*CLOCKS_PER_SEC)*1000/CLOCKS_PER_SEC;
if (_msec >= 1000)
{
_msec -= 1000;
_sec++;
}
输出s
(3) 使用time命令:
time my_program
这几种方法的输出结果不一样。其中(2)输出的都是0 (1)输出的时间比(3)要多

...全文
203 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yswang168 2008-11-13
  • 打赏
  • 举报
回复
你们能告诉我,linux提供的time命令的算法吗?即输出的到底是否包含其子进程的时间?

man time
--------
NAME
time - time a simple command or give resource usage

SYNOPSIS
time [options] command [arguments...]

DESCRIPTION
The time command runs the specified program command with the given
arguments. When command finishes, time writes a message to standard
output giving timing statistics about this program run. These statis-
tics consist of (i) the elapsed real time between invocation and termi-
nation, (ii) the user CPU time (the sum of the tms_utime and tms_cutime
values in a struct tms as returned by times(2)), and (iii) the system
CPU time (the sum of the tms_stime and tms_cstime values in a struct
tms as returned by times(2)).
-----------------

man 2 times
------------
NAME
times - get process times

SYNOPSIS
#include <sys/times.h>

clock_t times(struct tms *buf);

DESCRIPTION
The times() function stores the current process times in the struct tms
that buf points to. The struct tms is as defined in <sys/times.h>:

struct tms {
clock_t tms_utime; /* user time */
clock_t tms_stime; /* system time */
clock_t tms_cutime; /* user time of children */
clock_t tms_cstime; /* system time of children */
};

The tms_utime field contains the CPU time spent executing instructions
of the calling process. The tms_stime field contains the CPU time
spent in the system while executing tasks on behalf of the calling pro-
cess. The tms_cutime field contains the sum of the tms_utime and
tms_cutime values for all waited-for terminated children. The
tms_cstime field contains the sum of the tms_stime and tms_cstime val-
ues for all waited-for terminated children.

Times for terminated children (and their descendants) is added in at
the moment wait(2) or waitpid(2) returns their process ID. In particu-
lar, times of grandchildren that the children did not wait for are
never seen.

All times reported are in clock ticks.


romandion 2008-10-29
  • 打赏
  • 举报
回复
//文件名:t4.c ;平台linux


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

#include <time.h>

int main()
{
int wpid , pid ,status ;
int i ;
struct rusage ru;

clock_t t ;
clockid_t clockid ;
struct timespec tsb , tse ;

t = clock() ;
if((pid = fork()) == 0)
{
for(i = 0 ; i < 1000000000 ; i++) ;

exit(0) ;
}
else if (pid > 0)
{
clock_getcpuclockid(pid , &clockid) ;
clock_gettime(clockid , &tsb) ;

wpid = wait3(&status , 0 , &ru) ;

clock_gettime(clockid , &tse) ;

printf("clock_gettime , sec = %d , nsec = %d \n" , tse.tv_sec - tsb.tv_sec , tse.tv_nsec - tsb.tv_nsec) ;

printf("wait3 pid = %d , user[sec = %d , usec = %d] sys[sec = %d , usec = %d] \n" ,
wpid , ru.ru_utime.tv_sec , ru.ru_utime.tv_usec , ru.ru_stime.tv_sec , ru.ru_stime.tv_usec) ;
}

t = clock() - t ;
printf("clock = %d\n" , t) ;

return 0 ;
}

/*--------------------------------文件结束-------------------------*/

$ gcc -o t4 t4.c -lrt
$ time t4
clock_gettime , sec = 2 , nsec = 57972000
wait3 pid = 12020 , user[sec = 2 , usec = 61686] sys[sec = 0 , usec = 2999]
clock = 0

real 0m2.067s
user 0m2.062s
sys 0m0.005s


从上面的执行结果上,除了使用clock,其他结果都很接近。


现在,我们修改下代码:

clock_getcpuclockid(pid , &clockid) ;
clock_gettime(clockid , &tsb) ;

for(i = 0 ; i < 500000000 ; i++) ;

wpid = wait3(&status , 0 , &ru) ;

执行如下:
$ time t4
clock_gettime , sec = 3 , nsec = 81670000
wait3 pid = 12226 , user[sec = 2 , usec = 63686] sys[sec = 0 , usec = 0]
clock = 1020000

real 0m3.098s
user 0m3.094s
sys 0m0.002s


发现,除了wait3外,其他各种计算方法的时间都增加1半,所以,我推断wait3才是真正计算子进程的时间,而其他几种算法都包含了父进程的时间。最明显的证据是clock = 1020000
ys_wang168 2008-10-27
  • 打赏
  • 举报
回复
# man wait3

pid_t wait3(int *status, int options,
struct rusage *rusage);
.......
If rusage is not NULL, the struct rusage as defined in <sys/resource.h>
it points to will be filled with accounting information. See
getrusage(2) for details.


#man getrusage
int getrusage(int who, struct rusage *usage);
...
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 */
};

输出就是ru结构所指的 user time 及system time
romandion 2008-10-20
  • 打赏
  • 举报
回复
dog250说得对,clock是统计当前进程的CPU时间,所以要计算子进程的CPU使用时间,在父进程中使用clock是没有用的。
需要通过2个函数来解决:
1、int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
获取子进程的CLOCK ID号

2、int clock_gettime(clockid_t clock_id, struct timespec *tp);
获取子进程的时间

后面的程序就简单。试试看,这种方法应该最准确了。
dog250 2008-10-20
  • 打赏
  • 举报
回复
clock函数用来统计当前进程使用cpu的时间,在你的方式2中,父进程从fork返回马上就wait3了,而wait导致父进程睡眠,睡眠是不使用cpu的,当然这一段时间就没有加到计时里了,所以你的输出全为0;至于1,我没有看到你是怎么输出的,请给出完全的代码

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧