【问题标题】:How to send a simple string between two programs using pipes?如何使用管道在两个程序之间发送一个简单的字符串?
【发布时间】:2011-02-16 14:09:18
【问题描述】:

我尝试在网上搜索,但几乎没有任何资源。一个小例子就足够了。

编辑 我的意思是,两个不同的 C 程序相互通信。一个程序应该发送“Hi”,而另一个程序应该接收它。类似的东西。

【问题讨论】:

  • 大概你的意思不是ls | grep ".o"之类的吧?也许对你的意思的更多解释会有所帮助......
  • 来吧,伙计...有点努力。谷歌“c 管道示例代码”。第一个结果是准确的:tldp.org/LDP/lpg/node11.html
  • 我想要两个完全不同的程序之间的通信。我找不到相关的资源。
  • 如果你没有分叉一个进程,那么你需要查看“命名管道”。

标签: c unix pipe


【解决方案1】:

首先,让程序 1 将字符串写入stdout(就好像您希望它出现在屏幕上一样)。然后第二个程序应该从stdin 中读取一个字符串,就好像用户正在从键盘上打字一样。然后你运行:

$ program_1 | program_2

【讨论】:

    【解决方案2】:

    此答案可能对未来的 Google 员工有所帮助。

    #include <stdio.h>
    #include <unistd.h>
    
    int main(){     
         int p, f;  
         int rw_setup[2];   
         char message[20];      
         p = pipe(rw_setup);    
         if(p < 0){         
            printf("An error occured. Could not create the pipe.");  
            _exit(1);   
         }      
         f = fork();    
         if(f > 0){
            write(rw_setup[1], "Hi from Parent", 15);    
         }  
         else if(f == 0){       
            read(rw_setup[0],message,15);       
            printf("%s %d\n", message, r_return);   
         }  
         else{      
            printf("Could not create the child process");   
         }      
         return 0;
    
    }
    

    您可以找到高级双向管道调用示例here

    【讨论】:

      【解决方案3】:

      来自Creating Pipes in C,这向您展示了如何分叉程序以使用管道。如果不想 fork(),可以使用named pipes

      此外,您可以通过将prog1 的输出发送到stdout 并从prog2 中的stdin 读取来获得prog1 | prog2 的效果。您还可以通过打开一个名为 /dev/stdin 的文件来读取标准输入(但不确定它的可移植性)。

      /*****************************************************************************
       Excerpt from "Linux Programmer's Guide - Chapter 6"
       (C)opyright 1994-1995, Scott Burkett
       ***************************************************************************** 
       MODULE: pipe.c
       *****************************************************************************/
      
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <unistd.h>
      #include <sys/types.h>
      
      int main(void)
      {
              int     fd[2], nbytes;
              pid_t   childpid;
              char    string[] = "Hello, world!\n";
              char    readbuffer[80];
      
              pipe(fd);
      
              if((childpid = fork()) == -1)
              {
                      perror("fork");
                      exit(1);
              }
      
              if(childpid == 0)
              {
                      /* Child process closes up input side of pipe */
                      close(fd[0]);
      
                      /* Send "string" through the output side of pipe */
                      write(fd[1], string, (strlen(string)+1));
                      exit(0);
              }
              else
              {
                      /* Parent process closes up output side of pipe */
                      close(fd[1]);
      
                      /* Read in a string from the pipe */
                      nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                      printf("Received string: %s", readbuffer);
              }
      
              return(0);
      }
      

      【讨论】:

      • 嘿斯蒂芬,无论如何我可以将此代码用于两个不同的功能?意味着写入管道是在一个函数中完成的,而在另一个函数中读取管道?像这样的工作代码将不胜感激。
      【解决方案4】:

      Here's a sample:

      int main()
      {
          char buff[1024] = {0};
          FILE* cvt;
          int status;
          /* Launch converter and open a pipe through which the parent will write to it */
          cvt = popen("converter", "w");
          if (!cvt)
          {
              printf("couldn't open a pipe; quitting\n");
              exit(1)
          }
          printf("enter Fahrenheit degrees: " );
          fgets(buff, sizeof (buff), stdin); /*read user's input */
          /* Send expression to converter for evaluation */
          fprintf(cvt, "%s\n", buff);
          fflush(cvt);
          /* Close pipe to converter and wait for it to exit */
          status=pclose(cvt);
          /* Check the exit status of pclose() */
          if (!WIFEXITED(status))
              printf("error on closing the pipe\n");
          return 0;
      }
      

      这个程序的重要步骤是:

      1. popen() 调用在子进程和父进程中的管道之间建立关联。
      2. fprintf() 调用将管道用作普通文件来写入子进程的标准输入或从其标准输出读取。
      3. pclose() 调用关闭管道并导致子进程终止。

      【讨论】:

      • 我认为这个例子没有抓住问题的重点,尽管我承认“转换器”程序是一个不同的程序。第一条评论涉及完全独立的程序之间的通信,这些程序没有兄弟/父母/第二堂兄弟关系。
      【解决方案5】:
      dup2( STDIN_FILENO, newfd )
      

      然后阅读:

      char reading[ 1025 ];
      int fdin = 0, r_control;
      if( dup2( STDIN_FILENO, fdin ) < 0 ){
          perror( "dup2(  )" );
          exit( errno );
      }
      memset( reading, '\0', 1025 );
      while( ( r_control = read( fdin, reading, 1024 ) ) > 0 ){
          printf( "<%s>", reading );
          memset( reading, '\0', 1025 );
      }
      if( r_control < 0 )
          perror( "read(  )" );    
      close( fdin );    
      

      但是,我认为fcntl 可能是一个更好的解决方案

      echo "salut" | code
      

      【讨论】:

        【解决方案6】:

        一个普通的管道只能连接两个相关的进程。它由一个进程创建,并在最后一个进程关闭时消失。

        named pipe,也因其行为而被称为 FIFO,可用于连接两个不相关的进程,并且独立于进程而存在;这意味着即使没有人使用它也可以存在。 FIFO 是使用mkfifo() 库函数创建的。

        示例

        writer.c

        #include <fcntl.h>
        #include <sys/stat.h>
        #include <sys/types.h>
        #include <unistd.h>
        
        int main()
        {
            int fd;
            char * myfifo = "/tmp/myfifo";
        
            /* create the FIFO (named pipe) */
            mkfifo(myfifo, 0666);
        
            /* write "Hi" to the FIFO */
            fd = open(myfifo, O_WRONLY);
            write(fd, "Hi", sizeof("Hi"));
            close(fd);
        
            /* remove the FIFO */
            unlink(myfifo);
        
            return 0;
        }
        

        reader.c

        #include <fcntl.h>
        #include <stdio.h>
        #include <sys/stat.h>
        #include <unistd.h>
        
        #define MAX_BUF 1024
        
        int main()
        {
            int fd;
            char * myfifo = "/tmp/myfifo";
            char buf[MAX_BUF];
        
            /* open, read, and display the message from the FIFO */
            fd = open(myfifo, O_RDONLY);
            read(fd, buf, MAX_BUF);
            printf("Received: %s\n", buf);
            close(fd);
        
            return 0;
        }
        

        注意:为简单起见,上述代码中省略了错误检查。

        【讨论】:

        • 什么是相关流程
        • 可能通过一个或多个父/子关系相关的进程(例如,包括兄弟姐妹)。共同的祖先将创建管道的两端。不相关的进程缺少那个共同的祖先。
        • 如果读者先开始,这将不起作用。一个快速的解决方法是将阅读器的open() 放入一个循环中。但是 +1 因为您提供了两个程序示例。
        • 我认为这个例子需要一些调整才能在 Windows 上工作? unistd.h 是 POSIX 和所有...
        • 是的,它需要针对 Windows 进行调整。 Wikipedia article on named pipes 讨论了 Unix/Windows 的一些差异,而快速的 Google search 可以帮助 Windows 实现。
        【解决方案7】:

        一个程序写入标准输出的内容可以被另一个程序通过标准输入读取。很简单,使用 c 编写 prog1 以使用 printf() 打印内容,prog2 使用 scanf() 读取内容。然后运行

        ./prog1 | ./prog2
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-11
          • 2022-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-06
          相关资源
          最近更新 更多