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\n", getpid(), count);
    } else {
        count++;
        printf("i am parent pid: %d, count:%d\n", getpid(), count);
        // wait等待子进程结束
        int cPid = wait(NULL);
        printf("wait child pid:%d\n", cPid);
    }

    return 0;
}

  

posted on 2020-02-09 11:40  luckygxf  阅读(351)  评论(0编辑  收藏  举报

导航