Se você tiver htop
, você pode pressionar Shift + k para alternar a exibição dos encadeamentos do kernel. Se você pressionar F5 para o modo árvore, todos eles devem aparecer como filhos de kthreadd
.
There are some visible differences between a kernel thread and a user-space thread:
/proc/$pid/cmdline
is empty for kernel threads - this is the method used by ps and top to distinguish kernel threads.The
/proc/$pid/exe
symbolic link has no target for kernel threads - which makes sense since they do not have a corresponding executable on the filesystem.More specifically, the
readlink()
system call returnsENOENT
("No such file or directory"), despite the fact that the link itself exists, to denote the fact that the executable for this process does not exist (and never did).Therefore, a reliable way to check for kernel threads should be to call
readlink()
on/proc/$pid/exe
and check its return code. If it succeeds then$pid
is a user process. If it fails withENOENT
, then an extrastat()
on/proc/$pid/exe
should tell apart the case of a kernel thread from a process that has just terminated.
/proc/$pid/status
is missing several fields for most kernel threads - more specifically a few fields related to virtual memory.
A resposta acima de Identificando os tópicos do kernel
Outra maneira de distinguir os encadeamentos do kernel de outro processo é executar top -c
. Do manual top
:
3. COMMAND -- Command Name or Command Line
Display the command line used to start a task or the name of the associated program. You toggle between command line and name with 'c', which is both a command-line option and an interactive com‐ mand.When you've chosen to display command lines, processes without a command line (like kernel threads) will be shown with only the program name in brackets, as in this example:
[ mdrecoveryd ]
A execução de ps aux
também exibe processos que foram iniciados sem um comando entre colchetes (e terão um arquivo /proc/[pid]/cmdline
vazio).
Exemplo:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 19 0.0 0.0 0 0 ? S< Mar02 0:00 [kworker/1:0H]
Veja o pacote procps-3.2.8
arquivo /proc/readproc.h
.
// Basic data structure which holds all information we can get about a process.
// (unless otherwise specified, fields are read from /proc/#/stat)
//
// Most of it comes from task_struct in linux/sched.h