gpt4 book ai didi

c - times() 返回的结构 tms 值全为零或负数

转载 作者:行者123 更新时间:2023-12-02 00:28:19 25 4
gpt4 key购买 nike

我正在尝试在 C 编程中实现 times() 函数。

我正在使用 struct tms 结构,它由以下字段组成:tms_utime、tms_cutime、tms_stime 和 tms_cstime。

为了在我的程序中实现 times() 函数,我这样做:

  1. 在派生并创建子进程之前,我调用了 times 函数(在父进程中)。

    times(&start_tms);
  2. 我创建了一个管道,当我在子进程中时,我将启动结构的时间传递给管道。
  3. child 执行一个简单的ls -l命令
  4. 当 child 执行完后,父亲第二次调用times()函数。

    times(&end_tms);

    不幸的是,end_tms的次数都是零!很奇怪,但我不知道为什么。

我在我的程序中不理解的一些事情是:

1) 在第一个 printfs 中,结构开始的时间是负数。这是为什么?2) 当我运行程序时,为什么我多次得到零?我做错了什么?

我的程序如下:

提前致谢


#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\n");

int fd[2]; //two pointers
int nbytes;
char string[] = "Hello, world!\n";
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\n\n",start_tms.tms_utime);
printf("Test start_tms.tms_cutime = %f\n\n",start_tms.tms_cutime);
printf("Test start_tms.tms_stime = %f\n\n",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\n\n", readbuffer);
printf("Test t1 = %f\n\n",t1);
printf("Test end_tms.tms_utime = %f\n\n",end_tms.tms_utime);
printf("Test end_tms.tms_cutime = %f\n\n",end_tms.tms_cutime);
printf("Test end_tms.tms_stime = %f\n\n",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\n\n",cpu_time);
printf("cpu Utime %f\n\n",utime);
printf("cpu Stime %f\n\n",stime);


}

}

最佳答案

你的逻辑很奇怪。您在子项中所做的写入只是复制父项在 start_tms 中已经可用的数据。 ,所以你的整个管道读/写的东西是不必要的。

其次,clock_t不是浮点类型,它是整数类型。你不能使用 %f打印它。使用 %jdintmax_tprintf 中保持安全

而你错过了#include <sys/wait.h>对于 waitpid。所以打开你的编译器警告,并阅读它们。

这是您的代码的 C99 版本,可在此处运行:

#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\n\n", (intmax_t)start_tms.tms_utime);
printf("Test start_tms.tms_cutime = %jd\n\n", (intmax_t)start_tms.tms_cutime);
printf("Test start_tms.tms_stime = %jd\n\n", (intmax_t)start_tms.tms_stime);
printf("Test start_tms.tms_cstime = %jd\n\n", (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\n\n",end_tms.tms_utime);
printf("Test end_tms.tms_cutime = %jd\n\n",end_tms.tms_cutime);
printf("Test end_tms.tms_stime = %jd\n\n",end_tms.tms_stime);
printf("Test end_tms.tms_cstime = %jd\n\n",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\n\n", (intmax_t)cpu_time);
printf("cpu Utime %jd\n\n", (intmax_t)utime);
printf("cpu Stime %jd\n\n", (intmax_t)stime);
printf("cpu CStime %jd\n\n", (intmax_t)cstime);
}
}

如果你没有 intmax_t , 检查 clock_t 的大小在你的实现中找到一个与之匹配的标准整数类型,并在你的 printf 中使用适当的格式字符串电话。

关于c - times() 返回的结构 tms 值全为零或负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7945989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com