Is there a way to get this information from the /proc directory? I want to be able to get how long each process has been running on seconds.
EDIT: I needed to do this from C++. Sorry for the confusion.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Okay guys, so after reading the
topcommand’s source code, I figured out a non-hacky way of getting the start time of a process. The formula that they use is:(You have to divide by HZ because process_start_time is in jiffies)
Obtaining these values:
current_time– You can get this from the C commandgettimeofday().boot_time– This value is located in/proc/uptime. This file contains two numbers: the uptime of the system (seconds), and the amount of time spent in idle process (seconds). Take the first.process_start_time– This value is located in/proc/[PID]/stat. The time difference (in jiffies) between system boot and when the process started. (The 22nd value in the file if you split on whitespace).The code (Sorry, I sometimes mix c and c++):
Happy Coding!