简体   繁体   English

在C中使用fork()时如何管理父子进程的输出顺序

[英]How to manage output order of parent and child processes when using fork() in C

Task is: Write a program that starts 2 new processes.任务是:编写一个程序,启动 2 个新进程。 Parent and child processes print exactly 42 numbers.父进程和子进程正好打印 42 个数字。 Parent numbers are divisible by 2, child numbers by 4 and 8. First number is always 0. Output order is: 1) child one, 2) parent, 3) child two父编号可被 2 整除,子编号可被 4 和 8 整除。第一个数字始终为 0。输出顺序为:1) 子一,2) 父,3) 子二

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

void main() {

    pid_t c1, c2;
    int j = 1;

    c1 = fork();

    if (c1 == 0) {
                for (int i = 0; j <= 42; i += 4) {
                    printf("c1: %d\n", i);
                    j++;
                }
    } 

    else {
        c2 = fork();
        if (c2 == 0) {
                sleep(2);
                for (int i = 0; j <= 42; i += 8) {
                    printf("c2: %d\n", i);
                    j++;
                }
        } 
        else {
                sleep(1);
                for (int i = 0; j <= 42; i += 2) {
                    printf("p: %d\n", i);
                    j++;
                    wait(0);
                }
        }
    }
}

Output is:输出是:

c1: 0
c1: 4
c1: 8
...
c1: 164
p: 0
p: 2
c2: 0
c2: 8
c2: 16
...
c2: 328
p: 4
p: 6
p: 8
...
p: 82

Why does the parent process stop at "p: 2", then c2 prints 42 numbers and then again the parent prints 40 numbers?为什么父进程在“p:2”处停止,然后 c2 打印 42 个数字,然后父进程再次打印 40 个数字? It works without wait(0), but not how it's supposed to.它无需等待(0)即可工作,但不是它应该如何工作。

Task is: Write a program that starts 2 new processes.任务是:编写一个程序,启动 2 个新进程。 Parent and child processes print exactly 42 numbers.父进程和子进程正好打印 42 个数字。 Parent numbers are divisible by 2, child numbers by 4 and 8. First number is always 0. Output order is: 1) child one, 2) parent, 3) child two父编号可被 2 整除,子编号可被 4 和 8 整除。第一个数字始终为 0。输出顺序为:1) 子一,2) 父,3) 子二

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

void main() {

    pid_t c1, c2;
    int j = 1;

    c1 = fork();

    if (c1 == 0) {
                for (int i = 0; j <= 42; i += 4) {
                    printf("c1: %d\n", i);
                    j++;
                }
    } 

    else {
        c2 = fork();
        if (c2 == 0) {
                sleep(2);
                for (int i = 0; j <= 42; i += 8) {
                    printf("c2: %d\n", i);
                    j++;
                }
        } 
        else {
                sleep(1);
                for (int i = 0; j <= 42; i += 2) {
                    printf("p: %d\n", i);
                    j++;
                    wait(0);
                }
        }
    }
}

Output is:输出是:

c1: 0
c1: 4
c1: 8
...
c1: 164
p: 0
p: 2
c2: 0
c2: 8
c2: 16
...
c2: 328
p: 4
p: 6
p: 8
...
p: 82

Why does the parent process stop at "p: 2", then c2 prints 42 numbers and then again the parent prints 40 numbers?为什么父进程在“p:2”处停止,然后 c2 打印 42 个数字,然后父进程再次打印 40 个数字? It works without wait(0), but not how it's supposed to.它无需等待(0)即可工作,但不是它应该如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM