Linux 进程 ID 和线程 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8565880/
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-06 03:43:56  来源:igfitidea点击:

Linux process ID and thread ID

c++clinuxprocesspthreads

提问by Terry Li

Suppose we have many user processes running on Linux. Each process has many threads running.

假设我们有许多用户进程在 Linux 上运行。每个进程都有许多线程在运行。

I can get process ID by calling getpid(), the return value of which is an integer.

我可以通过调用获取进程ID getpid(),它的返回值是一个整数。

I can get thread ID by calling pthread_self(), the return value of which is an opaque type called pthread_t.

我可以通过调用获取线程 ID pthread_self(),其返回值是一个名为 的不透明类型pthread_t

Now I need to store the process ID (an int, typically 4 bytes) and the thread ID (pthread_t, need to figure out how many bytes) in shared memory so that I can later use the two pieces of ID information to identify that specific thread and to check if the thread is still running or not.

现在我需要pthread_t在共享内存中存储进程 ID(一个 int,通常为 4 个字节)和线程 ID(需要计算出多少字节),以便我以后可以使用这两个 ID 信息来识别该特定线程并检查线程是否仍在运行。

I've found many online sources cast pthread_tto either unsigned intor unsigned long. Since I don't want any data loss during the casting, how should I deal with the pthread_tdata so that it's a fixed-size piece of data (as mentioned, I need to store the thread information in shared memory).

我发现许多在线资源都pthread_t转换为unsigned intunsigned long。由于我不想在转换过程中丢失任何数据,我应该如何处理pthread_t数据,使其成为固定大小的数据(如前所述,我需要将线程信息存储在共享内存中)。

Also, how should I identify that specific thread by combination of process ID and thread ID later? How to check if the thread is still running or not?

另外,稍后我应该如何通过进程 ID 和线程 ID 的组合来识别该特定线程?如何检查线程是否仍在运行?

采纳答案by Dan Fego

If you want to store pid_tand pthread_tanywhere, you should use their respective types (i.e. "pid_t" and "pthread_t"). So if you want to store them in shared memory somewhere, do a memcpy()to get them there.

如果你想存储pid_tpthread_t任何地方,你应该利用各自的类型(即“将为pid_t”和“的pthread_t”)。因此,如果您想将它们存储在共享内存中的某个地方,请执行 amemcpy()将它们放在那里。

As far as identifying specific threads by combinations of PID and TID, see Nemo's comment.

至于通过 PID 和 TID 的组合识别特定线程,请参阅 Nemo 的评论。

If you do make the assumption that they will exist, you can have your program look at /procto find the appropriate pid's directory, and looking in /proc/<pid>/taskfor the threads.

如果您确实假设它们将存在,您可以让您的程序查看/proc以找到适当的 pid 目录,并查找/proc/<pid>/task线程。

回答by Matteo Italia

Why don't you just pack them in a struct?

你为什么不把它们打包成一个struct

typedef struct
{
    int procID;
    pthread_t threadID;

} ProcThreadID;

without worrying about the specific underlying type of pthread_t(after all we are in C, so everything is POD and can be copied blindly with memcpy).

不用担心具体的底层类型pthread_t(毕竟我们是C,所以一切都是POD,可以盲目复制memcpy)。

You can get its size easily using the sizeofoperator:

您可以使用sizeof运算符轻松获取其大小:

size_t ptIDSize = sizeof(ProcThreadID);

and you can copy it wherever you want with a simple memcpy.

您可以使用简单的memcpy.

回答by Chris Mansley

You can use pthread_joinas a crude way of detecting completion, but I am sure that is not what you want. Instead you must handle this yourself by creating a thread complete flag. A nice way of setting this flag is in the pthread cleanup handlers. See this related post

您可以将其pthread_join用作检测完成情况的粗略方法,但我确信这不是您想要的。相反,您必须通过创建线程完成标志来自己处理这个问题。设置此标志的一个好方法是在 pthread 清理处理程序中。看到这个相关的帖子

回答by Vinayak S M

command to get thread id's running in a process
$ ps -eLf | grep 14965

UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 14965 14732 14965 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14966 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14967 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14968 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14969 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14970 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14971 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14972 0 201 15:28 pts/10 00:00:00 ./a.out


Here the 4th column (LWP) shows all the threads running in process with ID 14965

获取线程 ID 在进程中运行的命令
$ ps -eLf | grep 14965 这里的第 4 列 (LWP) 显示了 ID 为 14965 的进程中运行的所有线程

UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 14965 14732 14965 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14966 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14967 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14968 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14969 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14970 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14971 0 201 15:28 pts/10 00:00:00 ./a.out
root 14965 14732 14972 0 201 15:28 pts/10 00:00:00 ./a.out