how to get current status of a process given its pid?

how to get current status of a process given its pid?

Post by Lin Zh » Sat, 28 Sep 2002 02:26:37



Hi, I am trying to get the status of a process, whether it's still running
or terminated, given its pid using waitpid(). My code looks something like:

int status;
pid_t pid = retrieveMyPid();            //gets the pid from a list

waitpid (pid, &status, WNOHANG);

if (!WIFEXITED(status))
        printf("Process Done\n");
else
        printf("Process Running\n");

But somehow status doesn't get any value and remains as zero no matter if I
have the process still running or not. Any suggestions?

Thanks.

 
 
 

how to get current status of a process given its pid?

Post by Cedric Ro » Sat, 28 Sep 2002 02:36:16



>Hi, I am trying to get the status of a process, whether it's still running
>or terminated, given its pid using waitpid(). My code looks something like:

>int status;
>pid_t pid = retrieveMyPid();            //gets the pid from a list

>waitpid (pid, &status, WNOHANG);

>if (!WIFEXITED(status))
>        printf("Process Done\n");
>else
>        printf("Process Running\n");

>But somehow status doesn't get any value and remains as zero no matter if I
>have the process still running or not. Any suggestions?

>Thanks.

waitpid is for use with the child processes, ie the one you created
with fork().
Maybe it's not the case here ?

If your system has a /proc filesystem, you can try to access /proc/<pid>.
If there is no such directory, surely the process doesn't run anymore.
If there is, it might be the process you want, or it can be another one,
since pids are reallocated later (typically pids range from 0
to 32767, I am not really sure of the upper bound though...).

So, it's a bit tricky to know if the process you watch is still
there or not. If it's a child, waitpid is enough, though.

Cdric.

 
 
 

how to get current status of a process given its pid?

Post by Dragan Cvetkovi » Sat, 28 Sep 2002 03:07:49




> >Hi, I am trying to get the status of a process, whether it's still running
> >or terminated, given its pid using waitpid(). My code looks something like:

[snip]

Quote:

> waitpid is for use with the child processes, ie the one you created
> with fork().
> Maybe it's not the case here ?

> If your system has a /proc filesystem, you can try to access /proc/<pid>.
> If there is no such directory, surely the process doesn't run anymore.
> If there is, it might be the process you want, or it can be another one,
> since pids are reallocated later (typically pids range from 0
> to 32767, I am not really sure of the upper bound though...).

If this is one of your programs or if you are running as a root, you can do
kill (pid, 0) and look for ESRCH in errno in case it doesn't run any more.

HTH, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer

 
 
 

how to get current status of a process given its pid?

Post by David Schwart » Sat, 28 Sep 2002 03:07:41



> Hi, I am trying to get the status of a process, whether it's still running
> or terminated, given its pid using waitpid(). My code looks something like:

> int status;
> pid_t pid = retrieveMyPid();            //gets the pid from a list

> waitpid (pid, &status, WNOHANG);

> if (!WIFEXITED(status))
>         printf("Process Done\n");
> else
>         printf("Process Running\n");

> But somehow status doesn't get any value and remains as zero no matter if I
> have the process still running or not. Any suggestions?

> Thanks.

        My man page says:

       WIFEXITED(status)
              is non-zero if the child exited normally.

        DS

 
 
 

how to get current status of a process given its pid?

Post by Lin Zh » Sat, 28 Sep 2002 03:13:01




>>Hi, I am trying to get the status of a process, whether it's still running
>>or terminated, given its pid using waitpid(). My code looks something
>>like:

>>int status;
>>pid_t pid = retrieveMyPid();            //gets the pid from a list

>>waitpid (pid, &status, WNOHANG);

>>if (!WIFEXITED(status))
>>        printf("Process Done\n");
>>else
>>        printf("Process Running\n");

>>But somehow status doesn't get any value and remains as zero no matter if
>>I have the process still running or not. Any suggestions?

>>Thanks.

> waitpid is for use with the child processes, ie the one you created
> with fork().
> Maybe it's not the case here ?

> If your system has a /proc filesystem, you can try to access /proc/<pid>.
> If there is no such directory, surely the process doesn't run anymore.
> If there is, it might be the process you want, or it can be another one,
> since pids are reallocated later (typically pids range from 0
> to 32767, I am not really sure of the upper bound though...).

> So, it's a bit tricky to know if the process you watch is still
> there or not. If it's a child, waitpid is enough, though.

> Cdric.

Yes, the process is a child process created by fork(). By calling waitpid()
with a process (something like 5746 from running DDD), it should return the
status of that process. But strangely the value of status never changes
from what's given at initial declaration. Did I do something wrong? Is
there someother function I can call to retrieve the status of a process?

thanks.

 
 
 

how to get current status of a process given its pid?

Post by Jens.Toerr.. » Sat, 28 Sep 2002 03:34:50



> Hi, I am trying to get the status of a process, whether it's still running
> or terminated, given its pid using waitpid(). My code looks something like:
> int status;
> pid_t pid = retrieveMyPid();            //gets the pid from a list
> waitpid (pid, &status, WNOHANG);
> if (!WIFEXITED(status))
>         printf("Process Done\n");
> else
>         printf("Process Running\n");
> But somehow status doesn't get any value and remains as zero no matter if I
> have the process still running or not. Any suggestions?

If the process you're looking for has been created from the process
doing the test using fork(), waitpid() will return 0 when the child
is not available either because it's still running (or you get -1
and errno is set to ECHILD if the process does not exist). But if
the child is still alive (and waitpid() returns 0) the value of
status does not tell you anything.

If the process you're looking for isn't a child process waitpid()
won't help you at all. But you could send a signal of 0 to the
process (signal 0 isn't a real signal but is a "just testing"
signal that lets you test if you could send a signal to the other
process). If kill(pid,0) returns -1 you can't send a signal due to
one of two reasons: either there exists no process with the pid you
used (in which case errno is set to ESRCH) or you don't have the
permissions to send a signal to the process (in which case errno is
set to EPERM). Thus the process you're looking for is still alive
(but also could already be a zombie!) when kill(pid,0) returns 0,
when it's completely dead kill() returns -1 and errno is set to
ESRCH (unfortunately, you can't distinguish this from the case
when you got the PID wrong...).
                                     Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |
 | |_| |  | |    | |          http://www.physik.fu-berlin.de/~toerring
  \___/ens|_|homs|_|oerring

 
 
 

1. Getting a pid and process status

Hello everybody,

probably answered this a million times, but I can't find those answers, so
here I go:

Basically I want to get, from within my program, the status (running,
stopped, zombie) from a completely unrelated (i.e. no child or parent)
program.

My line of reasoning is: fetch the relevant pid and the use another
function to get the status from that pid. However I cannot find a way to
get the pid of another program other than the one I'm making.

As a newbie I'm completely lost here, who can help me out?

TIA!


                       Read the Source     www.student.kun.nl/paul.lemmens

"That young girl is one of the least benightedly unintelligent organic
life forms it has been my profound lack of pleasure not to be able to
avoid meeting." (The Ultimate Hitchhikers Guide To The Galaxy, Marvin the
Paranoid Android)

2. Pkill under Trusted Solaris 8

3. Getting PID of process and check exit status

4. Linux Based Medical Records

5. getting pid of current shell, and waiting to die cleanly

6. Suspend disabled?

7. How to get a list of all child pid's of the current process?

8. INN Quewstions

9. PROCESS NAME FROM A GIVEN PID

10. Print tracer PID in /proc/<pid>/status

11. PROCESS NAME FROM A GIVEN PID

12. How to find children PID's given parent PID?

13. current directory of a given process