1. fork创建子进程
2. wait等待子进程结束
#include <stdio.h> #include <unistd.h> #include <sys/wait.h> int main() { int count = 0; int pid = fork(); if (pid == 0) { count++; sleep(5); printf("i am child pid: %d, count:%d ", getpid(), count); } else { count++; printf("i am parent pid: %d, count:%d ", getpid(), count); // wait等待子进程结束 int cPid = wait(NULL); printf("wait child pid:%d ", cPid); } return 0; }