如何在linux中获取当前进程名称?

时间:2023-01-13 16:02:28

How can I get the process name in C? The same name, which is in /proc/$pid/status. I do not want to parse that file. Is there any programmatic way of doing this?

如何在C中获取进程名称?同名,在/ proc / $ pid / status中。我不想解析该文件。有没有任何程序化的方法来做到这一点?

7 个解决方案

#1


22  

It's either pointed to by the argv[0] or indeed you can read /proc/self/status. Or you can use getenv("_"), not sure who sets that and how reliable it is.

它是由argv [0]指向的,或者你可以读取/ proc / self / status。或者您可以使用getenv(“_”),不确定是谁设置它以及它有多可靠。

#2


33  

If you're on using a glibc, then:

如果你正在使用glibc,那么:

#define _GNU_SOURCE
#include <errno.h>

extern char *program_invocation_name;
extern char *program_invocation_short_name;

See program_invocation_name(3)

见program_invocation_name(3)

Under most Unices, __progname is also defined by the libc. The sole portable way is to use argv[0]

在大多数Unices下,__ protame也由libc定义。唯一可移植的方法是使用argv [0]

#3


10  

You can use __progname. However it is not better than argv[0] as it may have portability issues. But as you do not have access to argv[0] it can work as follows:-

您可以使用__progname。然而,它并不比argv [0]好,因为它可能有可移植性问题。但由于您无法访问argv [0],因此可以按以下方式工作: -

extern char *__progname;
printf("\n%s", __progname);

#4


5  

Look at the value of argv[0] which was passed to main. This should be the name under which your process was invoked.

查看传递给main的argv [0]的值。这应该是调用进程的名称。

#5


5  

I often make use of following call,

我经常使用以下电话,

char* currentprocname = getprogname();

#6


1  

If you cannot access argv[] in main(), because you are implementing a library, you can have a look at my answer on a similar question here.

如果你不能访问main()中的argv [],因为你正在实现一个库,你可以在这里查看我对类似问题的答案。

It basically boils down into giving you access to argc, argv[] and envp[] outside of main(). Then you could, as others have already correctly suggested, use argv[0] to retrieve the process name.

它基本上归结为允许您访问main()之外的argc,argv []和envp []。然后,您可以像其他人已经正确建议的那样,使用argv [0]来检索进程名称。

#7


0  

This is a version that works on macOS, FreeBSD and Linux.

这是一个适用于macOS,FreeBSD和Linux的版本。

#if defined(__APPLE__) || defined(__FreeBSD__)
const char * appname = getprogname();
#elif defined(_GNU_SOURCE)
const char * appname = program_invocation_name;
#else
const char * appname = "?";
#endif

#1


22  

It's either pointed to by the argv[0] or indeed you can read /proc/self/status. Or you can use getenv("_"), not sure who sets that and how reliable it is.

它是由argv [0]指向的,或者你可以读取/ proc / self / status。或者您可以使用getenv(“_”),不确定是谁设置它以及它有多可靠。

#2


33  

If you're on using a glibc, then:

如果你正在使用glibc,那么:

#define _GNU_SOURCE
#include <errno.h>

extern char *program_invocation_name;
extern char *program_invocation_short_name;

See program_invocation_name(3)

见program_invocation_name(3)

Under most Unices, __progname is also defined by the libc. The sole portable way is to use argv[0]

在大多数Unices下,__ protame也由libc定义。唯一可移植的方法是使用argv [0]

#3


10  

You can use __progname. However it is not better than argv[0] as it may have portability issues. But as you do not have access to argv[0] it can work as follows:-

您可以使用__progname。然而,它并不比argv [0]好,因为它可能有可移植性问题。但由于您无法访问argv [0],因此可以按以下方式工作: -

extern char *__progname;
printf("\n%s", __progname);

#4


5  

Look at the value of argv[0] which was passed to main. This should be the name under which your process was invoked.

查看传递给main的argv [0]的值。这应该是调用进程的名称。

#5


5  

I often make use of following call,

我经常使用以下电话,

char* currentprocname = getprogname();

#6


1  

If you cannot access argv[] in main(), because you are implementing a library, you can have a look at my answer on a similar question here.

如果你不能访问main()中的argv [],因为你正在实现一个库,你可以在这里查看我对类似问题的答案。

It basically boils down into giving you access to argc, argv[] and envp[] outside of main(). Then you could, as others have already correctly suggested, use argv[0] to retrieve the process name.

它基本上归结为允许您访问main()之外的argc,argv []和envp []。然后,您可以像其他人已经正确建议的那样,使用argv [0]来检索进程名称。

#7


0  

This is a version that works on macOS, FreeBSD and Linux.

这是一个适用于macOS,FreeBSD和Linux的版本。

#if defined(__APPLE__) || defined(__FreeBSD__)
const char * appname = getprogname();
#elif defined(_GNU_SOURCE)
const char * appname = program_invocation_name;
#else
const char * appname = "?";
#endif