设置线程/proc/PID/cmdline?

发布于 2024-07-06 09:27:10 字数 222 浏览 4 评论 0原文

Linux/NPTL上,线程被创建为某种进程。

我可以看到我的一些进程有一个奇怪的命令行:

cat /proc/5590/cmdline 
hald-addon-storage: polling /dev/scd0 (every 2 sec)

您知道如何为进程的每个线程执行此操作吗? 这对于调试非常有帮助。

On Linux/NPTL, threads are created as some kind of process.

I can see some of my process have a weird cmdline:

cat /proc/5590/cmdline 
hald-addon-storage: polling /dev/scd0 (every 2 sec)

Do you have an idea how I could do that for each thread of my process? That would be very helpful for debugging.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-07-13 09:27:10

如果您想以可移植的方式执行此操作,并且可以跨多个 Unix 变体运行,那么可用的选项非常少。

您需要做的是,调用者进程必须使用指向您希望在进程输出中看到的名称的 argv [0] 参数调用 exec,并且文件名指向实际的可执行文件。

您可以通过使用以下命令从 shell 尝试此行为:

exec -a "This is my cute name" bash

这会将当前的 bash 进程替换为名为 “这是我可爱的名字” 的进程。

要在 C 中执行此操作,您可以查看 sendmail 或任何其他已广泛移植的软件的源代码,并找到跨操作系统支持此操作所需的所有变体。

某些操作系统具有 setproctitle(3) API,另一些操作系统则允许您覆盖 argv [0] 的内容并显示结果。

If you want to do this in a portable way, something that will work across multiple Unix variations, there are very few options available.

What you have to do is that your caller process must call exec with the argv [0] argument pointing to the name that you would like to see in the process output, and the filename pointing to the actual executable.

You can try this behavior from the shell by using:

exec -a "This is my cute name" bash

That will replace the current bash process with one named "This is my cute name".

For doing this in C, you can look at the source code of sendmail or any other piece of software that has been ported extensively and find all the variations that are needed across operating systems to support this.

Some operating systems have a setproctitle(3) API, some others allow you to override the contents of argv [0] and show that result.

梦旅人picnic 2024-07-13 09:27:10

argv 指向可写字符串。 只需写一些东西给他们:

#include <string.h>
#include <unistd.h>

int
main(int argc, char** argv)
{
    strcpy(argv[0], "Hello, world!");
    sleep(10);
    return 0;
}

argv points to writable strings. Just write stuff to them:

#include <string.h>
#include <unistd.h>

int
main(int argc, char** argv)
{
    strcpy(argv[0], "Hello, world!");
    sleep(10);
    return 0;
}
小姐丶请自重 2024-07-13 09:27:10

呸..代码不是那么好,诀窍是重用环境(这里是argv_buffer)指针:

memset (argv_buffer[0] + len, 0, argv_size - len);
argv_buffer[1] = NULL;

有更好的主意吗?

这适用于不同的线程吗?

Bah.. the code is not that nice, the trick is to reuse the environ (here argv_buffer) pointer:

memset (argv_buffer[0] + len, 0, argv_size - len);
argv_buffer[1] = NULL;

Any better idea?

Is that working for different threads?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文