ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用c创建rw管道的最佳方法是什么?

2019-10-23 23:50:50  阅读:156  来源: 互联网

标签:linux c-4 pipe


我有一个程序A,需要将命令发送到程序B的标准输入,并读回该程序B的输出.(在C中编程,而不仅仅是Linux)

程序A->发送字母A->程序B
ProgramA<-B的输出<-ProgramB 我实际上是第一部分,使用popen()将命令发送到B.我确实知道popen只是一种方法. 那么,使用c进行两种方法的最佳方法是什么?

解决方法:

使用posix功能(因此,将在linux和符合posix标准的任何系统上运行),可以结合使用pipe / execl / dup.简而言之,发生的是:

>创建2个管道(一个读取给孩子,一个写入给孩子)
>分叉当前进程.这保持打开相同的FD
>关闭当前的stdin / stdout.然后使用dup(它使用最低的可用描述符来复制您提供给它的内容)
>执行子进程

注意需要冲洗.
父代的代码为:

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

int main ()
{
    pid_t pid;
    int pipe_in[2];   /* This is the pipe with wich we write to the child process. */
    int pipe_out[2];  /* This is the pipe with wich we read from the child process. */

    if (pipe (pipe_in) || pipe (pipe_out)) {
        fprintf (stderr, "Error in creating pipes!\n");
        exit (1);
    }

    /* Attempt to fork and check for errors */
    if ((pid = fork ()) == -1) {
        fprintf (stderr, "Error in fork!\n");
        exit (1);
    }

    if (pid) {
        /* The parent has the non-zero PID. */
        char temp[100];
        int result;
        FILE* child_in;
        FILE* child_out;

        child_in = fdopen(pipe_out[0],"r");
        child_out = fdopen(pipe_in[1],"w");
        close(pipe_out[1]);
        close(pipe_in[0]);

        fprintf(child_out, "something\n"); 

        fgets(temp,100,child_in);
        printf(" Read from child %s \n", temp); 

        /* Send a command to the child. */
        fprintf(child_out, "quit\n"); 
        fflush(child_out);

        fgets(temp,100,child_in);
        printf(" Read from child %s \n", temp);

        wait (&result);             /* Wait for child to finish */
    }
    else {
        /* The child has the zero pid returned by fork*/
        close (1);
        dup (pipe_out[1]);  /* dup uses the lowest numbered unused file descriptor as new descriptor. In our case this now is 1. */

        close (0);          /* dup uses the lowest numbered unused file descriptor as new descriptor. In our case this now is 0. */
        dup (pipe_in[0]);  

        close (pipe_out[0]);
        close (pipe_out[1]);
        close (pipe_in[0]);
        close (pipe_in[1]);
        execl ("child", "child", NULL);
        exit(1); /* Only reached if execl() failed */
    }
    return 0;
}

一个简单的孩子是:

#include <stdio.h>
#include <string.h>

int main ()
{
    char temp[100];

    do {
        printf ("In child: \n");
        fflush (stdout);
        fgets (temp, 100, stdin);
        printf ("Child read %s\n", temp);
        fflush (stdout);
    } while (!strstr (temp, "quit"));

    return 0;
}

您可以使用以下命令进行编译:

gcc -o parent parent.c
gcc -o child child.c
./parent

你会看到

从儿童中读取在儿童中:
 从孩子那里读孩子已经退出

标签:linux,c-4,pipe
来源: https://codeday.me/bug/20191023/1916446.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有