LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 04:35 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
[C++] How to fetch the process id of the currently active C++ file?


I tried this:

Code:
#include <iostream>
#include <stdlib.h>
#include <sstream>

int main ()
{
    std::stringstream psOutput;
    psOutput << "ps --no-headers -f -p " << getpid();

    system (psOutput.str().c_str());

    return 0;
}
The output:
Code:
anisha@linux-trra:~/> ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
anisha    6426  6423  0 13:32 pts/0    00:00:00 /bin/bash
anisha    7590  6426  0 15:32 pts/0    00:00:12 kwrite api.cpp
anisha    8099  6426  0 16:02 pts/0    00:00:00 ps -f

anisha@linux-trra:~/> g++ api.cpp -Wall
anisha@linux-trra:~/> ./a.out 
anisha    8119  6426  0 16:03 pts/0    00:00:00 ./a.out

anisha@linux-trra:~/>
I wanted the pid of api.cpp, not of a.out.

How to solve this?

Last edited by TheIndependentAquarius; 02-11-2013 at 04:39 AM.
 
Old 02-11-2013, 04:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,676

Rep: Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855Reputation: 2855
a cpp files is not runnable, it is not a process. It will be compiled, and linked together with other compiled files and the result will be one single program. if you execute this process it will get a process id. In a multithreaded environment all the running threads will get also an id, but a source file (like your cpp) will never have a process id.
 
1 members found this post helpful.
Old 02-11-2013, 04:41 AM   #3
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,680
Blog Entries: 29

Original Poster
Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Quote:
Originally Posted by pan64 View Post
but a source file (like your cpp) will never have a process id.
Thanks! What I was seeing was actually the pid of kwrite, not
of that cpp file!

I actually wanted to fetch the file name and was thinking of
extracting it from output of ps!

The simpler way is __FILE__!
http://gcc.gnu.org/onlinedocs/cpp/St...ed-Macros.html

Last edited by TheIndependentAquarius; 02-11-2013 at 04:51 AM.
 
  


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
How to fetch data from file rudra_biswal Linux - Newbie 4 04-03-2012 03:02 AM
program to fetch pid of a running process smitha kulkarni Linux - Server 9 03-05-2010 12:51 PM
mplayer fetch subtitles option: --fetch-subs frenchn00b Linux - General 1 11-09-2009 11:21 PM
Generic error "cannot fetch general purpose registers for thread, no such process"? kranti Programming 0 10-22-2005 06:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:24 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration