简体   繁体   English

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

[英]Parallel programming in C not executing instructions

I need help with C parallel programming, from this graph: graph image我需要 C 并行编程方面的帮助,来自这张图: graph image

I wrote this code:我写了这段代码:

#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   ---------");
}

And the output is:输出是:

---------   Program start   ---------
Block A instructions
Block B instructions
Block E instructions
Block F instructionsBlock B instructions
Block C instructions
Block D instructions

Why doesn't the program write the other instructions, and why does it write "block B instructions" twice?为什么程序不写其他指令,为什么它写了两次“块B指令”?

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

The new code is:新代码是:

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

The output now is:现在的输出是:

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

Sometimes the C and D instructions are written before the E and F instructions, is it normal or should it always be EF --> CD ?有时C和D指令写在E和F指令之前,这是正常的还是应该总是EF --> CD? Btw is the code good or are there some mistakes?顺便说一句,代码是好的还是有一些错误? I compiled it with -Wall and I don't get any error messages我用 -Wall 编译它,但没有收到任何错误消息

<stdio.h> is buffered , see stdio(3) and setvbuf(3) . <stdio.h>缓冲,请参阅stdio(3)setvbuf(3) You should call fflush(3) at appropriate places, in particular before fork(2) .您应该在适当的地方调用fflush(3) ,特别是在fork(2)之前。

BTW, the code of your standard libc is free software , probably GNU glibc .顺便说一句,您的标准 libc 的代码是自由软件,可能是GNU glibc Of course it uses syscalls(2) .当然它使用syscalls(2) So you should study its source code.所以你应该研究它的源代码。

Read alsoHow to debug small programs另请阅读如何调试小程序

printf("\\nBlock C instructions");

is error-prone.容易出错。 The \\n could flush the buffer, and should in practice be used as the last character of printf(3) control format string. \\n可以刷新缓冲区,实际上应该用作printf(3)控制格式字符串的最后一个字符。

It doesn't print the other instructions because of the exit(0) , so it will never reach G and H blocks.由于exit(0) ,它不会打印其他指令,因此它永远不会到达 G 和 H 块。 For the B being printed twice see Basile Starynkevitch's answer.对于 B 被打印两次,请参阅 Basile Starynkevitch 的回答。

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

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