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?
Traverse the Process Tree
process
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:#!/bin/bash# 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=$2Original 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.