管道用于多个过程 [英] Pipe for multiple processes

查看:80
本文介绍了管道用于多个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在做一些家庭作业,并且很难过.目标是生成100,000个数字,并将工作分为10个进程(每个10,000个数字)将它们加在一起

Currently working on some homework and having a hard time. The goal is to generate 100,000 numbers and add them all together by dividing the work into 10 processes (10,000 numbers each)

我想我已经弄清楚了如何(希望)派生进程,但是使用Pipe()中继每个子进程的小计不起作用...下面的程序为每个子进程返回44901,为每个子进程返回449010总计.

I think I've figured out how to fork processes (hopefully), but using Pipe() to relay the subtotals from each child process is not working... the program below returns 44901 for each child process and 449010 for the running total.

我正在努力奋斗,但我觉得这很简单,我应该能够理解.

I'm struggling hard but I feel like this is something simple I should be able to understand.

main()
{
    int i;
    pid_t pid;
    int status = 0;
    int fd[2];
    int runningTotal = 0;
    pipe(fd);

    int t;
    int r;

    for (i = 0; i < 10; i++) {
        pid = fork();
        if (pid == 0){
            close(fd[0]);
            t = ChildProcess();
            write(fd[1], &t, sizeof(t));
            exit(0);
        }
        close(fd[1]);
        read(fd[0], &r, sizeof(r));
        runningTotal = runningTotal + r;
        wait(&status);
    }

    printf("%i\n", runningTotal);
}

int ChildProcess() {
    int i;
    int total = 0;
    int r = 0;

    for (i = 0; i < 10000; i++) {
        r = rand() % 10; // 0 to 10
        total = total + r;
    }

    printf("%i\n", total);
    return total;
}

推荐答案

通常,每个孩子都会使用一个单独的管道,否则父母就无法知道它读取的数据来自哪个进程.在这种情况下,我认为这并不是什么大问题,因为在这里,您实际上不在乎.尽管这仍然让我有点畏缩,但我认为您确实可以只用一根烟斗就可以完成此特定任务.

Ordinarily, one would use a separate pipe for each child, for otherwise it's impossible for the parent to know from which process the data it reads comes. I don't think that's so much of an issue in this particular case, though, because here, you actually don't care. Although it still makes me cringe a bit, I think you indeed can get away with just one pipe for this particular task.

事实上,我根本不认为您的问题与管道有关.它与rand()一起使用.所有子进程都计算完全相同的(伪)随机数序列,因为它们都使用相同的(默认)种子.如果要产生不同的数字序列,则需要在每个子进程中调用srand(),并在每个子进程中提供不同的种子. rand()将生成的数字顺序完全由其开始的种子决定.

In fact, I don't think your problem is with the pipe at all. It is with rand(). All child processes compute exactly the same sequence of (pseudo-)random numbers because they all use the same (default) seed. If you want to produce different sequences of numbers, then you need to call srand() in each child process, giving a different seed in each one. The sequence of numbers rand() will generate is completely determined by the seed with which it starts.

也请注意,如果系统的随机数生成器完全没有问题,那么由各个过程计算出的所有总和应该彼此非常接近,并且与您报告的结果也非常接近.这是统计中的中心极限定理的结果,但您可以简单地将其视为较大的结果平均平衡较小的结果.计算余数mod 10可能会引起一些偏差.

Note, too, that if the system's random number generator is any good at all, then all the sums computed by the various processes should be very close to each other, and to the result you reported. This is a consequence of the Central Limit Theorem in statistics, but you can think of it simply as the larger results balancing the smaller ones on average. There's probably a slight bias arising from calculating the remainder mod 10.

这篇关于管道用于多个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆