* Re: [gentoo-user] OT: get process name in c++
[not found] <1151499883.17966.13.camel@orpheus>
@ 2006-06-28 13:51 ` Petr Uzel
2006-06-28 14:01 ` Petr Uzel
2006-06-28 15:55 ` Richard Fish
2006-07-27 0:19 ` [gentoo-user] " Iain Buchanan
2 siblings, 1 reply; 16+ messages in thread
From: Petr Uzel @ 2006-06-28 13:51 UTC (permalink / raw
To: gentoo-user
Dne středa 28 červen 2006 15:04 Iain Buchanan napsal(a):
> Hi again :)
>
> I have been googling for this one for a little while, and trying various
> options, but I can't seem to find it: How do I get the process from
> within a process in c or c++?
>
> I don't want to use argv[0], because the particular place is deep within
> classes, and there are too many (I'm doing some porting) to edit all of
> their constructors, for example, to pass argv[0].
>
> something like
>
> char *name = getpsname (pid);
>
Hi,
what about directly reading /proc/pid_of_the_process/stat (may be some other
file in this directory).
--
Petr Uzel
petr.uzel@centrum.cz
Jabber : knotor@njs.netlab.cz
ICQ : 101606095
Gentoo Linux
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-06-28 13:51 ` [gentoo-user] OT: get process name in c++ Petr Uzel
@ 2006-06-28 14:01 ` Petr Uzel
2006-06-29 0:47 ` Iain Buchanan
0 siblings, 1 reply; 16+ messages in thread
From: Petr Uzel @ 2006-06-28 14:01 UTC (permalink / raw
To: gentoo-user
Dne středa 28 červen 2006 15:51 Petr Uzel napsal(a):
> Dne středa 28 červen 2006 15:04 Iain Buchanan napsal(a):
> > Hi again :)
> >
> > I have been googling for this one for a little while, and trying various
> > options, but I can't seem to find it: How do I get the process from
> > within a process in c or c++?
> >
> > I don't want to use argv[0], because the particular place is deep within
> > classes, and there are too many (I'm doing some porting) to edit all of
> > their constructors, for example, to pass argv[0].
> >
> > something like
> >
> > char *name = getpsname (pid);
>
> Hi,
> what about directly reading /proc/pid_of_the_process/stat (may be some
> other file in this directory).
Now I found better solution :
file /proc/self/status is a link pointing to the
directory /proc/pid_of_process. And by parsing status file in this directory
you should get all the informations you want.
--
Petr Uzel
petr.uzel@centrum.cz
Jabber : knotor@njs.netlab.cz
ICQ : 101606095
Gentoo Linux
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
[not found] <1151499883.17966.13.camel@orpheus>
2006-06-28 13:51 ` [gentoo-user] OT: get process name in c++ Petr Uzel
@ 2006-06-28 15:55 ` Richard Fish
2006-07-27 0:19 ` [gentoo-user] " Iain Buchanan
2 siblings, 0 replies; 16+ messages in thread
From: Richard Fish @ 2006-06-28 15:55 UTC (permalink / raw
To: gentoo-user
On 6/28/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> I don't want to use argv[0], because the particular place is deep within
> classes, and there are too many (I'm doing some porting) to edit all of
> their constructors, for example, to pass argv[0].
The normal way of handling this is with a global variable. Just set
it in your main() function, and it can be accessed from any other code
in your program.
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-06-28 14:01 ` Petr Uzel
@ 2006-06-29 0:47 ` Iain Buchanan
2006-07-07 2:37 ` Iain Buchanan
0 siblings, 1 reply; 16+ messages in thread
From: Iain Buchanan @ 2006-06-29 0:47 UTC (permalink / raw
To: gentoo-user
On Wed, 2006-06-28 at 16:01 +0200, Petr Uzel wrote:
> Dne středa 28 červen 2006 15:51 Petr Uzel napsal(a):
> > Dne středa 28 červen 2006 15:04 Iain Buchanan napsal(a):
> > > Hi again :)
> > >
> > > I have been googling for this one for a little while, and trying various
> > > options, but I can't seem to find it: How do I get the process from
> > > within a process in c or c++?
> > >
> > > I don't want to use argv[0], because the particular place is deep within
> > > classes, and there are too many (I'm doing some porting) to edit all of
> > > their constructors, for example, to pass argv[0].
> file /proc/self/status is a link pointing to the
> directory /proc/pid_of_process. And by parsing status file in this directory
> you should get all the informations you want.
great idea, thanks!
--
Iain Buchanan <iaindb at netspace dot net dot au>
In every non-trivial program there is at least one bug.
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-06-29 0:47 ` Iain Buchanan
@ 2006-07-07 2:37 ` Iain Buchanan
2006-07-07 2:55 ` Richard Fish
2006-07-07 22:05 ` Richard Fish
0 siblings, 2 replies; 16+ messages in thread
From: Iain Buchanan @ 2006-07-07 2:37 UTC (permalink / raw
To: gentoo-user
On Thu, 2006-06-29 at 10:17 +0930, Iain Buchanan wrote:
> On Wed, 2006-06-28 at 16:01 +0200, Petr Uzel wrote:
>
> > file /proc/self/status is a link pointing to the
> > directory /proc/pid_of_process. And by parsing status file in this directory
> > you should get all the informations you want.
>
> great idea, thanks!
Someone mentioned that /proc is deprecated for 2.6? is this correct? Is
there a /sys alternative to /proc/self/status?
thanks,
--
Iain Buchanan <iaindb at netspace dot net dot au>
In charity there is no excess.
-- Francis Bacon
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 2:37 ` Iain Buchanan
@ 2006-07-07 2:55 ` Richard Fish
2006-07-07 4:35 ` Iain Buchanan
2006-07-07 22:05 ` Richard Fish
1 sibling, 1 reply; 16+ messages in thread
From: Richard Fish @ 2006-07-07 2:55 UTC (permalink / raw
To: gentoo-user
On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> Someone mentioned that /proc is deprecated for 2.6? is this correct? Is
> there a /sys alternative to /proc/self/status?
Again, I *highly* suggest doing the standard, cross-platform, and
most-efficient thing, which is to just store off the argv[0] pointer
to a global variable.
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 2:55 ` Richard Fish
@ 2006-07-07 4:35 ` Iain Buchanan
2006-07-07 4:48 ` Richard Fish
0 siblings, 1 reply; 16+ messages in thread
From: Iain Buchanan @ 2006-07-07 4:35 UTC (permalink / raw
To: gentoo-user
On Thu, 2006-07-06 at 19:55 -0700, Richard Fish wrote:
> On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> > Someone mentioned that /proc is deprecated for 2.6? is this correct? Is
> > there a /sys alternative to /proc/self/status?
>
> Again, I *highly* suggest doing the standard, cross-platform, and
> most-efficient thing, which is to just store off the argv[0] pointer
> to a global variable.
yes I would, except that I'm editing a class which is inherited by a
multitude of programs, so the /proc way I only have to change one file,
whereas the argv[0] way I have to change every source file...
but suggestion noted :)
--
Iain Buchanan <iaindb at netspace dot net dot au>
<dracus> Ctrl+Option+Command + P + R
<Knghtbrd> dracus - YE GODS! That's worse than EMACS!
<LauraDax> hehehehe
<dracus> don't ask what that does :P
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 4:35 ` Iain Buchanan
@ 2006-07-07 4:48 ` Richard Fish
2006-07-07 5:05 ` Iain Buchanan
0 siblings, 1 reply; 16+ messages in thread
From: Richard Fish @ 2006-07-07 4:48 UTC (permalink / raw
To: gentoo-user
On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> yes I would, except that I'm editing a class which is inherited by a
> multitude of programs, so the /proc way I only have to change one file,
> whereas the argv[0] way I have to change every source file...
No, that's the point of a global. Inheritance and all doesn't matter.
You change main.c (2 lines):
char* progname = 0;
...
progname = argv[0];
main.h (1 line):
extern char* progname;
and the source file where you want to use it it (2 lines):
#include "main.h"
...
printf("progname is %s\n", progname);
Your details might vary, for example if there is already a globals.h
you could move the declaration from main.h to there, etc. But this
really is the sane way of doing this...
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 4:48 ` Richard Fish
@ 2006-07-07 5:05 ` Iain Buchanan
2006-07-07 17:14 ` Richard Fish
0 siblings, 1 reply; 16+ messages in thread
From: Iain Buchanan @ 2006-07-07 5:05 UTC (permalink / raw
To: gentoo-user
On Thu, 2006-07-06 at 21:48 -0700, Richard Fish wrote:
> On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> > yes I would, except that I'm editing a class which is inherited by a
> > multitude of programs, so the /proc way I only have to change one file,
> > whereas the argv[0] way I have to change every source file...
>
> No, that's the point of a global. Inheritance and all doesn't matter.
>
> You change main.c (2 lines):
> char* progname = 0;
but I don't have just one main.c, I have 20+ different apps, each with
their own main.c, which I don't maintain, for which I'd have to issue
updates. Not to mention educate the other programmers on including this
ditty in each new app.
the /proc way I can just update the class in the background, which is in
cvs, and no one even knows the difference...
but please keep responding with ideas :)
--
Iain Buchanan <iaindb at netspace dot net dot au>
When you go out to buy, don't show your silver.
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 5:05 ` Iain Buchanan
@ 2006-07-07 17:14 ` Richard Fish
2006-07-07 20:53 ` Uwe Thiem
0 siblings, 1 reply; 16+ messages in thread
From: Richard Fish @ 2006-07-07 17:14 UTC (permalink / raw
To: gentoo-user
On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> but I don't have just one main.c, I have 20+ different apps, each with
> their own main.c, which I don't maintain, for which I'd have to issue
Ah, ok, sorry, I missed that part.
So, can I ask /why/ you need the process name in this class?
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 17:14 ` Richard Fish
@ 2006-07-07 20:53 ` Uwe Thiem
2006-07-07 21:54 ` Richard Fish
0 siblings, 1 reply; 16+ messages in thread
From: Uwe Thiem @ 2006-07-07 20:53 UTC (permalink / raw
To: gentoo-user
On 07 July 2006 18:14, Richard Fish wrote:
> On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> > but I don't have just one main.c, I have 20+ different apps, each with
> > their own main.c, which I don't maintain, for which I'd have to issue
>
> Ah, ok, sorry, I missed that part.
>
> So, can I ask /why/ you need the process name in this class?
How about:
getenv( "_" );
Uwe
--
Mark Twain: I rather decline two drinks than a German adjective.
http://www.SysEx.com.na
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 20:53 ` Uwe Thiem
@ 2006-07-07 21:54 ` Richard Fish
2006-07-10 0:00 ` Iain Buchanan
0 siblings, 1 reply; 16+ messages in thread
From: Richard Fish @ 2006-07-07 21:54 UTC (permalink / raw
To: gentoo-user
On 7/7/06, Uwe Thiem <uwix@iway.na> wrote:
> How about:
>
> getenv( "_" );
Well, that is set by bash before it exec's anything, so that should
work, provided the program[s] are always exec'd by bash and not by
something else. It would be nicer if "_" was set by the kernel, or
even glibc.
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 2:37 ` Iain Buchanan
2006-07-07 2:55 ` Richard Fish
@ 2006-07-07 22:05 ` Richard Fish
1 sibling, 0 replies; 16+ messages in thread
From: Richard Fish @ 2006-07-07 22:05 UTC (permalink / raw
To: gentoo-user
On 7/6/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> Someone mentioned that /proc is deprecated for 2.6? is this correct? Is
> there a /sys alternative to /proc/self/status?
Oh, I don't think /proc itself is deprecated. After all, ps, pstree,
top and so on still use it. And it is not mentioned in
/usr/src/linux/Documentation/feature-removal-schedule.txt. There are
some things in /proc that will go away (/proc/pci, for example), but
it seems that /proc should be around for a good while yet. It is
theoretically possible that someone could build a kernel without proc
filesystem support, but I'm not sure how useful such a kernel would
be!
Also, if I were going to use /proc for this (and I'm not saying I
would, I really don't like it, but it's not my project), I would
either read /proc/self/cmdline or do a readlink on /proc/self/exe.
The readlink option would give you the actual path to the program
being run.
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] OT: get process name in c++
2006-07-07 21:54 ` Richard Fish
@ 2006-07-10 0:00 ` Iain Buchanan
0 siblings, 0 replies; 16+ messages in thread
From: Iain Buchanan @ 2006-07-10 0:00 UTC (permalink / raw
To: gentoo-user
On Fri, 2006-07-07 at 14:54 -0700, Richard Fish wrote:
> On 7/7/06, Uwe Thiem <uwix@iway.na> wrote:
> > How about:
> >
> > getenv( "_" );
>
> Well, that is set by bash before it exec's anything, so that should
> work, provided the program[s] are always exec'd by bash and not by
> something else. It would be nicer if "_" was set by the kernel, or
> even glibc.
well, they are forked by start-stop-daemon in an init script at the
moment, which is in turn run by /sbin/runscript, which is run by bash...
as to why: Firstly there are various log message functions defined that
print the name of the program calling the log function, the log message
(and then do some other stuff depending on what type of message -
critical, info, etc). Secondly, and more importantly, no task should
have multiple copies of itself running, or "bad things" will happen
(there's quite a bit of shared memory which would get corrupted). And
some tasks require that a whole list of other tasks are not running.
QNX used to have some functions to register a name with the system to
stop this (qnx_name_locate, etc), but linux doesn't... that I can see...
cya,
--
Iain Buchanan <iaindb at netspace dot net dot au>
Having the fewest wants, I am nearest to the gods.
-- Socrates
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* [gentoo-user] Re: OT: get process name in c++
[not found] <1151499883.17966.13.camel@orpheus>
2006-06-28 13:51 ` [gentoo-user] OT: get process name in c++ Petr Uzel
2006-06-28 15:55 ` Richard Fish
@ 2006-07-27 0:19 ` Iain Buchanan
2006-07-27 4:35 ` Richard Fish
2 siblings, 1 reply; 16+ messages in thread
From: Iain Buchanan @ 2006-07-27 0:19 UTC (permalink / raw
To: gentoo-user
Hi,
I posted this email a short while ago, and got some great tips for
accessing /proc/self/status to read the process name. However, guess
what I just found (after I've finished and tested my /proc/
implementation?)
man 3 program_invocation_short_name:
NAME
program_invocation_name, program_invocation_short_name - obtain name
used to invoke calling program
...
DESCRIPTION
program_invocation_name contains the name that was used to invoke the
calling program. This is the same as the value of argv[0] in main(),
with the difference that the scope of program_invocation_name is
global.
cool! Oh well, reading /proc/ was an experience :)
On Wed, 2006-06-28 at 22:34 +0930, Iain Buchanan wrote:
> Hi again :)
>
> I have been googling for this one for a little while, and trying various
> options, but I can't seem to find it: How do I get the process from
> within a process in c or c++?
>
> I don't want to use argv[0], because the particular place is deep within
> classes, and there are too many (I'm doing some porting) to edit all of
> their constructors, for example, to pass argv[0].
>
> something like
>
> char *name = getpsname (pid);
>
> But does that exist? Maybe there's an interface to /proc that I'm
> unaware of?
>
> I'd appreciate any tips!
>
> thanks,
cya,
--
Iain Buchanan <iaindb at netspace dot net dot au>
If you are shooting under 80 you are neglecting your business;
over 80 you are neglecting your golf.
-- Walter Hagen
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [gentoo-user] Re: OT: get process name in c++
2006-07-27 0:19 ` [gentoo-user] " Iain Buchanan
@ 2006-07-27 4:35 ` Richard Fish
0 siblings, 0 replies; 16+ messages in thread
From: Richard Fish @ 2006-07-27 4:35 UTC (permalink / raw
To: gentoo-user
On 7/26/06, Iain Buchanan <iaindb@netspace.net.au> wrote:
> Hi,
>
> I posted this email a short while ago, and got some great tips for
> accessing /proc/self/status to read the process name. However, guess
> what I just found (after I've finished and tested my /proc/
> implementation?)
>
> man 3 program_invocation_short_name:
/me hides head in sand.
I guess the glibc folks are smarter than both of us! :-)
-Richard
--
gentoo-user@gentoo.org mailing list
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2006-07-27 4:40 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <1151499883.17966.13.camel@orpheus>
2006-06-28 13:51 ` [gentoo-user] OT: get process name in c++ Petr Uzel
2006-06-28 14:01 ` Petr Uzel
2006-06-29 0:47 ` Iain Buchanan
2006-07-07 2:37 ` Iain Buchanan
2006-07-07 2:55 ` Richard Fish
2006-07-07 4:35 ` Iain Buchanan
2006-07-07 4:48 ` Richard Fish
2006-07-07 5:05 ` Iain Buchanan
2006-07-07 17:14 ` Richard Fish
2006-07-07 20:53 ` Uwe Thiem
2006-07-07 21:54 ` Richard Fish
2006-07-10 0:00 ` Iain Buchanan
2006-07-07 22:05 ` Richard Fish
2006-06-28 15:55 ` Richard Fish
2006-07-27 0:19 ` [gentoo-user] " Iain Buchanan
2006-07-27 4:35 ` Richard Fish
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox