james
james

Reputation: 1157

how to retrieve process status whose procees id is given from c program?

I have to retrieve process status(whether process is running or stopped) whose procees id is given from my c program(i am using linux). i planned to use exec command and written below statement

execv("ps -el|grep |awk '{print $2}'",NULL);

But it is not giving me desired output.

Please let me know where i am wrong.

Upvotes: 0

Views: 829

Answers (3)

eneville
eneville

Reputation: 61

I doubt exec is the family of calls you require here. system(3) might be more ideal.

Upvotes: 1

lavonardo
lavonardo

Reputation: 111

The exec call returns the error code corresponding to whether the execution of the program was successful or not.

If you fork a child process and then exec the command in the child process, you can read the its exit status in the parent process using the waitpid call.

Upvotes: 1

caf
caf

Reputation: 239331

The third field in /proc/<pid>/stat contains the process status: R if it's Running, S if it's Sleeping (there's a few others too, like D for Disk Wait and Z for Zombie).

Upvotes: 3

Related Questions