LinuxQuestions.org
Help answer threads with 0 replies.
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 03-22-2010, 06:57 PM   #1
Cracker-Barrel
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,...))

Thanks
 
Old 03-22-2010, 07:09 PM   #2
paulsm4
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, 11:52 AM   #3
Cracker-Barrel
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, 01:18 PM   #4
paulsm4
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, 04:33 PM   #5
jiml8
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.
 
  


Reply


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


All times are GMT -5. The time now is 01:32 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration