Identifying kernel threads

后端 未结 4 1022
心在旅途
心在旅途 2021-01-04 03:50

I\'d like to know how I can distinguish a kernel-thread from a user-thread for a process-scanner I\'m building. I\'m having a hard time finding a good definition of both typ

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 04:15

    As you pointed out in your own comment above, all user processes are descendants of the init process (pid=1). Kernel threads are not descendants of the init process, since init is a user process, and user processes cannot create kernel threads. Therefore, to check if process p is a user process and not a kernel thread, one needs to operate on the process graph and evaluate if init dom p where dom is the Dominator operator. Concretely in Python:

    def is_user_process(p):
      if (p=='1'):
        print 'User process'
      else:
        pstat = open('/proc/%s/stat'%p).read().split()
        parent = pstat[3]
        if (parent=='1'):
          print 'User process'
        elif (parent=='0'):
          print 'Kernel thread'
        else:
          is_user_process(parent)
    

提交回复
热议问题