在Linux中运行的进程如何确定它被暂停多长时间?

时间:2016-01-13 04:30:33

标签: c linux

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

一个简单的解决方案就是“忙碌”的睡眠:

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()函数将返回进程暂停的总时间。

谢谢!克劳迪奥

1 个答案:

答案 0 :(得分:1)

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.