waitpid

生成された子プロセスの終了を待ちます.
エラーの際は(-1)を返します.
WIFEXITED, WEXITSTATUSなどのマクロを使うと状態が分かります.

#include<sys/types.h>
#include<wait.h>

pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);


waitpid example

getpidのサンプルプログラムにwaitpidを入れてみました.

#include<stdio.h>
#include<stdlib.h>
#include<wait.h>
#include<unistd.h>
#include<sys/types.h>

int main(void)
{
    int i;
    int status;
    pid_t pid;

    if((pid = fork()) == EOF) {
        perror("fork");
        exit(1);
    }

    else if(pid == 0)
        printf("Child Process running(PID:%d)\n", getpid());

    else {
        waitpid(pid, &status, 0);
        printf("Mother Process running(PID:%d)\n", getpid());
    }

    for(i = 0; i < 10; i++) {
        printf("%02d: process running(PID:%d)\n", i, getpid());
        sleep(1);
    }

    return 0;
}

                            
実行例

$ ./waitpid
Child Process running(PID:5836)
00: process running(PID:5836)
01: process running(PID:5836)
02: process running(PID:5836)
03: process running(PID:5836)
04: process running(PID:5836)
05: process running(PID:5836)
06: process running(PID:5836)
07: process running(PID:5836)
08: process running(PID:5836)
09: process running(PID:5836)
Mother Process running(PID:5835)
00: process running(PID:5835)
01: process running(PID:5835)
02: process running(PID:5835)
03: process running(PID:5835)
04: process running(PID:5835)
05: process running(PID:5835)
06: process running(PID:5835)
07: process running(PID:5835)
08: process running(PID:5835)
09: process running(PID:5835)

waitpidがあるから子プロセスを10回カウントさせ終了したら親プロセスが実行されています.