我正在尝试在C编程中实现times()函数。
我正在使用struct tms
结构,其中包含以下字段:tms_utime,tms_cutime,
tms_stime和tms_cstime。
为了在我的程序中实现times()函数,我这样做:
在我分叉并创建一个子代之前,我调用了times函数(在父进程中)。
times(&start_tms);
ls -l
命令当孩子完成执行时,父亲第二次调用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);
}
}
答案 0 :(得分:3)
你的逻辑非常奇怪。您在子项中执行的写操作只是复制start_tms
中父项已经可用的数据,因此您的整个管道读/写操作是不必要的。
其次,clock_t
不是浮点类型,它是一个整数类型。您无法使用%f
进行打印。在%jd
s。
intmax_t
和printf
是安全的
你的waitpid缺少#include <sys/wait.h>
。因此,打开编译器警告,然后阅读它们。
以下是您的代码的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
中使用相应的格式字符串调用