Part 4. Execution times (15 points)

You’re nearly finished with ps. Two tasks remain. The first is making the execution time of a process print out in the [DD-]hh:mm:ss format. Meaning that if the process has been running for less than a day, the hours, minutes, and seconds are printed out (two digits each) and if the process has been running for longer than a day, then the number of days are printed out followed by a dash and then hours, minutes, and seconds. For example, 00:05:23 for a process that has been running for 5 minutes and 23 seconds and 03-15:59:21 for a process that has been running for 3 days, 15 hours, 59 minutes, and 21 seconds.

The process for computing the execution time has two parts: First, you need to get the execution time in seconds and second, you need to convert that into days, hours, minutes, and seconds. The second part is standard. The first part isn’t difficult, but isn’t obvious.

Linux keeps track of time in terms of ticks. There are some number of ticks per second which can vary by system. You will have to ask the OS how many ticks there are per second. Fortunately, there’s a libc function that will do that for us! Here’s a function you can use which returns the number of ticks per second.

/// Returns the number of clock ticks per second.
fn ticks_per_second() -> Result<i64> {
    let ret = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };

    if ret == -1 {
        return Err(io::Error::last_os_error().into());
    }
    Ok(ret)
}

Your task

In your ps’s run(), call ticks_per_second(). To compute the execution time of a process in seconds, use the execution_time field of the Process struct and divide it by the result of ticks_per_second().

Convert this time to days, hours, minutes, and seconds, and when printing the execution time, use the appropriate format.