c多进程

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;
}

  

原文地址:https://www.cnblogs.com/luckygxf/p/12286607.html