Get the creation time of a process via pid in C++
My issue is simple yet difficult. Wondering if someone would have some insight.
Scenario: ( in C++ code)
I need a process A to be able to get the start time of process B. I have the PID of the process B.
I would have thought there would be a simple system call to make by passing in the pid of Process B, but I don't thing there is??!!
So my other thought is to create a script that will be passed the pid and either I can read the file creation of /proc/<PID>/stat or I could also parse the start time of ps on the <PID>. This script can be ran from the C++ code. My difficulty is capturing this information in the C++ code. If I run exec ( or system if not a script) I need the results in a program variable....not stdout.
Does anyone have any suggestion????
My only solution is write it to a tmp file and read it back into the program variable. Seems rather arguious but a least it would work.
Background - I need to port Windows code
Simple: cat /proc/PID#/stat, and look at the 22nd field. That's the start time of your process (PID#).
'Hope that helps .. PSM
I was already aware of the stat file. That wasn't isn't so much my issue. My main issue is getting the value into a variable within my C++ program.
I looking to confirm that there is no system call that would read the value directly into a variable within my code. If there is such a call or method or procedure, I would really be interested in knowing( I haven't been able to fine anything in my books or web).
'cat' the results into a file ( via a script) and then have the program access that temp file is my only solution right now.
Of *course* there's a "system call" to read from proc.
Or, more specifically, a "standard library call".
All you have to do is "fopen ()" /proc/WHATEVER_PID/stat (just as you would a text file), and "fscanf()".
Or "cin", or "open()/read()", or whatever text I/O functions you prefer.
'Hope that helps .. PSM
The thing to remember is that, in Linux, EVERYTHING is a file.
This means that, in the vast majority of cases, you can open() (or fopen()), read(), write(), close() just like a file.
Pretty elegant in most cases, actually. You do have to be careful about the buffering that fopen/fread/fwrite/fclose do. I've found that using the lower level open/read/write/close is better when you are talking directly to the kernel or to hardware (as you are indeed doing when you talk to /proc). It makes you do more housekeeping, but keeps you out of trouble.
|All times are GMT -5. The time now is 08:28 PM.|