If I don't call exec() or system(), can I find out if a process
is still alive? (I know its pid)
Thanks!
--
Yong (yo...@rockmag.gso.uri.edu)
You can use kill system call with signal number 0 (zero )
kill(pid,0)
for more info man 2 kill
C.EKINGEN
-
-
-
======
NAME
kill - send a signal to a process or a group of processes
SYNOPSIS
#include <signal.h>
int kill (pid, sig)
pid_t pid; int sig;
DESCRIPTION
kill sends a signal to a process or a group of processes.
The process or group of processes to which the signal is to
be sent is specified by pid. The signal that is to be sent
is specified by sig and is either one from the list given in
signal(2), or 0. If sig is 0 (the null signal), error
checking is performed but no signal is actually sent. This
can be used to check the validity of pid.
.....
--
Amrit Jassal
KPMG Peat Marwick,
Enterprise Package Solutions,
Palo Alto.
: If I don't call exec() or system(), can I find out if a process
: is still alive? (I know its pid)
: Thanks!
if (sigsend(P_PID,pid,0)<0)
if (errno==ESRCH)
/* proc doesn't exist */
else
/* some oether error */
else
/* proc exists */
You can substitute sigsend(P_PID,pid,0) with kill(pid,0) if you want.
--
AJ Musgrove
----------------------------------------------------------------
My opinions do not necessarily reflect those of my employer, or
anyone else for that matter. O-
----------------------------------------------------------------
/proc/<PID> should exists if the process PID is still running.
Use stat() system call to check for file existence.
man stat
man proc
Yong Yuan <yo...@rockmag.gso.uri.edu> wrote in article
<59nn8m$1...@bubbla.uri.edu>...
> Hi,
>
> If I don't call exec() or system(), can I find out if a process
> is still alive? (I know its pid)
>
> Thanks!
>
> --
> Yong (yo...@rockmag.gso.uri.edu)
>
: /proc/<PID> should exists if the process PID is still running.
: >
Not to split hairs, but I believe that access() would be clearer in this
case, as F_OK in amod is "Check existance of file". stat() could be
confusing down the road for a maintence programmer or something.
Just my 0.02$ :)
The easiest way should be to do a 'kill(pid, 0);', if the process exists,
kill returns zero, otherwise -1.
/stefan
--
'The number of Unix installations has grown to ten, with more expected.'
<Unix Programmer's Manual, 2nd ed.; june 1972>
>The easiest way should be to do a 'kill(pid, 0);', if the process exists,
>kill returns zero, otherwise -1.
On newer systems(SV4+) you can also check for the existance of
/proc/pid, it's either a file or a directory. However remember that the
process may be dying and may not exist by the time you get the
response. Kill(pid,0) however is good for most uses.
The easiest way should be to do a 'kill(pid, 0);', if the process exists,
kill returns zero, otherwise -1.
/stefan
>The easiest way should be to do a 'kill(pid, 0);', if the process exists,
>kill returns zero, otherwise -1.
On newer systems(SV4+) you can also check for the existance of
>The easiest way should be to do a 'kill(pid, 0);', if the process exists,
>kill returns zero, otherwise -1.
kill() can also return -1 if you don't have permission to kill it.
You should test:
kill(pid, 0) == -1 && errno == ESRCH
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Stefan> The easiest way should be to do a 'kill(pid, 0);', if the
Stefan> process exists, kill returns zero, otherwise -1.
Actually kill can quite legitimately return -1 even if the process exists.
To use this method generally, you also need to examine errno.
--
Andrew Gierth (and...@microlise.co.uk)
"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam
All very fine if you're on System V release 4. A somewhat more portable
way is to use kill(2) with signal 0.
--
Boyd Roberts <bo...@france3.fr> N 31 447109 5411310
``Not only is UNIX dead, but it's starting to smell really bad.'' -- rob
Send it signal 0 using kill(2).
--
James Youngman VG Gas Analysis Systems |The trouble with the rat-race
Before sending advertising material, read |is, even if you win, you're
http://www.law.cornell.edu/uscode/47/227.html|still a rat.
Unless you have a really high turnover of processes, it is unlikely that
the same process ID will be reused within the same day. Each new process
is assigned a higher process ID than the last, and the count does not
wrap until 32768. This means that you have to start and stop at least
32000 processes before an ID will be reused.
>> This only helps to determine whether a process with the specified pid is
>> still alive. Since a pid can be reused, it might run a completely different
>> program.
Fletcher> Unless you have a really high turnover of processes, it is
Fletcher> unlikely that the same process ID will be reused within the
Fletcher> same day. Each new process is assigned a higher process ID
Fletcher> than the last, and the count does not wrap until 32768.
Fletcher> This means that you have to start and stop at least 32000
Fletcher> processes before an ID will be reused.
Those limits are system-dependent.
In any case, the rate of process turnover can be very variable. Consider,
for example, the effect of running a shell script that does a mass rename
or edit in a directory with thousands of files in it. If you work at it,
you can wrap PIDs in only a few minutes even on a fairly modest machine.
Also watch out for AIX - it has an unusual scheme for allocating pids that
can (in rare cases) cause them to be reused very quickly.
I used popen and call ps -fp (not ucb as -f is illegal), and parsed the output since I had to
do it as a non-root program. You can get more efficient using system calls such as priocntl()
(SVR4) but they require root priviledge.