LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linux get process start time of given PID (https://www.linuxquestions.org/questions/programming-9/linux-get-process-start-time-of-given-pid-4175631677/)

ilansch 06-11-2018 01:19 AM

Linux get process start time of given PID
 
I need to get the start time of process using C code in userspace.
The process will run as root, So I can fopen /proc/PID/stat.
I saw implementation, e.g:

https://stackoverflow.com/questions/...ocess-on-linux
or
http://brokestream.com/procstat.c

But they are invalid, Why they are invalid ? if the process 2nd parameter contains space, e.g:

[ilan@CentOS7286-64 tester]$ cat /proc/1077/stat
1077 (rs:main Q:Reg) S 1 1054 1054 0 -1 1077944384 21791 0 10 0 528 464 0 0 20 0 3 0 1056 321650688 1481 18446744073709551615 1 1 0 0 0 0 2146172671 16781830 1133601 18446744073709551615 0 0 -1 1 0 0 1 0 0 0 0 0 0 0 0 0 0

These solutions will not work.

Is there a better way retrieving a process start time other then parsing the /proc/PID/stat results ? I can do the following logic:

1. read long, first parameter is pid
2. read char, make sure that i finish reading only when hitting close ')'. - 2nd parameter is tcomm (filename of the executable)
3. read char - 3rd parameter process state.

In **solaris**, you simply read the result to psinfo_t struct.

NevemTeve 06-11-2018 01:43 AM

I seem to have found a file called /proc/<pid>/sched, the line se.exec_start might be your friend:
Code:

bash (6579, #threads: 1)
-------------------------------------------------------------------
se.exec_start                                :    426516073.478059
se.vruntime                                  :      2277932.238573
se.sum_exec_runtime                          :            30.874435
...


ilansch 06-11-2018 02:05 AM

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

NevemTeve 06-11-2018 02:18 AM

Quote:

Originally Posted by ilansch (Post 5866078)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Are you absolutely sure, or just guessing?

syg00 06-11-2018 05:08 AM

Quote:

Originally Posted by ilansch (Post 5866068)
But they are invalid, Why they are invalid ? if the process 2nd parameter contains space, e.g:

These solutions will not work.

By that logic, every piece of software ever written is "invalid". Rubbish.
What effort have you made to diagnose the error ?. None at all apparently. You have the code, do some analysis given the specific failure. A quick look suggests it shouldn't take much to get that procstat code working .

ilansch 06-12-2018 02:07 AM

Quote:

Originally Posted by syg00 (Post 5866121)
By that logic, every piece of software ever written is "invalid". Rubbish.
What effort have you made to diagnose the error ?. None at all apparently. You have the code, do some analysis given the specific failure. A quick look suggests it shouldn't take much to get that procstat code working .

What the hell are you talking about and why attacking me, Is your wife making problems at home ? WTF is wrong with you.
I analyzed the code and found out what is wrong, also suggested solution.
You should read the question before your lamented comment and sad comment.

MY QUESTION is my solution is OK or should i take a different approach, maybe there is system call to retrieve a struct of the data, like in solaris, other than parsing the results of cat /proc/PID/stat. Do me a favor and dont answer the thread.

NevemTeve 06-12-2018 06:39 AM

Your solution is excellent. You may close this topic.


All times are GMT -5. The time now is 09:32 PM.