C语言中不执行指令的并行编程

C语言中不执行指令的并行编程,c,parallel-processing,fork,operator-precedence,C,Parallel Processing,Fork,Operator Precedence,我需要C并行编程方面的帮助,从以下图表: 我写了这段代码: #include <stdio.h> #include <stdlib.h> int main () { int r2; printf("--------- Program start ---------"); printf("\nBlock A instructions"); //block A printf("\nBlock B instructions"); //b

我需要C并行编程方面的帮助,从以下图表:

我写了这段代码:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    int r2;
    printf("---------   Program start   ---------");
    printf("\nBlock A instructions"); //block A
    printf("\nBlock B instructions"); //block B
    r2= fork();
    if (r2==0){ //child
        printf("\nBlock E instructions"); //block E
        printf("\nBlock F instructions"); //block F
        exit(0);
    }
    else{
        if (r2>0){ //father
            printf("\nBlock C instructions"); //block C
            printf("\nBlock D instructions"); //block D
            exit(0);
        }
        else printf("\nError");
    }
    printf("\nBlock G instructions"); //block G
    printf("\nBlock H instructions"); //block H
    printf("\n---------   Program finish   ---------");
}
为什么程序不写其他指令,为什么写“块B指令”两次

---------------------编辑:---------------------

新守则是:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main ()
{
    printf("---------   Program start   ---------\n");
    printf("Block A instructions\n"); //block A
    printf("Block B instructions\n"); //block B
    fflush(stdout);
    pid_t r2= fork();
    if (r2==0){ //child
        printf("Block E instructions\n"); //block E
        printf("Block F instructions\n"); //block F
        fflush(stdout);
        exit(1);
    }
    else{
        if (r2>0){ //father
            printf("Block C instructions\n"); //block C
            printf("Block D instructions\n"); //block D
            fflush(stdout);
            wait(NULL); //wait for child process to join with this parent, returns no value
        }
        else printf("Error");
    }
    printf("Block G instructions\n"); //block G
    printf("Block H instructions\n"); //block H
    printf("---------   Program finish   ---------");
    return 0;
}
有时C和D指令写在E和F指令之前,这是正常的还是应该始终是EF-->CD? 顺便问一下,代码是好的还是有一些错误?我用-Wall编译了它,没有收到任何错误消息,请参阅和。你应该在适当的地方打电话,尤其是之前

顺便说一句,您的标准libc的代码可能是。当然,它使用。所以你应该研究它的源代码

同时阅读 它容易出错。
\n
可以刷新缓冲区,实际上应该用作控件格式字符串的最后一个字符。

是,请参阅和。你应该在适当的地方打电话,尤其是之前

顺便说一句,您的标准libc的代码可能是。当然,它使用。所以你应该研究它的源代码

同时阅读
它容易出错。
\n
可以刷新缓冲区,实际上应该用作控制格式字符串的最后一个字符。

由于退出(0)
,它不会打印其他指令,因此它永远不会到达G和H块。对于两次打印的B,请参见Basile Starynkevitch的答案。

由于退出(0)
,它不会打印其他指令,因此它永远不会到达G和H块。对于打印两次的B,请参见Basile Starynkevitch的答案。

使用
gcc-g-Wall
编译代码,不要问两个问题。创建一个新问题,并在询问新问题之前使用调试器(
gdb
)使用
{printf(“Error\n”);fflush(NULL);}
使用
gcc-g-Wall
编译代码不要问两个问题。创建一个新问题,并在询问新问题之前使用调试器(
gdb
),使用
{printf(“Error\n”);fflush(NULL);}
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int main ()
{
    printf("---------   Program start   ---------\n");
    printf("Block A instructions\n"); //block A
    printf("Block B instructions\n"); //block B
    fflush(stdout);
    pid_t r2= fork();
    if (r2==0){ //child
        printf("Block E instructions\n"); //block E
        printf("Block F instructions\n"); //block F
        fflush(stdout);
        exit(1);
    }
    else{
        if (r2>0){ //father
            printf("Block C instructions\n"); //block C
            printf("Block D instructions\n"); //block D
            fflush(stdout);
            wait(NULL); //wait for child process to join with this parent, returns no value
        }
        else printf("Error");
    }
    printf("Block G instructions\n"); //block G
    printf("Block H instructions\n"); //block H
    printf("---------   Program finish   ---------");
    return 0;
}
---------   Program start   ---------
Block A instructions
Block B instructions
Block E instructions
Block F instructions
Block C instructions
Block D instructions
Block G instructions
Block H instructions
---------   Program finish   ---------
printf("\nBlock C instructions");