2016-11-20 15 views
0

我目前正在编写一个程序,它使用fork()创建一个子进程。这个子进程应该使用选项“-d”执行shell命令“uniq”,以便从stdin读取。在执行命令后,我想通过管道为“uniq”程序发送字符串作为标准输入。因此,在我将相同的字符串发送给对方后,字符串应打印在stdout上(请参阅uniq手册页:https://linux.die.net/man/1/uniq)。但没有打印。为什么执行uniq -d的子进程不能打印通过管道传递给stdin的重复项?

这是到目前为止我的代码:

void execute_uniq_process() 
{ 
    //create pipe 
    int pipefd[2]; 
    if(pipe(pipefd)<0) 
    { 
     //error handling 
    } 

    //create argv for command: 
    char *argv[] = {"uniq","-d",(char *)0}; 

    //create child process 
    pid_t pid = fork(); 
    int status; 

    switch(pid) 
    { 
     case -1: 
     //error handling 
     break; 

     case 0: 
      //child process 
      //close writing end in pipe 
      close(pipefd[1]); 

      //duplicate stdin 
      dup2(pipefd[0],STDIN_FILENO); 
      close(pipefd[0]); 

      execvp("uniq",argv); 
      exit(errno); 
     break; 

     default: 
      //parent process 
      //close reading end in pipe 
      close(pipefd[0]); 

      //write all commands to pipe 
      write(pipefd[1],"test1",5); 
      write(pipefd[1],"test1",5); 
      write(pipefd[1],"test2",5); 
      //edited: 
      close(pipefd[1]); 

      //waits till child is finished 
      wait(&status); 

      if(WEXITSTATUS(status) != EXIT_SUCCESS) 
      { 
       //error handling 
      } 
     break; 
    } 
} 

所以我希望有在外壳印有“测试1”。我也想知道如何干净地终止uniq过程。我认为写入可能会有问题,所以我必须在管道中每写入一个字符串后模拟一个“输入”。

+1

您必须在等待'uniq'完成之前关闭管道,因为在关闭管道之前它不知道它已经获得了EOF。而且你甚至没有写过一行到'uniq'的标准输入。 'write()'系统调用写入了你要写入的内容,并且肯定不会添加任何自己的意愿的新行。 –

+0

谢谢。在等待uniq进程之前,我编辑了我的帖子并关闭了管道。现在它终止。但直到现在我还不明白为什么没有写入uniq的标准输入。孩子的阅读结束被引导到标准输入。父母在管道中写入。对不起,我是这个主题的新手。 – hiaslosch17

+0

你还没有写入'uniq'的整行输入;在你写的内容中没有新行,所以没有重复('uniq'看到'test1test1test2'没有换行符)。看到我的答案。 –

回答

0

您必须在等待uniq完成之前关闭管道,因为在关闭管道之前,它不会知道它具有EOF。而且您还没有写入一行到uniq的标准输入,因为在写入的数据中没有换行符。系统调用仅写入您要求写入的内容;它当然不会添加任何自己的意愿换行符。

这些变化,以及各式各样的琐事,导致:

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/wait.h> 
#include <unistd.h> 

int main(void) 
{ 
    int pipefd[2]; 
    if (pipe(pipefd) < 0) 
    { 
     fprintf(stderr, "Failed to create pipe\n"); 
     return 1; 
    } 

    char *argv[] = {"uniq", "-d", (char *)0}; 

    pid_t pid = fork(); 

    switch (pid) 
    { 
    case -1: 
     fprintf(stderr, "Failed to fork\n"); 
     return 1; 

    case 0: 
     close(pipefd[1]); 
     dup2(pipefd[0], STDIN_FILENO); 
     close(pipefd[0]); 
     execvp(argv[0], argv); 
     fprintf(stderr, "Failed to exec %s\n", argv[0]); 
     exit(errno); 

    default: 
     close(pipefd[0]); 
     write(pipefd[1], "test1\n", 6); 
     write(pipefd[1], "test1\n", 6); 
     write(pipefd[1], "test2\n", 6); 
     close(pipefd[1]); 
     int status; 
     int corpse = wait(&status); 

     if (WEXITSTATUS(status) != EXIT_SUCCESS) 
     { 
      fprintf(stderr, "Child (%s) exited (PID %d, status 0x%.4X)\n", argv[0], corpse, status); 
     } 
     break; 
    } 
    return 0; 
} 

在运行时该输出test1

相关问题