Search code examples
clinuxlinux-kernelps

Outputting both stopped and Running processes in linux?


I'm presently using a Linux command in my c program to show a group of processes. When a process is stopped (suspended) though, this command does not list it. I need to list both running AND stopped jobs. Note: When I say stopped, I do NOT mean terminated jobs. The issue is with displaying suspended processes.

execvp("/bin/ps", parmList);

I have to use the ps command. Is there anyway to show both running and stopped (suspended) processes in this situation?


Solution

  • There are two ways to go about it:

    1) Continuously monitor processes for change in state.

    2) Register a handler to get notified asynchronously, when state changes.

    1st case

    execute a script that continously monitors process states. You have to use ps with BSD style options ie

    ps axo pid,stat
    

    2nd case

    you can only monitor the child processes. you can monitor them by using waitpid() provided they are child processes.

    So, basically you register a handler for signals and in the handler you use waitpid to get the status.

    You will find the signals explained here: [http://linux.die.net/man/2/waitpid][1]