getppid() — Get the parent process ID
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1
XPG4 XPG4.2 Single UNIX Specification, Version 3 |
both |
Format
#define _POSIX_SOURCE
#include <unistd.h>
pid_t getppid(void);
General description
Gets the parent process ID (PPID).
Returned value
getppid() returns the parent process ID. It is always successful.
There are no documented errno values.
Example
CELEBG15
/* CELEBG15 */
#define _POSIX_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h> /*FIX: used to be <wait.h>*/
volatile short footprint=0;
void catcher(int signum) {
switch (signum) {
case SIGALRM: puts("caught SIGALRM");
break;
case SIGUSR2: puts("caught SIGUSR2");
break;
default: printf("caught unexpected signal %d\n", signum);
}
footprint++;
}
main() {
struct sigaction sact;
int status;
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sact.sa_handler = catcher;
sigaction(SIGUSR2, &sact, NULL);
sigemptyset(&sact.sa_mask);
sact.sa_flags = 0;
sact.sa_handler = catcher;
sigaction(SIGALRM, &sact, NULL);
printf("parent (pid %d) is about to fork child\n", (int) getpid());
if (fork() == 0) {
printf("child is sending SIGUSR2 to pid %d\n", (int) getppid());
kill(getppid(), SIGUSR2);
exit(0);
}
alarm(30);
while (footprint == 0);
wait(&status);
puts("parent is exiting");
}
Output
parent (pid 6094854) is about to fork child
is sending SIGUSR2 to pid 6094854
caught SIGUSR2
parent is exiting