times() 返回的 struct tms 值全部为零或负数
我正在尝试在 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);
}
}
I'm trying to implement the times() function in C programming.
I'm using the struct tms
structure which consists of the fields: tms_utime, tms_cutime,
tms_stime and tms_cstime.
In order to implement the times() function in my program, I do:
Before I fork and create a child, I call the times function (in the parent process).
times(&start_tms);
- I create a pipe and I pass the times of start structure to the pipe when I'm in the child process.
- The child executes a simple
ls -l
command When the child finishes he execution, the father calls for the second time the times() function.
times(&end_tms);
Unfortunately, the times of end_tms are all zero! Weird, but I don't know why.
Some things I don't understand in my program are:
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?
My program is as follows:
Thanks, in advance
#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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的逻辑很奇怪。您在子级中执行的写入操作只是复制
start_tms
中父级已可用的数据,因此整个管道读/写操作是不必要的。其次,clock_t 不是浮点类型,它是整型。您不能使用
%f
来打印它。在printf
中使用%jd
和intmax_t
是安全的。并且您缺少 waitpid 的
#include
。因此,请打开编译器警告并阅读它们。以下是适用于此处的代码的 C99 版本:
如果您没有
intmax_t
,请检查您的实现中clock_t
的大小,并找到与其匹配的标准整数类型,并在printf
调用中使用适当的格式字符串。Your logic is very strange. The writes you do in the child simply copy data that is already available to the parent in
start_tms
, so your whole pipe read/write thing is unnecessary.Secondly,
clock_t
is not a floating point type, it's an integral type. You can't use%f
to print it. Use%jd
andintmax_t
to be safe in theprintf
s.And you're missing
#include <sys/wait.h>
for waitpid. So turn on your compiler warnings, and read them.Here's a C99 version of your code that works here:
If you don't have
intmax_t
, check the size ofclock_t
on your implementation and find a standard integer type that matches it, and use the appropriate format string in yourprintf
calls.