在 linux (htop) 上更改线程名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4461289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:19:23  来源:igfitidea点击:

change thread name on linux (htop)

clinuxpthreads

提问by Gaetano Mendola

I have a multithread application and I would like that htop (as example) shows a different name per each thread running, at the moment what it shows is the "command line" used to run the main.

我有一个多线程应用程序,我希望 htop(例如)为每个运行的线程显示不同的名称,目前它显示的是用于运行主线程的“命令行”。

I have tried using

我试过使用

prctl(PR_SET_NAME, .....)

but it works only with top and with that call is only possible specify names up to 16 bytes.

但它仅适用于 top 并且该调用只能指定最多 16 个字节的名称。

I guess the trick is to modify the /proc/PID/cmdline content but that is a readonly field.

我想诀窍是修改 /proc/PID/cmdline 内容,但这是一个只读字段。

Anyone knows how to achieve it ?

任何人都知道如何实现它?

回答by llongi

You have to distinguish between per-thread and per-process setting here.

您必须在此处区分每线程和每进程设置。

prctl(PR_SET_NAME, ...) sets the name (up to 16 bytes) on a per-thread basis, and you can force "ps" to show that name with the c switch (ps Hcx for example). You can do the same with the c switch in top, so I assume htop has similar functionality.

prctl(PR_SET_NAME, ...) 在每个线程的基础上设置名称(最多 16 个字节),您可以强制“ps”使用 c 开关显示该名称(例如 ps Hcx)。您可以对 top 中的 c 开关执行相同操作,因此我假设 htop 具有类似的功能。

What "ps" normally shows you (ps Hax for example) is the command line name and arguments you started your program with (indeed what /proc/PID/cmdline tells you), and you can modify those by directly modifying argv[0] (up to its original length), but that is a per-process setting, meaning you cannot give different names to different threads that way.

“ps”通常向您显示(例如 ps Hax)是您启动程序时使用的命令行名称和参数(实际上是 /proc/PID/cmdline 告诉您的),您可以通过直接修改 argv[0] 来修改它们(最多达到其原始长度),但这是每个进程的设置,这意味着您不能以这种方式为不同的线程指定不同的名称。

Following is the code I normally use to change the process name as a whole:

以下是我通常用来整体更改进程名称的代码:

// procname is the new process name
char *procname = "new process name";

// Then let's directly modify the arguments
// This needs a pointer to the original arvg, as passed to main(),
// and is limited to the length of the original argv[0]
size_t argv0_len = strlen(argv[0]);
size_t procname_len = strlen(procname);
size_t max_procname_len = (argv0_len > procname_len) ? (procname_len) : (argv0_len);

// Copy the maximum
strncpy(argv[0], procname, max_procname_len);
// Clear out the rest (yes, this is needed, or the remaining part of the old
// process name will still show up in ps)
memset(&argv[0][max_procname_len], '##代码##', argv0_len - max_procname_len);

// Clear the other passed arguments, optional
// Needs to know argv and argc as passed to main()
//for (size_t i = 1; i < argc; i++) {
//  memset(argv[i], '##代码##', strlen(argv[i]));
//}

回答by Drew Noakes

Since version 0.8.4, htophas an option: Show custom thread names

从 0.8.4 版本开始,htop有一个选项:显示自定义线程名称

Press F2and select the Display optionsmenu. You should see:

F2并选择Display options菜单。你应该看到:

htop custom thread names

htop 自定义线程名称