I would do it the same way as pstree does it. See http://www.thp.uni-duisburg.de/pstree/