C了解从叉称为多标准输出的exec [英] C Read stdout from multiple exec called from fork

查看:128
本文介绍了C了解从叉称为多标准输出的exec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个进程低于code创建一个孩子(叉()),然后孩子通过调用替换其本身的执行exec()。在标准输出的EXEC的是写在管道而不是外壳。然后父进程从什么EXEC已与而(读(pipefd [0],缓冲区,缓冲区尺寸)!= 0)

写的管道中读取

谁能告诉我该怎么做同样的事情,但上面有孩子的过程(谁与EXEC如上自行更换)N多的描述。

  INT pipefd [2];
管(pipefd);如果(叉()== 0)
{
    关闭(pipefd [0]); //关闭读终在孩子    dup2(pipefd [1],1); //标准输出发送到管道
    dup2(pipefd [1],2); //标准错误发送到管道    关闭(pipefd [1]); //这个描述符不再需要    EXEC(...);
}
其他
{
    //父    字符缓冲区[1024];    关闭(pipefd [1]); //关闭管道的写入结束在父    而(读(pipefd [0],缓冲液,的sizeof(缓冲液))!= 0)
    {
    }
}


解决方案

我找到了答案。我做了管阵列,这样的过程不会覆盖另一个进程的输出。

下面是我的code。你是否发现任何错误?

 的#include<&signal.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&unistd.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / time.h中>#定义N 10INT主(INT ARGC,CHAR *的argv []){
    ssiz​​e_t供readlen;
    INT pipefd [N] [2];
    INT I;
    对于(i = 0; I< N;我++){
        管(pipefd [I]);
    }    INT PID = GETPID();    对于(i = 0; I< N;我++){
        如果(叉()== 0)//父进程将继续循环
        {            关闭(pipefd [I] [0]); //关闭读终在孩子            dup2(pipefd [I] [1],1); //标准输出发送到管道
            dup2(pipefd [I] [1],2); //标准错误发送到管道            关闭(pipefd [I] [1]); //这个描述符不再需要            焦B〔50];
            sprintf的(B,%D,我);            EXECL(/斌/回声,回声,B,NULL);
        }
    }    如果(PID == GETPID()){        //父        字符缓冲区[1024];        对于(i = 0; I< N;我++){
            关闭(pipefd [I] [1]); //关闭管道的写入结束在父            而((readlen =读(pipefd [I] [0],缓冲液,的sizeof(缓冲液)))!= 0)
            {
                        缓冲[readlen] ='\\ 0';
            }            的printf(%S \\ n,缓冲区);        }
    }
}

In the code below a process creates one child (fork()) and then the child replaces itself by calling exec(). The stdout of the exec is written in a pipe instead of the shell. Then the parent process reads from the pipe what the exec has written with while (read(pipefd[0], buffer, sizeof(buffer)) != 0)

Can someone tell me how to do the exact same thing as described above but with N number of children processes (who replace themselves with exec as above).

int pipefd[2];
pipe(pipefd);

if (fork() == 0)
{
    close(pipefd[0]);    // close reading end in the child

    dup2(pipefd[1], 1);  // send stdout to the pipe
    dup2(pipefd[1], 2);  // send stderr to the pipe

    close(pipefd[1]);    // this descriptor is no longer needed

    exec(...);
}
else
{
    // parent

    char buffer[1024];

    close(pipefd[1]);  // close the write end of the pipe in the parent

    while (read(pipefd[0], buffer, sizeof(buffer)) != 0)
    {
    }
}

解决方案

I found the answer. I made an array of pipes so that a process does not overwrite the output of another process.

Here is my code. Do you find any mistake?

#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>

#define N 10

int main(int argc, char *argv[]) {
    ssize_t readlen;
    int pipefd[N][2];
    int i;
    for (i = 0; i < N; i++) {
        pipe(pipefd[i]);
    }

    int pid = getpid();

    for (i = 0; i < N; i++) {
        if (fork() == 0) //The parent process will keep looping
        {

            close(pipefd[i][0]);    // close reading end in the child

            dup2(pipefd[i][1], 1);  // send stdout to the pipe
            dup2(pipefd[i][1], 2);  // send stderr to the pipe

            close(pipefd[i][1]);    // this descriptor is no longer needed

            char b[50];
            sprintf( b, "%d", i);

            execl("/bin/echo", "echo", b,NULL);


        }
    }

    if (pid == getpid()) {

        // parent

        char buffer[1024];

        for (i = 0; i < N; i++) {
            close(pipefd[i][1]);  // close the write end of the pipe in the parent

            while ((readlen=read(pipefd[i][0], buffer, sizeof(buffer))) != 0)
            {
                        buffer[readlen] = '\0';
            }

            printf("%s\n",buffer);

        }
    }


}

这篇关于C了解从叉称为多标准输出的exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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