-
Notifications
You must be signed in to change notification settings - Fork 580
Linux kernel thread criteria is wrong #1001
Comments
I could not find any document that guarentees this behavior. Plus, Even if we are dropping support for older kernels, there are better ways to do this: https://stackoverflow.com/a/56369641/3342190 . It is well documented, and works for 2.6+. This |
This simply asserts that it is the method, but doesn't cite anything. I took a cursory glance through the source of procps-ng and I can't find any feature in
...which doesn't actually say that they distinguish kthreads from user threads, just that kthreads (having no cmdline) will be displayed like so.
Indeed there isn't, though it has been this way ever since But yeah I agree, it's probably better to go with checking For kernels < v2.6.27 (which we should check via uname or such) when PF_KTHREAD was introduced, then we can fall back to checking cmdline. I don't think this current way of checking for kthread is correct anyway, because it is entirely possible for user threads to end up with a NULL cmdline. (That's the entire reason I stumbled upon this issue. I was confused for 10-15 minutes why some process appeared in |
A check or switch will be great. It's here if you wan't to make a PR: |
It's possible for user tasks to have an empty
cmdline
too, ifargv
wasNULL
when it was exec-ed. (Rare, but it happens.)The correct way to check for a kernel thread is to check if its parent PID is 2. Although this kind of is a kernel implementation detail, currently:
kthreadd
is guaranteed to be PID 2 because it is started right afterinit
is created (but beforeinit
is scheduled)kthreadd
, so all kernel threads have PPID ofkthreadd
, which is 2.Alternatively, you could check for zero
vsize
but not a zombie. (This is of course impossible for a userspace process.) (Credits to this SO answer for this.)I'll make a PR if I have time.
The text was updated successfully, but these errors were encountered: