Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Null Signal and Process Existence Check in Linux
Null Signal and Process Existence Check in Linux
Linux System Programming · EmbeddedPathashala
When you call kill(pid, 0), the kernel does NOT send any signal. Instead it only performs the permission check. This is called the null signal technique.
The result tells you whether the process exists and whether you have permission to signal it — without disturbing the target process at all.
| Return value & errno | Meaning |
|---|---|
| Returns 0 | Process exists AND caller has permission to signal it |
| Returns -1, errno = EPERM | Process exists BUT caller lacks permission |
| Returns -1, errno = ESRCH | Process does NOT exist |
/* Use kill(pid, 0) null signal to check process existence
Compile: gcc -o check_pid check_pid.c
Usage: ./check_pid <PID> */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
return 1;
}
pid_t pid = (pid_t)atoi(argv[1]);
/* Send null signal — no signal is actually sent */
if (kill(pid, 0) == 0) {
printf("PID %d exists and we can signal it.\n", (int)pid);
} else {
if (errno == EPERM)
printf("PID %d exists but we lack permission.\n", (int)pid);
else if (errno == ESRCH)
printf("PID %d does NOT exist.\n", (int)pid);
else
perror("kill");
}
return 0;
}
./check_pid 1 (init — exists, EPERM), ./check_pid $$ (your shell — exists), ./check_pid 999999 (likely ESRCH)./* Wait for a process to exit by polling with null signal
Compile: gcc -o wait_exit wait_exit.c
Usage: ./wait_exit <PID> */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <pid>\n", argv[0]);
return 1;
}
pid_t pid = (pid_t)atoi(argv[1]);
int checks = 0;
printf("Monitoring PID %d...\n", (int)pid);
while (1) {
if (kill(pid, 0) == -1) {
if (errno == ESRCH) {
printf("PID %d has exited after %d checks.\n",
(int)pid, checks);
break;
} else if (errno == EPERM) {
printf("PID %d exists (no permission). Still alive.\n",
(int)pid);
} else {
perror("kill");
break;
}
} else {
printf("Check %d: PID %d still running.\n",
++checks, (int)pid);
}
sleep(1);
if (checks > 30) {
printf("Gave up after 30 seconds.\n");
break;
}
}
return 0;
}
| Technique | Works For | Notes |
|---|---|---|
| kill(pid, 0) null signal | Any process | Subject to PID recycling |
| wait() system calls | Child processes only | Most reliable for own children |
| Semaphore / file lock | Cooperating processes | Requires pre-arrangement |
| Pipe (watching write-end close) | Any related process | Elegant for monitoring |
| stat(“/proc/PID”) | Any process | Not affected by PID recycling ambiguity if done carefully |
raise() and killpg() — Sending signals to yourself and process groups
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
