简体   繁体   English

如何将管道从父进程发送到子进程

[英]how to send the pipe from parent to child process

I am trying to use the fork to create three child process, two of them will write string of char to pipe will the third one will read it from pipr and output it one the screen. 我正在尝试使用fork创建三个子进程,其中两个将char字符串写入管道,第三个将从pipr读取它并将其输出到屏幕。 We are supposed to create 4 files and call exec system call to access those file. 我们应该创建4个文件,然后调用exec系统调用来访问这些文件。 but I have no idea how to send the pipe to other process. 但我不知道如何将管道发送到其他进程。 Since I tried in the main process to create fd[2] and pipe(fd) in the child process call close(fd[0]), but it gives me undeclared? 由于我在主进程中尝试过在子进程调用close(fd [0])中创建fd [2]和pipe(fd),但是它没有声明? how could I do this. 我该怎么做。 here's my code 这是我的代码

   int main(void){
int fd[2],z,status,i;
pid_t childB, childC, childD;
char *arg[1] = {0};
z = pipe(fd);
if(z <0 ){perror("create pipe"); exit(0);}

childB = fork();
if(childB==0){execv("PipeW1",arg);}
if(childB<0){printf("fork failed\n");exit(0);}
....

and in the PipeW1 method, I did this: 在PipeW1方法中,我这样做:

    void main (int argc, char *argv[]){
    int i;
    char str[6];
    close(fd[0]);   
    for(i=1;i<=500;i++){
    sprintf(str,"%03daaa",i);
    z = write(fd[1],str,6);
    if(z<0) {perror("write process B"); exit(1);}
    if(i%100==0){usleep(100000);}
}
close(fd[1]);
exit(0);
}

any suggestions will be helpful! 任何建议都会有所帮助!

Thanks 谢谢

Pipes remain open through an execve - but you cannot use a variable declared in one program in another (fd[]), it's not only in a different C scope (compilation unit), but it's an entirely different /process image/. 管道通过execve保持打开状态-但是您不能在另一个程序中使用在另一个程序中声明的变量(fd []),它不仅在不同的C范围(编译单元)中,而且是一个完全不同的/ process image /。

The numeric value, however, will remain - that is what identifies the pipe. 但是,该数值将保留-这就是标识管道的内容。 So, pass fd[0], the number which represents the "read" end of the pipe, to the child process. 因此,将代表管道“已读”端的数字fd [0]传递给子进程。 Ordinarily it would be natural to use fork(), but since you exec(), the best way to do this is to pass the number fd[0] as a comand line arg, in the argv[] array. 通常,使用fork()很自然,但是由于您使用exec(),执行此操作的最佳方法是在argv []数组中将数字fd [0]作为命令行arg传递。 Note that this will be a string, so you'll need to use something like snprintf() to create the argument. 请注意,这将是一个字符串,因此您需要使用诸如snprintf()之类的东西来创建参数。

One way to approach the problem is to redirect stdout in the child process. 解决该问题的一种方法是在子进程中重定向stdout This can be done with dup2 . 这可以用dup2 After the fork the child process can call dup2 to connect stdout to the write end of the pipe. fork ,子进程可以调用dup2stdout连接到管道的写入端。 Then the file descriptors for the pipe can be closed. 然后,可以关闭管道的文件描述符。 After the exec , the child writes to the pipe by writing to stdout , eg with printf . exec ,孩子通过写到stdout来写管道,例如用printf

Here is a complete example which demonstrates the concepts. 这是一个演示概念的完整示例。

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

int main( void )
{
    int fd[2];
    pid_t pid;

    if ( pipe( fd ) < 0 )
    {
        perror( "pipe" );
        exit( 1 );
    }

    if ( (pid = fork()) < 0 )
    {
        perror( "fork" );
        exit( 1 );
    }

    if ( pid == 0 )
    {
        dup2( fd[1], STDOUT_FILENO );   // attach the write end of the pipe to stdout
        close( fd[0] );                 // close the pipe (read end)
        close( fd[1] );                 // close the pipe (write end)

        char *args[] = { "./test", "hello", "world", NULL };
        execv( args[0], args );
    }

    // the following code is only for the parent, since the child has exec'd
    close( fd[1] );         // close the write end of the pipe
    char buffer[100];
    ssize_t length = read( fd[0], buffer, sizeof(buffer) - 1 );     // read from the child
    if ( length < 0 )
    {
        perror( "read" );
        exit( 1 );
    }
    buffer[length] = '\0';
    printf( "parent: %s", buffer );
    close( fd[0] );
}

test.c 测试

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

int main( int argc, char *argv[] )
{
    if ( argc < 2 )
    {
        printf( "no args\n" );
        exit( 1 );
    }

    for ( int i = 1; i < argc; i++ )
        printf( "%s%s", argv[i], (i+1 < argc) ? ", " : "\n" );
}

The expected output is 预期的输出是

parent: hello, world 父母:你好,世界

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

相关问题 如何使用 pipe 或任何其他方法将数据从多个子进程发送到父数组? - How to send data from multiple child process to a parent array using pipe or any other method? 从父进程中的.txt中读取数字并使用pipe将其发送到子进程并写入 - Read numbers from .txt in parent process and send it to child process with pipe and write it 如何通过管道将最后一个孩子的号码发送给父母? - How to send a number from last child to parent through a pipe? 如何通过 pipe 从父进程向子进程发送多个字符串? - How to send multiple strings from parent to child processes trough pipe? 子进程如何从管道读取stdout,而父进程如何将stdin写入管道? - How can the child process read stdout from the pipe and the parent process write stdin to the pipe? 如何在父进程和子进程之间发送带有 pipe() 的矩阵? - How can I send a matrix with pipe() between parent process and child process? 如何将数据从父进程发送到子进程并发出信号 - How to send data from parent process to child process and signal 用子进程和父进程写入管道 - writing to a pipe with a child and parent process 子进程和父进程之间的pipe() - pipe() between child and parent process 如何使用C从父进程向子进程发送信号? - How to send a signal from parent to child process using C?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM