Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-22-2010, 05:57 PM   #1
LQ Newbie
Registered: Nov 2009
Posts: 13

Rep: Reputation: 0
Question 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
( GetProcessTimes(creationtime,...))

Old 03-22-2010, 06:09 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Simple: cat /proc/PID#/stat, and look at the 22nd field. That's the start time of your process (PID#).

'Hope that helps .. PSM
Old 03-23-2010, 10:52 AM   #3
LQ Newbie
Registered: Nov 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks paulsm4

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.
Old 03-23-2010, 12:18 PM   #4
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
Old 03-23-2010, 03:33 PM   #5
Senior Member
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Getting pid of a process!! vishamr2000 Programming 34 03-12-2015 07:12 AM
pid of a process Marin_ Linux - Software 3 03-07-2010 10:22 PM
how to get the name of process from PID in C/C++? kulandaivelu Programming 3 02-03-2009 09:41 AM
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 03:10 AM
[C++] Get process ID (PID) Moraxus Programming 5 10-16-2008 12:31 PM

All times are GMT -5. The time now is 10:33 AM.

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