-3

In BASH there is a pstree command which 'draws' a tree of processes. I am wondering what is the similar function in C programming language?

A simple example would be appreciated.

user1926550
  • 539
  • 5
  • 10
  • 18
  • 1
    If you want that functionality why not just call pstree? – VoronoiPotato May 08 '13 at 14:40
  • 2
    It's open source, I'm sure you can find it and read how it does it directly. – Carl Norum May 08 '13 at 14:41
  • 1
    @user1926550: simply asking "Can I do X in language Y" is not enough of a question for SO. – user7116 May 08 '13 at 15:02
  • This question is very unclear. Do you want to draw a tree of something? Do you want to output information about processes? Or both? For the source to pstree, try here for example: http://packages.ubuntu.com/source/raring/psmisc – Wodin May 08 '13 at 15:13

1 Answers1

2

There is no such 'function' in C. But you can easily program something that creates something alike, using execl()/system() calls to ps, or by reading the /proc file system (on linux).

From there, you can get the children list of every process, and for each process of this list get their children etc.. starting from process 1 init.

otherwise,

int main() {
    system('pstree');
    return 0;
}

would work :-)

If you want to reimplement it, you'd better follow Carl Norum's advice to Use The Source, Luke!

zmo
  • 24,463
  • 4
  • 54
  • 90
  • 1
    ... if you are on a platform on which these system calls apply. If not you will have to use the equivalent system calls for your platform. – Vicky May 08 '13 at 15:01
  • there is no system calls that handle process introspection. I'm sure there is some libraries that handle this we can find on google, but that would'nt be posix. The only standard way to do this, would be the ways I've given in my answer. (though /proc is not standard) – zmo May 08 '13 at 15:05