gpt4 book ai didi

c - 在 Linux 中运行的进程如何确定它已挂起多长时间?

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:51 25 4
gpt4 key购买 nike

假设一个进程在 12:00:00 开始,它唯一做的就是休眠 120 秒 (sleep(120))。它通常应该在 12:02:00 被唤醒。现在想象一下,在 60 秒后系统将它暂停 (12:01:00) 300 秒(5 分钟)。发生的事情是在 12:06:00,进程恢复并立即唤醒,因为据我所知, sleep 指令使用机器时间来确定何时应该唤醒。但我正在寻找的是一个解决方案,让进程在剩余的 60 秒内继续休眠。

一个简单的解决方案是“忙” sleep :

for (i = 0; i < 120; i++) sleep(1);

但我正在寻找如下解决方案:

sleeping_time = 120;
do {
start_time = current_time();
sleep(sleeping_time);
sleeping_time = sleeping_time - (current_time() - start_time - suspended_time());
} while ( sleeping_time > 0 );

在这种情况下,suspended_time() 函数将返回进程暂停的总时间。

谢谢!克劳迪奥

最佳答案

clock_gettime() 函数可用于您感兴趣的计时。

它可以获取进程启动后耗时

可以获取进程实际运行的时间(不包括未运行进程的运行时间)

根据手册页(修剪为仅讨论 clock_gettime())

#include <time.h>
int clock_gettime(clockid_t clk_id, struct timespec *tp);

tp 参数是一个 timespec 结构,如:

       struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};

clk_id 参数是要作用的特定时钟的标识符。

时钟可能是系统范围的,因此对所有进程可见,或者如果它仅在单个进程内测量时间,则对每个进程可见。

  CLOCK_REALTIME
System-wide clock that measures real (i.e., wall-clock) time.
Setting this clock requires appropriate privileges. This clock
is affected by discontinuous jumps in the system time (e.g., if
the system administrator manually changes the clock), and by the
incremental adjustments performed by adjtime(3) and NTP.

CLOCK_REALTIME_COARSE (since Linux 2.6.32; Linux-specific)
A faster but less precise version of CLOCK_REALTIME. Use when
you need very fast, but not fine-grained timestamps.

CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time
since some unspecified starting point. This clock is not
affected by discontinuous jumps in the system time (e.g.,
if the system administrator manually changes the clock),
but is affected by the incremental adjustments performed
by adjtime(3) and NTP.

CLOCK_MONOTONIC_COARSE (since Linux 2.6.32; Linux-specific)
A faster but less precise version of CLOCK_MONOTONIC.
Use when you need very fast, but not fine-grained time‐
stamps.

CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific)
Similar to CLOCK_MONOTONIC, but provides access to a raw
hardware-based time that is not subject to NTP adjust‐
ments or the incremental adjustments performed by adj‐
time(3).

CLOCK_BOOTTIME (since Linux 2.6.39; Linux-specific)
Identical to CLOCK_MONOTONIC, except it also includes any
time that the system is suspended. This allows applica‐
tions to get a suspend-aware monotonic clock without hav‐
ing to deal with the complications of CLOCK_REALTIME,
which may have discontinuities if the time is changed
using settimeofday(2).

CLOCK_PROCESS_CPUTIME_ID (since Linux 2.6.12)
High-resolution per-process timer from the CPU.

CLOCK_THREAD_CPUTIME_ID (since Linux 2.6.12)
Thread-specific CPU-time clock.

RETURN VALUE
clock_gettime() returns
0 for success,
or
-1 for failure (in which case errno is set appropriately).

ERRORS
EFAULT tp points outside the accessible address space.

EINVAL The clk_id specified is not supported on this system.

关于c - 在 Linux 中运行的进程如何确定它已挂起多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758443/

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