ç管道到另一个程序的STDIN [英] C Pipe to STDIN of Another Program

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

问题描述

我勉强可以理解为管手册页,所以我还挺需要帮助了解如何采取管道输入在外部可执行文件。

我有2个方案: main.o中&安培; log.o

我写的 main.o中以叉子。下面是它在做什么:


  • 父叉的意志的管道的数据子

  • 儿童叉的意志的 EXEC log.o

我需要为主要子叉管的STDIN log.o

log.o 只是需要STDIN和放大器;日志与时间戳的文件。

我的code是由各种计算器的页面我不记得&放一些code的;管道手册页:

 的printf(\\ n>中);
而(与fgets(输入,MAXINPUTLINE,标准输入)){
   焦炭BUF;
   INT FD [2],NUM,地位;
   如果(管道(FD)){
      PERROR(管子破了,DOOD);
      返回111;
   }
   开关(叉())​​{
   情况1:
      PERROR(叉是可悲FAIS);
      返回111;   案件0://子
      关闭(FD [1]); //关闭未使用的写端
      而(读(FD [0],&放大器; BUF,1)大于0)写(STDOUT_FILENO,&放大器; BUF,1);      写(STDOUT_FILENO,\\ n,1);
      关闭(FD [0]);
      execlp(./日志,登录,log.txt中,0); //这是我很困惑
      _exit(EXIT_FAILURE);   默认://父
      数据= stuff_happens_here();
      关闭(FD [0]); //关闭未使用的读端
      写(FD [1],数据,strlen的(数据));
      关闭(FD [1]); //读者会看到EOF
      等待(NULL); //等待孩子
   }
   的printf(\\ n>中);
}


解决方案

我想这是你会怎么做:结果
1.主要岔路口,通过管道父传消息给孩子。结果
2.孩子从管道接收消息,重定向消息,STDIN,执行日志。结果
3.日志中收到消息,从标准输入,做一些事情。

要做到这一点,关键是 dup2 重定向文件描述符,从管STDIN 。

这是修改后的简版:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&errno.h中GT;INT主(INT ARGC,CHAR *的argv []){
    INT FD [2];
    烧焦BUF [] =世界,你好!
    如果(管道(FD)){
      PERROR(管子);
      返回-1;
    }
    开关(叉())​​{
        情况1:
            PERROR(叉);
            返回-1;
        情况下0:
            //子
            关闭(FD [1]);
            dup2(FD [0],STDIN_FILENO);
            关闭(FD [0]);
            EXECL(./日志,NULL);
        默认:
            //父
            关闭(FD [0]);
            写(FD [1],buf中,的sizeof(buf中));
            关闭(FD [1]);
            等待(NULL);
    }
    的printf(END〜\\ n);
    返回0;
}

I can barely understand the man page for pipe, so I kinda need help understanding how to take a piped input in an external executable.

I have 2 programs: main.o & log.o

I written main.o to fork. Here is what it is doing:

  • Parent fork will pipe data to the child
  • Child fork will exec log.o

I need the child fork for main to pipe to STDIN of log.o

log.o simply takes STDIN & logs with time stamp to a file.

My code is composed of some code from various StackOverflow pages I dont remember & the man page for pipe:

printf("\n> ");
while(fgets(input, MAXINPUTLINE, stdin)){
   char buf;
   int fd[2], num, status;
   if(pipe(fd)){
      perror("Pipe broke, dood");
      return 111;
   }
   switch(fork()){
   case -1:
      perror("Fork is sad fais");
      return 111;

   case 0: // Child
      close(fd[1]); // Close unused write end
      while (read(fd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1);

      write(STDOUT_FILENO, "\n", 1);
      close(fd[0]);
      execlp("./log", "log", "log.txt", 0); // This is where I am confused
      _exit(EXIT_FAILURE);

   default: // Parent
      data=stuff_happens_here();
      close(fd[0]); // Close unused read end
      write(fd[1], data, strlen(data));
      close(fd[1]); // Reader will see EOF
      wait(NULL); // Wait for child
   }
   printf("\n> ");
}

解决方案

I suppose this is what you're going to do:
1. main fork, parent pass message to child via pipe.
2. child receive message from pipe, redirect message to STDIN, execute log.
3. log receive message from STDIN, do something.

the key to do this is dup2 to redirect file descriptor, from pipe to STDIN.

This is the modified simple version:

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

int main(int argc, char *argv[]) {
    int fd[2];
    char buf[] = "HELLO WORLD!";
    if(pipe(fd)){
      perror("pipe");
      return -1;
    }
    switch(fork()){
        case -1:
            perror("fork");
            return -1;
        case 0:
            // child
            close(fd[1]);
            dup2(fd[0], STDIN_FILENO);
            close(fd[0]);
            execl("./log", NULL);
        default:
            // parent
            close(fd[0]);
            write(fd[1], buf, sizeof(buf));
            close(fd[1]);
            wait(NULL);
    }
    printf("END~\n");
    return 0;
}  

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

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