如何在Linux pthreads中设置线程的名称? [英] How to set the name of a thread in Linux pthreads?

查看:1260
本文介绍了如何在Linux pthreads中设置线程的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中是否可以设置线程名称?

Is there any way of setting the name of a thread in Linux?

我的主要目的是在调试时会有所帮助,并且如果该名称是通过(例如)公开的,那也很好. /proc/$PID/task/$TID/...

My main purpose is it would be helpful while debugging, and also nice if that name was exposed through e.g. /proc/$PID/task/$TID/...

推荐答案

在选项PR_SET_NAME中使用prctl(2)函数(请参见

Use the prctl(2) function with the option PR_SET_NAME (see the docs).

请注意,旧版本的文档有些令人困惑.他们说

Note that old versions of the docs are a bit confusing. They say

设置调用进程的进程名称

Set the process name for the calling process

但是由于线程在Linux上是轻量级进程(LWP),因此在这种情况下,一个线程就是一个进程.

but since threads are light weight processes (LWP) on Linux, one thread is one process in this case.

您可以通过ps -o cmd或通过以下方式查看线程名称:

You can see the thread name with ps -o cmd or with:

cat /proc/$PID/task/$TID/comm

或在cat /proc/$PID/task/$TID/stat()之间:

4223 (kjournald) S 1 1 1 0...

或来自GDB info threads的双引号之间:

or from GDB info threads between double quotes:

* 1    Thread 0x7ffff7fc7700 (LWP 6575) "kjournald" 0x00007ffff78bc30d in nanosleep () at ../sysdeps/unix/syscall-template.S:84                                                                                  

这篇关于如何在Linux pthreads中设置线程的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆