I am aware that processes are organized in the form of a hierarchy. So, if I want to implement an operation which is to determine whether a given process belongs to a given sub-tree in the hierarchy by trace back to parent process all the way back to the root of the sub-tree, will it be a very high cost?

  • I would recommend looking at the source code of the program ps to see how it computes a process tree. I think the way to do it is to get a list of all processes and construct a graph structure from that, but I'd love to know if there's an easier way.
    – dhag
    May 4 '15 at 21:42
  • 1
    Did you check pstree ?
    – heemayl
    May 4 '15 at 21:46
  • dhag: thx! heemay: I do you know where is the source code of pstree? I am not to visualize the tree, I just want to find the hierarchy for my program.
    – Xin Li
    May 4 '15 at 21:49

It sounds like you have two PIDs and you want to know if one is a descendent of the other. If so, you can use this:


# Checks the process tree checking to see if PID $1 is an ancestor of
# PID $2.  Returns true/false (0/1).
# (Needs error handling to determine if $1 and $2 are provided and both
# are numeric.  Left as an exercise for the reader.)

ps -ea -o pid,ppid |
   awk '{ parent[$1] = $2 }
        END {  if (parent[start] == "")
                   exit 1
               while (lookfor != parent[start] && start != 1)
                   start = parent[start]
               exit start==1 ? 1 : 0
            }' lookfor="$1" start="$2"

Original post: It depends on what your ultimate goal is. If you're trying to determine whether two processes were started from the same terminal session, for example, then you can use the -o option of ps and tell it to print the session group id (the login shell always starts a new session and all descendants get that session id).

If you have some other purpose in mind or your situation has to account for non-login processes, then parsing the output of ps is probably the best way to go. I'd give an example in awk, but I'm not sure what you're looking for so don't know what output would best suit your needs.

  • In fact, I am trying to determine the the Linux container to which the given process belongs to. And I am provided with the API to calculate the init process for any given container. That's why I want to trace back to the init process for the container.
    – Xin Li
    May 5 '15 at 0:27
  • Thank you! But if I would like to make this algorithm a part of my kernel module, which need to determine very quickly so that OS can make decision to allocating processes. In this fashion, will it be a big cost?
    – Xin Li
    May 5 '15 at 17:22
  • You continue to add criteria! Can you edit your OP and add the details there? Be as specific as possible, especially giving examples to illustrate what you're looking to accomplish. For example, when you say "kernel module" that means something special to me (code executing in the kernel) and without more context I can't determine what you really mean. I'd love to help...
    – Azhrei
    May 5 '15 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.