如何在c ++中计算进程?

时间:2014-02-13 19:00:07

标签: c++ eclipse timer

我正在开展一个项目,我要计划创建fork()和pthread_create()。我们应该通过使用系统调用来创建个性化的计时器类来计算执行每个任务所需的时间。为了获得帮助,教授告诉我们检查man -k时间。

我不熟悉使用系统调用和使用手册页来编写文档,所以我完全迷失了。到目前为止,我正在努力实现的代码是:

#include "Timer.h"
#include <iostream>
#include <signal.h>
#include <ctime>
#include <time.h>
using namespace std;
Timer::Timer() {
    timer_t * tid;
    timer_create(CLOCK_REALTIME, NULL, tid);
    int time = timer_gettime(tid, 0);
    cout<<time;
}

编译时,eclipse会把这些错误告诉我:

undefined reference to 'timer_create', line 20
undefined reference to 'timer_gettime', line 21

互联网指出我在编译时包含-lrt库的方向,但我找不到任何说明如何做的事情,暗示这可能是完全错误的。

那么,我是走在正确的道路上吗?如果我是,我怎么能让这段代码工作?

编辑:

我得到了基于clock()的计时器工作。缺点是我们是时间fork(),它在计时时没有注册任何时间。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用此

time_t start,end;
start=clock();//predefined  function in c

//after the user defined function does its work

end=clock();
t=(end-start)/CLOCKS_PER_SEC;