tags:

views:

51

answers:

3

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.

+3  A: 

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).

caf
how to use pipe and grep in execl command according to above reply below statementexecl("/bin/cat","bin/cat","/proc/1/stat") is giving whole line but i want to retrieve 3rd column only
james
Don't exec `cat` to read a file in C - use the builtin file reading functions `fopen()`, `fgets()` etc.
caf
+1  A: 

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.

lavonardo
+1  A: 

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

eneville