getppid() - 親プロセス ID の取得

標準

標準/拡張機能 C/C++ 依存項目

POSIX.1
XPG4
XPG4.2
Single UNIX Specification、バージョン 3

両方  

形式

#define _POSIX_SOURCE
#include <unistd.h>

pid_t getppid(void);

機能説明

親プロセス ID (PPID) を取得します。

戻り値

getppid() は、親プロセス ID を戻します。常に正常終了します。

文書化される errno 値はありません。

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");
}
出力:
parent (pid 6094854) is about to fork child
is sending SIGUSR2 to pid 6094854
caught SIGUSR2
parent is exiting

関連情報