-1

I need to recover all running processes using c ++ and under linux. But I don't find lib or corresponding functions.

I can't run a linux command such as "ps".

Ugo Lfe
  • 717
  • 2
  • 9
  • 27
  • 2
    By "recover" you mean "list" or "enumerate", I presume? – mustaccio Jan 19 '18 at 16:54
  • Yes, in order to get all informations about currents PIDS. – Ugo Lfe Jan 19 '18 at 16:57
  • 3
    If you just want to enumerate processes, the `/proc` filesystem is your friend. But, it's really unclear *exactly* what you are asking. – Jesper Juhl Jan 19 '18 at 16:57
  • 1
    sounds like you have messed up your PATH variable. do this ; `saveBadPATH="$PATH" ; export PATH="/usr/bin;bin". Now ps` should work. You may need to add other elements to PATH, so `export PATH="/other/missing/path:$PATH"` etc. YOu `echo "$saveBadPATH" and if you see other elements to add, be sure that any element include a space char is added with syntax above, using dbl-quotes. Good luck. – shellter Jan 19 '18 at 16:58
  • 4
    @shellter what on earth are you talking about? – Jesper Juhl Jan 19 '18 at 16:59
  • @JesperJuhl : This is my interpretation of what O.P. means by 'I can't run a linux command such as "ps"', coupled with the word "Recover" in the title. As it is a poorly written Q, I think more than one set of eyes on the problem is the reasonable approach to helping get a solution. Good luck to all! – shellter Jan 19 '18 at 17:01
  • 1
    @shellter if the path is messed up that badly; then it's likely a good time to log out and start a new session ;) – UKMonkey Jan 19 '18 at 17:03
  • @shellter I like how following your advice would completely break `PATH`. Also, how is this C++ related? – melpomene Jan 19 '18 at 17:03
  • @shellter *I* read it as "OP wants to get a list of all currently running processes (and details about them) from code and running `system("ps")` is not an option". But yes, the question is *really* unclear. – Jesper Juhl Jan 19 '18 at 17:03
  • @melpomene : Yes I re-read my comment, and it should be `export PATH="/usr/bin:/bin"`. Sorry for that. Until we understand what specfically the OP is trying to accomplish, I'll leave my comment in place. If @JeperJuhl interpretation is correct, then I'll be happy to remove my comments. Good luck to all! – shellter Jan 19 '18 at 17:07

1 Answers1

3

(I'm guessing that you need to inspect, scan, list, or discover processes; recovering processes is a difficult thing, read about application checkpointing)

You should consider using proc(5), that is the /proc/ filesystem (Linux specific).

The process of pid 1234 is described by the /proc/1234/ directory. Try ls /proc/$$/ in a terminal.

So you can explore processes by using appropriate calls (e.g. opendir(3), readdir(3), closedir(3), stat(2) etc....) on the /proc/ file tree (and this is what ps, top etc.. are using; check with strace(1) ...).

Beware that many files under /proc/ have a 0 size as given by stat(2) but are sequentially readable (a bit like a pipe). Try for example stat /proc/$$/status then cat /proc/$$/status. See also this.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547