我正在使用 Ubuntu 20.04 LTS。内核版本为5.4.0-42。
这是一个示例程序:
// mre.c
// Compile with: cc -o mre mre.c -lSDL2
#include <stdio.h>
#include <SDL2/SDL.h>
int main(void)
{
SDL_Init(SDL_INIT_VIDEO); // Doesn't work without SDL_INIT_VIDEO
getchar();
}
./mre
当我查看打开线程名称的正在运行的程序时htop
,我看到它有以下四个线程:
mre:disk$3
mre:disk$2
mre:disk$1
mre:disk$0
以下是一些/usr/libexec/ibus-x11
具有相似名称的线程:
ibus-x1:disk$3
ibus-x1:disk$2
ibus-x1:disk$1
ibus-x1:disk$0
许多程序没有它们(也许它们没有使用特定的图形界面?)
此类线程总是四个(我的计算机有四个核心)并按降序列出。/usr/lib/xorg/Xorg
有八个这样的线程,每个数字 0-3 有两个。它们是做什么用的?
答案1
这些线程用于台面磁盘缓存:
util_queue_init(&cache->cache_queue, "disk$", 32, 4,
UTIL_QUEUE_INIT_RESIZE_IF_FULL |
UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY |
UTIL_QUEUE_INIT_SET_FULL_THREAD_AFFINITY);
https://sources.debian.org/src/mesa/22.0.3-1/src/util/disk_cache.c/?hl=174#L174
然后里面util_queue_init()
:
/* Form the thread name from process_name and name, limited to 13
* characters. Characters 14-15 are reserved for the thread number.
* Character 16 should be 0. Final form: "process:name12"
*
* If name is too long, it's truncated. If any space is left, the process
* name fills it.
*/
https://sources.debian.org/src/mesa/22.0.3-1/src/util/u_queue.c/?hl=405#L414-L420
因此,以某种方式调用该台面代码的所有 GUI 进程都会创建这些额外的线程,例如在我的 f33 桌面系统上:
pid tid comm cls
1942 1989 gnome-s:disk$0 BAT
1942 1990 gnome-s:disk$1 BAT
1942 1991 gnome-s:disk$2 BAT
1942 1992 gnome-s:disk$3 BAT
2041 2237 Xwaylan:disk$0 BAT
2041 2238 Xwaylan:disk$1 BAT
2041 2239 Xwaylan:disk$2 BAT
2041 2240 Xwaylan:disk$3 BAT
2041 2259 Xwaylan:disk$0 BAT
2041 2260 Xwaylan:disk$1 BAT
2041 2261 Xwaylan:disk$2 BAT
2041 2262 Xwaylan:disk$3 BAT
2292 2325 gsd-xse:disk$0 BAT
2292 2326 gsd-xse:disk$1 BAT
2292 2327 gsd-xse:disk$2 BAT
2292 2328 gsd-xse:disk$3 BAT
2307 2344 ibus-x1:disk$0 BAT
2307 2345 ibus-x1:disk$1 BAT
2307 2346 ibus-x1:disk$2 BAT
2307 2347 ibus-x1:disk$3 BAT
2464 2578 firefox:disk$0 BAT
2464 2579 firefox:disk$1 BAT
2464 2580 firefox:disk$2 BAT
2464 2581 firefox:disk$3 BAT
2756 2785 firefox:disk$0 BAT
2756 2786 firefox:disk$1 BAT
2756 2787 firefox:disk$2 BAT
2756 2788 firefox:disk$3 BAT
2806 2841 firefox:disk$0 BAT
2806 2842 firefox:disk$1 BAT
2806 2843 firefox:disk$2 BAT
2806 2844 firefox:disk$3 BAT
2919 3078 firefox:disk$0 BAT
2919 3079 firefox:disk$1 BAT
2919 3080 firefox:disk$2 BAT
2919 3081 firefox:disk$3 BAT
3346 3367 firefox:disk$0 BAT
3346 3368 firefox:disk$1 BAT
3346 3369 firefox:disk$2 BAT
3346 3370 firefox:disk$3 BAT
3408 3426 firefox:disk$0 BAT
3408 3427 firefox:disk$1 BAT
3408 3428 firefox:disk$2 BAT
3408 3429 firefox:disk$3 BAT
5794 5825 firefox:disk$0 BAT
5794 5826 firefox:disk$1 BAT
5794 5827 firefox:disk$2 BAT
5794 5828 firefox:disk$3 BAT
6345 6364 firefox:disk$0 BAT
6345 6365 firefox:disk$1 BAT
6345 6366 firefox:disk$2 BAT
6345 6367 firefox:disk$3 BAT
9502 9525 firefox:disk$0 BAT
9502 9526 firefox:disk$1 BAT
9502 9527 firefox:disk$2 BAT
9502 9528 firefox:disk$3 BAT
22548 22565 firefox:disk$0 BAT
22548 22566 firefox:disk$1 BAT
22548 22567 firefox:disk$2 BAT
22548 22568 firefox:disk$3 BAT
33788 33807 vlc:disk$0 BAT
33788 33808 vlc:disk$1 BAT
33788 33809 vlc:disk$2 BAT
33788 33810 vlc:disk$3 BAT
48178 74574 kwallet:disk$0 BAT
48178 74575 kwallet:disk$1 BAT
48178 74576 kwallet:disk$2 BAT
48178 74577 kwallet:disk$3 BAT
60824 60830 chromiu:disk$0 BAT
60824 60831 chromiu:disk$1 BAT
60824 60832 chromiu:disk$2 BAT
60824 60833 chromiu:disk$3 BAT
69502 69519 firefox:disk$0 BAT
69502 69520 firefox:disk$1 BAT
69502 69521 firefox:disk$2 BAT
69502 69522 firefox:disk$3 BAT