Let's say a process is started at 12:00:00 and the only thing it does is to sleep for 120 seconds (sleep(120)). It should normally be woken up at 12:02:00. Imagine now that after 60 seconds the system suspends it (12:01:00) for 300 seconds (5 minutes). What happens is that at 12:06:00, the process is resumed and it immediately wakes because, as far I could understand, the sleep instruction uses the machine time to determine when it should wake up. But what I'm looking for is a solution where the process continues to sleep for the remaining 60 seconds.
A simple solution would be a "busy" sleep:
for (i = 0; i < 120; i++) sleep(1);
but I am looking for a solution like:
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 );
In this case, the suspended_time() function would return the total time the process was suspended.
Thanks! Claudio
the
clock_gettime()
function is available for the timing your interested in.It can get elapsed time since the process started
It can get the actual time spent running the process (does not include elapsed time not running the process.)
Per the man page (trimmed to only discuss clock_gettime())
The tp argument is a timespec structure, as specified in :
The clk_id argument is the identifier of the particular clock on which to act.
A clock may be system-wide and hence visible for all processes, or per-process if it measures time only within a single process.