以下是 C 语言代码实现:

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

void child1() {
    printf('E\n');
}

void child2() {
    printf('F\n');
}

int main() {
    pid_t pid1, pid2;
    pid1 = fork();
    if (pid1 == 0) {  // 子进程1
        child1();
    } else {
        pid2 = fork();
        if (pid2 == 0) {  // 子进程2
            child2();
        } else {  // 父进程
            waitpid(pid1, NULL, 0);
            waitpid(pid2, NULL, 0);
        }
    }
    return 0;
} 

输出结果:

EF
使用 C 语言创建两个子进程分别输出 'E' 和 'F'

原文地址: https://www.cveoy.top/t/topic/jJbv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录