English 中文(简体)
零或负
原标题:struct tms values returned by times() all zero or negative
  • 时间:2011-10-30 15:54:42
  •  标签:
  • c
  • struct
  • tms

我试图在C方案拟订中履行(......)职能。

I m using the struct tms structure which consists of the fields: tms_utime, tms_cutime, tms_stime and tms_cstime.

为了在我的节目中履行职能,我确实:

  1. Before I fork and create a child, I call the times function (in the parent process).

    times(&start_tms);
    
  2. I create a pipe and I pass the times of start structure to the pipe when I m in the child process.
  3. The child executes a simple ls -l command
  4. 当儿童完成处决时,父亲第二次要求履行(或)职能。

    times(&end_tms);
    

    不幸的是,末端的时代是零! 我不知道为什么。

我在方案中不理解的是:

1) In the first printfs the times of the struct start are negative. Why is that? 2) When I run the program, why do I get zeros for times? What am i doing wrong?

我的方案如下:

提前感谢


#include <sys/times.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main() {

printf("test
");

int     fd[2]; //two pointers
int nbytes;
char    string[] = "Hello, world!
";
char    readbuffer[80];
struct tms start_tms;
struct tms end_tms;
clock_t start, end;
double cpu_time_used;

pipe(fd);

//once we have established the pipeline we fork the child
pid_t   childpid;
pid_t pid_waitpid;

//NEW MODIFICATION!!! call times before fork()!!!
times(&start_tms);

//they return negative values, but why???
printf("Test start_tms.tms_utime = %f

",start_tms.tms_utime);
printf("Test start_tms.tms_cutime = %f

",start_tms.tms_cutime);
printf("Test start_tms.tms_stime = %f

",start_tms.tms_stime);


if((childpid = fork()) == -1)
            {
                    perror("fork");
                    exit(1);
            }

if(childpid == 0)
            {

                    /* Child process closes up input side of pipe */
                     close(fd[0]);

                   /* call times function */ 
                   /*times(&start_tms);*/


                      //REMOVED!!!!
                    //write(fd[1], string, (strlen(string)+1));
                    write(fd[1], &start_tms.tms_cutime, sizeof(clock_t));
                    write(fd[1], &start_tms.tms_utime, sizeof(clock_t));
                    write(fd[1], &start_tms.tms_stime, sizeof(clock_t));

                     //execute /bin/ls
                    execl("/bin/ls", "/bin/ls", "-r", "-t", "-l", (char *) 0);

                    exit(0);


            }
else
            {
                    /* Parent process closes up output side of pipe */
                    close(fd[1]);

                    /* NEW MODIFICATION, wait for the child!!! */
                     if( (pid_waitpid  = waitpid(childpid,NULL,0) ) == -1)
                    {
                             perror("waitpid");
                             exit(1);
                     }



                    /* call times for capturing end times */
                    times(&end_tms);

                    /* define t1, t2, variables */
                    clock_t t1,t2,t3;


                     //REMOVED!!!!
                    //nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                    read(fd[0], &t1, sizeof(clock_t));
                    read(fd[0], &t2, sizeof(clock_t));
                    read(fd[0], &t3, sizeof(clock_t));

                    printf("Received string: %s

", readbuffer);
                    printf("Test t1 = %f

",t1);
                    printf("Test end_tms.tms_utime = %f

",end_tms.tms_utime);
                    printf("Test end_tms.tms_cutime = %f

",end_tms.tms_cutime);
                    printf("Test end_tms.tms_stime = %f

",end_tms.tms_stime);

                    /* Calculate times, unfortunately return zero, but why??? */
                    double cpu_time = end_tms.tms_cutime - t1;
                    double utime = end_tms.tms_utime - t2;
                    double stime = end_tms.tms_stime - t3;

                    //Unfortunately printfs return zero, but why???
                    printf("cpu time %f

",cpu_time);
                    printf("cpu Utime %f

",utime);
                    printf("cpu Stime %f

",stime);


}

}
最佳回答

你的逻辑非常奇怪。 你在孩子上写的文字只是复制父母在<条码>中已经掌握的数据,因此,你整个管道读/做事是不必要的。

第二,<代码>24_t不是浮动点类型,而是整体类型。 页: 1 使用<代码>%jd和intmax_t,在f上安全。

而你又找不到:#include <sys/wait.h>等待。 因此,请各位汇编者发出警告,并阅读这些警告。

这里的《你法典》第99版在此行:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/wait.h>
#include <sys/times.h>
#include <sys/types.h>

int main() {
    struct tms start_tms;
    struct tms end_tms;

    //once we have established the pipeline we fork the child
    pid_t   childpid;

    times(&start_tms);

    printf("Test start_tms.tms_utime = %jd

",  (intmax_t)start_tms.tms_utime);
    printf("Test start_tms.tms_cutime = %jd

", (intmax_t)start_tms.tms_cutime);
    printf("Test start_tms.tms_stime = %jd

",  (intmax_t)start_tms.tms_stime);
    printf("Test start_tms.tms_cstime = %jd

",  (intmax_t)start_tms.tms_cstime);


    if((childpid = fork()) == -1)
    {
        perror("fork");
        exit(1);
    }

    if(childpid == 0)
    {
        //execute /bin/ls
        execl("/bin/ls", "/bin/ls", "-R", "-t", "-l", (char *) 0);
        exit(0);
    }
    else
    {
        /* Parent process */

        /* NEW MODIFICATION, wait for the child!!! */
        if (waitpid(childpid,NULL,0) == -1)
        {
            perror("waitpid");
            exit(1);
        }

        /* call times for capturing end times */
        times(&end_tms);

        printf("Test end_tms.tms_utime = %jd

",end_tms.tms_utime);
        printf("Test end_tms.tms_cutime = %jd

",end_tms.tms_cutime);
        printf("Test end_tms.tms_stime = %jd

",end_tms.tms_stime);
        printf("Test end_tms.tms_cstime = %jd

",end_tms.tms_cstime);

        /* Calculate times, unfortunately return zero, but why??? */
        clock_t cpu_time = end_tms.tms_cutime - start_tms.tms_cutime;
        clock_t utime = end_tms.tms_utime - start_tms.tms_utime;
        clock_t stime = end_tms.tms_stime - start_tms.tms_stime;
        clock_t cstime = end_tms.tms_cstime - start_tms.tms_cstime;

        //Unfortunately printfs return zero, but why???
        printf("cpu time %jd

",  (intmax_t)cpu_time);
        printf("cpu Utime %jd

", (intmax_t)utime);
        printf("cpu Stime %jd

", (intmax_t)stime);
        printf("cpu CStime %jd

", (intmax_t)cstime);
    }
}

If you don t have intmax_t, check the size of clock_t on your implementation and find a standard integer type that matches it, and use the appropriate format string in your printf calls.

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签