c多进程

时间:2020-02-09 13:44:14   收藏:0   阅读:63

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

  

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!