简体   繁体   English

如何在C中同时运行两个子进程?

[英]How to run two child processes simultaneously in C?

So I'm getting into Concurrent Programming, but for some reason I can't even get the basics to work. 所以我进入并发编程,但由于某种原因我甚至无法使用基础知识。 I have a file called fork.c, which contains a method main. 我有一个名为fork.c的文件,其中包含一个方法main。 In this method main I fork twice, into child processes 1 and 2. 在这个方法主要我分叉两次,进入子进程1和2。

In child 1, I print the character 'A' 50 times. 在孩子1中,我打印了'A'字符50次。

In child 2, I print the character 'B' 50 times. 在孩子2中,我打印字符'B'50次。

When I run my code, I get the output AAAAA...AAAABBBBBB....BBBBBB. 当我运行我的代码时,我得到输出AAAAA ... AAAABBBBBB .... BBBBBB。 But never something like ABABABABABABAB.... In fact, sometimes I even get BBBBB....BBBBAAAA....AAAAA. 但从来没有像ABABABABABABAB那样....事实上,有时我甚至得到了BBBBB ...... BBBBAAAA ...... AAAAA。

So why am I experiencing this behavior? 那么为什么我会遇到这种行为? Perhaps I'm going about it completely wrong. 也许我完全错了。

#include <stdlib.h>
#include <stdio.h>

void my_char(char n) {
    write(1, &n, 1);
}

int main() {
    int status;
    pid_t child1, child2;

    if (!(child1 = fork())) {
        // first childi
        int a;
        for (a = 0; a < 50; a++) {
            my_char('A'); 
        }
        exit(0);
    } else if (!(child2 = fork())) {
        // second child
        int a;
        for (a = 0; a < 50; a++) {
            my_char('B');
        }
        exit(0);
    } else {
        // parent
        wait(&child1);
        wait(&child2);
        my_char('\n');
    }

    return 0;
}   

They are running concurrently, but the processes end almost immediately after being started. 它们同时运行,但进程在启动后几乎立即结束。 In other words, they're too short to actually get any real overlap. 换句话说,它们太短,实际上没有任何真正的重叠。

EDIT: 编辑:

The time needed to start another process is longer than the time it takes to run them. 启动另一个进程所需的时间比运行它们所需的时间长。 Therefore the chance of overlap is small. 因此重叠的可能性很小。 (there are also buffering issues which I'll omit) (还有缓冲问题,我将省略)

You need each process to do more work than that. 您需要每个流程完成更多工作。 Try printing more than 50. Printing more than 10000 will probably be enough. 尝试打印超过50个。打印超过10000可能就足够了。

I think this is much easier to figure how fork() works: 我认为这更容易理解fork()的工作原理:

#include <stdlib.h>
#include <stdio.h>

int main() {
    pid_t child1, child2;
    printf("Start\n");
    if (!(child1 = fork())) {
        // first childi
        printf("\tChild 1\n");
        sleep(5);
        exit(0);
    } else if (!(child2 = fork())) {
        // second child
        printf("\tChild 2\n");
        sleep(5);
        exit(0);
    } else {
        // parent
        printf("Parent\n");
        wait(&child1);
            printf("got exit status from child 1\n");
        wait(&child2);
            printf("got exit status from child 2\n");
    }

    return 0;
}

...and here is the output: ......这是输出:

    Start
        Child 1
    Parent
        Child 2
    got exit status from child 1
    got exit status from child 1

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

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