C 让流程计算所花费的时间

C 让流程计算所花费的时间,c,process,operating-system,C,Process,Operating System,我正在创建一个进程树,我需要让一个进程执行时间命令。在终端工作时,您可以: Time ./myExec 除了进程树之外,我还需要显示执行我的应用程序所用的时间,因此我认为最好让我的一个进程执行它,而不是自己在终端中编写它 我该怎么做 创建我的树的一段代码: pid_t idProcesso; // P1 idProcesso = fork(); switch(idProcesso){ case -1: exit(-1); //ERROR c

我正在创建一个
进程树
,我需要让一个进程执行
时间
命令。在终端工作时,您可以:

Time ./myExec  
除了
进程树
之外,我还需要显示执行我的应用程序所用的时间,因此我认为最好让我的一个进程执行它,而不是自己在终端中编写它

我该怎么做

创建我的树的一段代码:

pid_t idProcesso; // P1
    idProcesso = fork();

    switch(idProcesso){
        case -1: exit(-1); //ERROR
        case 0: //P2
            printf("sou P2: %d  |  meu pai P1: %d\n", getpid(), getppid());
            idProcesso = fork();
            switch(idProcesso){
                case -1: exit(-1); //Error
                case 0: //P4
                    printf("Sou P4: %d  |  meu pai P2: %d\n", getpid(), getppid());
                    break;

                default: //Continuação de P2
                wait(&status);
                printf("Sou P2: %d  |  já esperei meu filho P4: %d\n", getpid(), idProcesso);
                idProcesso = fork(); //P5
                switch(idProcesso){
                    case -1: exit(-1); //ERROR
                    case 0: //P5
                        printf("Sou P5: %d  |  meu pai P2: %d\n", getpid(), getppid());
                        break;
                    default: //Continuacao de P5
                        wait(&status); //P2 espera seu filho P5
                        printf("Sou P2: %d  |  já esperei meu filho P5: %d\n", getpid(), idProcesso);

                }
            }  
如果不可能这样做(因为我们在执行文件时使用它),则使用一种比我的更好的方法来测量时间(Alaways返回0):


Obs:我正在使用Linux。

您使用的是什么操作系统?@EOF抱歉,我忘了告诉您。Linux>Ubuntu发行版。太棒了!在这种情况下,您肯定已经阅读了
man 3 clock[…]Linux在clock()返回的值中没有包含等待孩子的时间。
您遇到的行为如何让您感到惊讶?@EOF Ya,我知道了有什么提示吗?当然,既然您现在已经阅读了
man 3 clock
,那么您还可以阅读其中的下一句
clock_t begin, end;
    double time_spent;
    begin = clock();  //Start counting time
    pid_t idProcesso; // P1
        idProcesso = fork();

        switch(idProcesso){
            case -1: exit(-1); //ERROR
            case 0: //P2
                printf("sou P2: %d  |  meu pai P1: %d\n", getpid(), getppid());
                idProcesso = fork();
                switch(idProcesso){
                    case -1: exit(-1); //Error
                    case 0: //P4
                        printf("Sou P4: %d  |  meu pai P2: %d\n", getpid(), getppid());
                        break;

                    default: //Continuação de P2
                    wait(&status);
                    printf("Sou P2: %d  |  já esperei meu filho P4: %d\n", getpid(), idProcesso);
                    idProcesso = fork(); //P5
                    switch(idProcesso){
                        case -1: exit(-1); //ERROR
                        case 0: //P5
                            printf("Sou P5: %d  |  meu pai P2: %d\n", getpid(), getppid());
                            break;
                        default: //Continuacao de P5
                            wait(&status); //P2 espera seu filho P5
                            printf("Sou P2: %d  |  já esperei meu filho P5: %d\n", getpid(), idProcesso);

                    }
                }  
end = clock();
                    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
                    printf("Tempo Gasto: %.2f \n", time_spent);