``等待''通话后可用的信息

发布于 2025-02-03 15:59:06 字数 1947 浏览 2 评论 0原文

我编写了一个程序,该程序验证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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚然 2025-02-10 15:59:06

2038错误

time_t未指定为long,因此请勿施放到long,它可能仅为32--位和时间值。

// printf (" before: user CPU seconds =% ld \ n ", (long) usg.ru_utime.tv_sec);

time_t甚至不确定是几秒钟的整数计数。一个合理的替代方法是铸造最宽的整数类型。这将适应一个实现,该实现使用time_t处理2038错误的更宽整数类型。

#include <inttypes.h>
printf (" before: user CPU seconds = %jd\n", (intmax_t) usg.ru_utime.tv_sec);

time_t Quanta假定为

而不是假设time_t是秒数的整数计数,请使用标准double dible difftime(time_t time1,time_t time00)< /代码>返回秒数的差异,而不论time_t编码。

// (now - start) > 5
difftime(now, start) > 5.0

燃烧CPU

而(1){now = time(null); ...等待时会燃烧许多CPU tick。一个更高级的想法将睡一段时间,然后再尝试一次。

        while (1) {
            now = time (NULL);
            double diff = difftime(now, start);
            if (diff > 5) {
                break;
            }
            diff *= 1000000; // microseconds
            usleep(diff/2); // Sleep for half of that 
        }

更高级的方法将使用其他系统警报例程。

缺少错误处理

以下是无限循环应time()返回-1如果日历时间不可用。要解决,请测试-1。

        start = time (NULL);
        while (1) {
            now = time (NULL);
            if ((now - start) > 5)
                break;
        }

次要:需要铸造吗

? > pid_t签名的整数类型,因此铸件是多余的。

// pid == (pid_t)-1
pid == -1

2038 bug

time_t is not specified as a long, so do not cast to long which may only be 32-bit and narrow the time value.

// printf (" before: user CPU seconds =% ld \ n ", (long) usg.ru_utime.tv_sec);

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 for time_t to handle the 2038 bug.

#include <inttypes.h>
printf (" before: user CPU seconds = %jd\n", (intmax_t) usg.ru_utime.tv_sec);

time_t quanta assumed

Rather than assume time_t is an integer count of seconds, use standard double difftime(time_t time1, time_t time0) which returns a difference in seconds regardless of time_t encoding.

// (now - start) > 5
difftime(now, start) > 5.0

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.

        while (1) {
            now = time (NULL);
            double diff = difftime(now, start);
            if (diff > 5) {
                break;
            }
            diff *= 1000000; // microseconds
            usleep(diff/2); // Sleep for half of that 
        }

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.

        start = time (NULL);
        while (1) {
            now = time (NULL);
            if ((now - start) > 5)
                break;
        }

Minor: cast needed?

On many implementations pid_t is a signed integer type so the cast is superfluous.

// pid == (pid_t)-1
pid == -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文