如何通过`bash`判断一个线程是不是内核线程?
How to identify a thread is a kernel thread or not through `bash`?
如何通过bash
判断一个线程是不是内核线程?
我发现可以通过ps
来识别内核线程:如果线程名被[]
括起来,就是内核线程。但我认为这不是一个好的解决方案。
对于这个问题的任何提示,我将不胜感激。
您可以通过查看 /proc/<PID>/stat
来确定特定任务是否为 kthread。更准确地说,根据man 5 proc
the 9th field of this virtual file contains the kernel flags for the process. In case of a kthread, the flags will have PF_KTHREAD
设置。
下面是一个 Bash 脚本示例,它以 PID 作为参数并检查它是否是 kthread:
#!/bin/bash
read -a stats < /proc//stat
flags=${stats[8]}
if (( ($flags & 0x00200000) == 0x00200000 )); then
echo 'KTHREAD'
else
echo 'NOT KTHREAD'
fi
这并不比仅执行 ps u -p <PID>
并检查 []
简单,但它仍然是一个纯粹的 Bash 解决方案。
还有一些"tricks"可用于识别内核线程,很容易修改上述脚本以使用this post中突出显示的方法之一。
如何通过bash
判断一个线程是不是内核线程?
我发现可以通过ps
来识别内核线程:如果线程名被[]
括起来,就是内核线程。但我认为这不是一个好的解决方案。
对于这个问题的任何提示,我将不胜感激。
您可以通过查看 /proc/<PID>/stat
来确定特定任务是否为 kthread。更准确地说,根据man 5 proc
the 9th field of this virtual file contains the kernel flags for the process. In case of a kthread, the flags will have PF_KTHREAD
设置。
下面是一个 Bash 脚本示例,它以 PID 作为参数并检查它是否是 kthread:
#!/bin/bash
read -a stats < /proc//stat
flags=${stats[8]}
if (( ($flags & 0x00200000) == 0x00200000 )); then
echo 'KTHREAD'
else
echo 'NOT KTHREAD'
fi
这并不比仅执行 ps u -p <PID>
并检查 []
简单,但它仍然是一个纯粹的 Bash 解决方案。
还有一些"tricks"可用于识别内核线程,很容易修改上述脚本以使用this post中突出显示的方法之一。