``等待''通话后可用的信息
我编写了一个程序,该程序验证getRusage()
rusage_children flag forie forie forie for homer a wait
呼叫的某些信息, 如何修改程序以使其在处理错误方面更有效?还存在 2038 bug 发生在此行上:printf() :用户CPU秒=%ld \ n“,(long)usg.ru_utime.tv_sec);
?
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "print_rlimit.h"
int
main (void)
{
switch (fork ()) {
case -1:
perror ("fork()");
return 1;
case 0: { // child
time_t start, now;
alarm (10);
start = time (NULL);
while (1) {
now = time (NULL);
if ((now - start) > 5)
break;
}
_exit (0);
}
default: { // parent
int ret;
struct rusage usg;
pid_t pid;
sleep (2);
ret = getrusage (RUSAGE_CHILDREN, &usg);
if (ret == -1) {
perror ("getrusage()");
return 1;
}
printf ("before: user CPU seconds = %ld\n", (long)usg.ru_utime.tv_sec);
pid = wait (NULL);
if (pid == (pid_t)-1) {
perror ("wait()");
return 1;
}
ret = getrusage (RUSAGE_CHILDREN, &usg);
if (ret == -1) {
perror ("getrusage()");
return 1;
}
printf ("after: user CPU seconds = %ld\n", (long)usg.ru_utime.tv_sec);
break;
}
}
return 0;
}
标头:
#ifndef _PRINT_RLIMITS
#define _PRINT_RLIMITS
void print_rlimit (int resource);
#endif
I wrote a program that verifies that the getrusage ()
RUSAGE_CHILDREN flag retrieves certain information only about children for whom a wait
call was made,
How can I modify the program so that it is more efficient in terms of handling errors? There is also danger of a 2038 bug occurring on this line: printf (" before: user CPU seconds =% ld \ n ", (long) usg.ru_utime.tv_sec);
?
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "print_rlimit.h"
int
main (void)
{
switch (fork ()) {
case -1:
perror ("fork()");
return 1;
case 0: { // child
time_t start, now;
alarm (10);
start = time (NULL);
while (1) {
now = time (NULL);
if ((now - start) > 5)
break;
}
_exit (0);
}
default: { // parent
int ret;
struct rusage usg;
pid_t pid;
sleep (2);
ret = getrusage (RUSAGE_CHILDREN, &usg);
if (ret == -1) {
perror ("getrusage()");
return 1;
}
printf ("before: user CPU seconds = %ld\n", (long)usg.ru_utime.tv_sec);
pid = wait (NULL);
if (pid == (pid_t)-1) {
perror ("wait()");
return 1;
}
ret = getrusage (RUSAGE_CHILDREN, &usg);
if (ret == -1) {
perror ("getrusage()");
return 1;
}
printf ("after: user CPU seconds = %ld\n", (long)usg.ru_utime.tv_sec);
break;
}
}
return 0;
}
header:
#ifndef _PRINT_RLIMITS
#define _PRINT_RLIMITS
void print_rlimit (int resource);
#endif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2038错误
time_t
未指定为long
,因此请勿施放到long
,它可能仅为32--位和窄时间值。time_t
甚至不确定是几秒钟的整数计数。一个合理的替代方法是铸造最宽的整数类型。这将适应一个实现,该实现使用time_t
处理2038错误的更宽整数类型。time_t
Quanta假定为而不是假设
time_t
是秒数的整数计数,请使用标准double dible difftime(time_t time1,time_t time00)< /代码>返回秒数的差异,而不论
time_t
编码。燃烧CPU
而(1){now = time(null); ...
等待时会燃烧许多CPU tick。一个更高级的想法将睡一段时间,然后再尝试一次。更高级的方法将使用其他系统警报例程。
缺少错误处理
以下是无限循环应
time()
返回-1如果日历时间不可用。要解决,请测试-1。次要:需要铸造吗
? >
pid_t
是签名的整数类型,因此铸件是多余的。2038 bug
time_t
is not specified as along
, so do not cast tolong
which may only be 32-bit and narrow the time value.time_t
is not certainly even an integer count of seconds. A reasonable alternative is to cast to the widest integer type. This will accommodate an implementation that uses a wider integer type fortime_t
to handle the 2038 bug.time_t
quanta assumedRather than assume
time_t
is an integer count of seconds, use standarddouble difftime(time_t time1, time_t time0)
which returns a difference in seconds regardless oftime_t
encoding.Burning CPU
while (1) { now = time (NULL); ...
burns lots of CPU ticks while waiting. A more advanced idea would sleep for some time before trying again.A more advanced approach would use other system alarm routines.
Missing error handling
Below is an infinite loop should
time()
return -1 if the calendar time is not available. To fix, test for -1.Minor: cast needed?
On many implementations
pid_t
is a signed integer type so the cast is superfluous.