LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2018, 01:19 AM   #1
ilansch
LQ Newbie
 
Registered: Feb 2017
Posts: 13

Rep: Reputation: Disabled
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.
 
Old 06-11-2018, 01:43 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
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
...
 
Old 06-11-2018, 02:05 AM   #3
ilansch
LQ Newbie
 
Registered: Feb 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Last edited by ilansch; 06-11-2018 at 02:07 AM.
 
Old 06-11-2018, 02:18 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Quote:
Originally Posted by ilansch View Post
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Are you absolutely sure, or just guessing?
 
Old 06-11-2018, 05:08 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Quote:
Originally Posted by ilansch View Post
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 .
 
Old 06-12-2018, 02:07 AM   #6
ilansch
LQ Newbie
 
Registered: Feb 2017
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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.
 
Old 06-12-2018, 06:39 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
Your solution is excellent. You may close this topic.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-20-2011 11:54 PM
Get the creation time of a process via pid in C++ Cracker-Barrel Programming 4 03-23-2010 03:33 PM
Why does exec start a process with new PID? Chowroc Programming 4 01-11-2006 05:55 AM
Start a Process with dedicated PID murder Linux - Newbie 5 08-15-2005 03:49 PM
How can I create a pid file from a backgournd process that I start? Hackiller Linux - General 3 05-12-2004 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration