Problem
According to the manual page for ps(C), the command "ps -p <PID>", where <PID> is the process ID of an active process, lists the status of only this process ID. If the process in fact does not exist, a blank line of output is displayed.
However, on a multiprocessor system with SCO Symmetrical Multiprocessing (SMP) installed, from time to time "ps -p <PID>" returns blank lines of output even when <PID> is known to be the process ID of an active process.
The -p option to ps(C) seems to be a very useful way to monitor the existence of processes; if I use it as part of a "keepalive" method to ensure that important daemons are always running, the above problem can be a serious one on my system.
Cause
Due to a problem in the OpenServer SMP environment, it is possible for the search of a valid PID to fail, if another search (on another CPU) is simultaneously being carried out.
Solution
This problem is resolved in SCO OpenServer Release 5.0.6.
For SCO OpenServer Release 5.0.5 and earlier, you can employ the alternative method below, which does not rely on the ps(C) utility. Furthermore, this method is more thorough in reporting the active/nonactive status of processes, as it distinguishes between zombie (defunct) processes and dead processes that are not zombies, and, importantly, between active processes that we have permission to manipulate and those that we do not.
NOTE: Such a method is not a substitute for the ps(C) utility in other respects. For example, this method is useful if you know what PIDs you want to monitor in terms of being alive or dead; ps(C) will still be useful if you determined that the process was still alive and you wanted to know other process attributes, such as the run state, PPID, wait channel, controlling tty, etc.
To use this utility, simply cut and paste the source code below
into a file "alive.c". Instructions on how to build and use it
are contained in the comments. Of course, the SCO OpenServer
Development System is required in order to build this utility.Notice that the use of kill(S) and sysi86(S) accomplish different objectives: kill(S) is used to verify whether a process is alive, and whether it is owned (and can therefore be manipulated) by the caller of the utility; sysi86(S) is used to tell whether a dead process is a zombie or not. See ta100489, "What is a defunct process and how do I remove it?" for a description of what zombies are.
/*
* alive.c
*
* A program which demonstrates the use of kill() and sysi86(RDUBLK) to
* obtain the status of a process. Works on SCO OpenServer Release
* 5.0.x and older unsupported releases (SCO Unix 3.2.0 and later)
*
* To compile: make alive
*
* To run: alive PID [...]
*
* Output: for each Process ID, reports whether such a process
* exists; whether it can be manipulated by the current
* user ID; and whether it is defunct (process has already
* called exit(S), but its parent has not called wait(S)).
*/#include <sys/sysi86.h>
#include <errno.h>main(int argc, char *argv[]) { int arg; int pid; int kill_err; int rdublk_err; int exists, ours, zombie; char buf[1024];
if (argc > 1)
printf(" PID Exists Ours Zombie\n");for (arg = 1; arg < argc; arg++) {
pid = atoi(argv[arg]);/* * Probe the process with kill(pid, 0). This prepares to send a * signal, but doesn't actually send one, so the target process is * unaffected. * * Expected returns are no error, indicating that the process exists * and we have permission to manipulate it; -1 with errno EPERM, * indicating that it exists but we do not have permission; or -1 * with errno ESRCH, indicating no such process. */ if (kill(pid, 0) < 0) kill_err = errno; else kill_err = 0;
/* * Evaluate the results of kill(pid, 0). */ switch(kill_err) { default: /* unexpected return from kill() */ printf("kill() returned unexpected errno %d\n", kill_err); exists = 0; ours = 0; break;
case 0: /* success -- alive and ours */
exists = 1; ours = 1; break;case EPERM: /* alive, but not ours */
exists = 1; ours = 0; break; case ESRCH: /* no such process */
exists = 0; ours = 0; break;
}/* * Probe the process with sysi86(RDUBLK, pid). This doesn't affect * the process, except potentially to read its ublock from swap, if * it had been swapped out. This same side-effect is caused by * ps(C), since it also uses sysi86(RDUBLK). * * Expected returns are no error, indicating that the process exists * and we have permission to manipulate it; -1 with errno ESRCH, * indicating no such process (which we should already have learned * above with kill(pid, 0), but which we will still record, in case * the process died between the two probes); or -1 with errno * EINVAL, indicating a defunct (zombie) process. */ if (sysi86(RDUBLK, pid, buf, 1024) < 0) rdublk_err = errno; else rdublk_err = 0;
switch(rdublk_err) {
default: /* unexpected return from RDUBLK */
printf("sysi86(RDUBLK) returned unexpected errno %d", rdublk_err);
zombie = 0; break;case 0: /* success -- not a zombie */
zombie = 0; break; case ESRCH: /* no such process; might have died since kill()
*/
zombie = 0; exists = 0; ours = 0; break;case EPERM: /* shouldn't happen, RDUBLK always allowed */
zombie = 0; break; case EINVAL: /* EINVAL indicates zombie */
zombie = 1; break;
} printf("%5d %c %c %c\n", pid,
exists ? 'Y' : 'N', ours ? 'Y' : 'N', zombie ? 'Y' : 'N');
}
}