如何将ThreadId转换为(LWP) PID?

huangapple go评论64阅读模式
英文:

How to convert ThreadId to (LWP) PID?

问题

有两个函数:

forkIO :: IO () -> IO ThreadId
forkOS :: IO () -> IO ThreadId

线程ID是data ThreadId,从中只能获取这些信息(在内部,它是data ThreadId = ThreadId ThreadId#)。因此,它的“参数”不可用,但与地址相关,而不是PID。同时,Haskell线程(即POSIX线程)在Linux中映射到某个LWP(对于其他操作系统如Mac和Windows,我不太清楚),我猜想是N:M的映射。我可以在Linux上使用ps -AL列出它们:

........
15001 15001 ?        00:00:00 haskell-languag
15001 15177 ?        00:00:02 ghc_ticker
15001 15178 ?        00:00:56 haskell-langu:w
15001 15179 ?        00:00:47 haskell-langu:w
15001 15180 ?        00:01:00 haskell-langu:w
15001 15181 ?        00:00:56 haskell-langu:w
15001 15182 ?        00:00:07 haskell-langu:w
15001 15183 ?        00:00:57 haskell-langu:w
........

是否有一种方法可以从ThreadId中找到PID(可能是LWP)?相关问题是:如果存在这种方法,它是否跨平台(对于其他操作系统是否有意义),例如在OsX和Windows上?

英文:

There are 2 functions:

forkIO :: IO () -> IO ThreadId
forkOS :: IO () -> IO ThreadId

and the thread ID is data ThreadId - that's all what it is available from it (internally it's data ThreadId = ThreadId ThreadId#). So, its "argument" is not available but it is related to an address, not to PID. At the same time Haskell threads (ie, POSIX threads) are mapped to some LWP in Linux (no idea about other OSes, like Mac, Windows), I suppose as N:M. I can list them in Linux box with ps -AL:

........
15001 15001 ?        00:00:00 haskell-languag
15001 15177 ?        00:00:02 ghc_ticker
15001 15178 ?        00:00:56 haskell-langu:w
15001 15179 ?        00:00:47 haskell-langu:w
15001 15180 ?        00:01:00 haskell-langu:w
15001 15181 ?        00:00:56 haskell-langu:w
15001 15182 ?        00:00:07 haskell-langu:w
15001 15183 ?        00:00:57 haskell-langu:w
........

Is there a way to find PID (possibly LWP) from ThreadId? And related question: will it be cross-platform (to make sense for other OSes) - on OsX, Windows, if such way exists?

答案1

得分: 3

Unbound threads, those created by forkIO, are not tied to any specific POSIX / OS thread. They might be run on an OS thread for a while, and then continue on another OS thread. Even if you found a way to get their current PID, that information can become out-of-date quickly.

Bound threads, those created by forkOS, are instead tied to a specific POSIX / OS thread. These threads have access to thread-local state, and can call C libraries that make use of such state. I can't find a way to get the PID from the thread ID, but I guess you might get the current PID in the thread and advertise it to the other threads in some way (e.g. using shared state variables like MVars).

Even with bound threads, I believe the OS thread with that PID is not exclusively dedicated to the bound thread. The mapping Haskell_threads:OS_threads is still not 1:1.

英文:

Unbound threads, those created by forkIO, are not tied to any specific POSIX / OS thread. They might be run on an OS thread for a while, and then continue on another OS thread. Even if you found a way to get their current PID, that information can become out-of-date quickly.

Bound threads, those created by forkOS, are instead tied to a specific POSIX / OS thread. These threads have access to thread-local state, and can call C libraries that make use of such state. I can't find a way to get the PID from the thread ID, but I guess you might get the current PID in the thread and advertise it to the other threads in some way (e.g. using shared state variables like MVars).

Even with bound threads, I believe the OS thread with that PID is not exclusively dedicated to the bound thread. The mapping Haskell_threads:OS_threads is still not 1:1.

huangapple
  • 本文由 发表于 2023年6月22日 15:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定