【问题标题】:fork and pipe bad file descriptor errorfork 和 pipe 错误的文件描述符错误
【发布时间】:2015-10-18 00:03:39
【问题描述】:

我正在尝试写一个基本的shell,为了方便管道,我写了以下函数,其中tokens是要执行的行,index是在tokens中的位置管道字符(目前,管道的两边都需要空格)。每次我在 shell 中运行管道命令时,两个命令都按预期运行,但无法写入/读取管道,并出现错误“错误文件描述符”。这令人困惑的原因是我的文件重定向工作得很好,所以我不明白为什么它不能与管道一起工作。我已经阅读了文档并查看了一些示例代码,但我想不出我做错了什么。几个小时后,我不知所措,我哪里出错了?

int pipeit(vector<string> tokens,unsigned int index){
    //separate the entire line into two delimited by the pipe
    vector<string> firstline;
    vector<string> secondline;
    for(unsigned int i=0;i<tokens.size();i++){
        if(i==index){
            continue;
        }
        else if(i<index){
            firstline.push_back(tokens[i]);
        }
        else{
            secondline.push_back(tokens[i]);
        }
    }

    //make sure the lines aren't empty
    if(secondline.size()==0||firstline.size()==0){
      fprintf(stderr, "ERROR, expected at least one command at both ends of pipe\n");
    }

    //open a pipe, check that it was successfully created
    int pip[2];
    if(pipe(pip)<0){
      perror("pipe failure");
      return -1;
    }

    //attempt to fork
    int parentstatus=0;
    int childstatus=0;
    switch(fork()){

      //fork failed, close the pipe and return -1 (failure)
      case -1:
        {
          close(pip[0]);
          close(pip[1]);
          perror("fork failure");
          return -1;
        }

      //pid of 0 indicates this is the child process
      case 0:
        {
          //close the write end of the pipe and redirect stdin to the read end
          close(pip[0]);
          int stdIn=dup(0);
          dup2(pip[1],0);

          //once redirect is done, can close the other end of the pipe
          close(pip[1]);

          //parse the second line as a list of commands (not important for question)
          //then restore stdin, close the old file descriptor pointing to the pipe
          execute_line(secondline,builtins);
          dup2(stdIn,0);
          close(stdIn);
          break;
        }

      //pid other than 0 indicates this is the parent process
      default:
        {
          //close the read end of the pipe
          close(pip[1]);

          //redirect stdout to the write end of the pipe
          int stdOut=dup(1);
          dup2(pip[0],1);
          close(pip[0]);

          //execute this line (not important for question)
          parentstatus=execute_line(firstline,builtins);

          //restore stdout and close the temporary fd pointing to the pipe
          dup2(stdOut,1);
          close(stdOut);

          //wait for child process to exit and store it's return value in 'status' (then childstatus)
          int status;
          wait(&status);
          childstatus=status;
          break;
        }
    }

    //return a combination of the parent and child's return status
    //(not standard, I know, just easy and irrelevant for the question)
    return childstatus & parentstatus;
  }

tokens 只是一个以空格分隔的std::vector&lt;string&gt;,其中包含要执行的命令(是的,您需要用空格填充“|”才能使其工作,暂时不用担心)和@ 987654328@ 是列表中“|”的位置性格被发现,让事情变得更容易。出于可运行源文件的目的,只需将上述函数定义粘贴到:

#include <unistd.h> //pipe, dup2, fork
#include <iostream>
#include <string>

int builtins = 0xf00; //not what this actually is, but that doesn't matter atm

using namespace std;

int pipeit(vector<string> tokens, unsigned int index);

void execute_line(vector<string> cmds, int biltins){
    cout << "Executing: " << endl;
    for(unsigned int i = 0; i < cmds.size(); ++i){
        cout<<cmds[i]<< " ";
    }
    cout << endl;
}

int main(){
    vector<string> cmds;
    cmds.push_back(new string("cmd1"));
    cmds.push_back(new string("|"));
    cmds.push_back(new string("cmd2"));
    return pipeit(cmds, 1);
}

【问题讨论】:

  • 如果您将代码标记为c++,您将获得更好的答案。
  • 哦,是的,我忘了我使用的是 c++ 而不仅仅是 c。谢谢!
  • "寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。没有明确的问题陈述对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example."
  • 我很确定具体是什么不清楚。我正在尝试在 shell 中实现管道,我以我认为完整的方式描述了错误,并包含了相关代码
  • 您的代码既不充分(它不包含重现问题所需的所有内容)也不最少。

标签: c++ linux shell


【解决方案1】:

问题是我从管道的写入端读取并写入读取端。每当您使用pipe(my_pipe) 时,my_pipe[0]用于阅读,my_pipe[1]用于书写。这并不意味着我的代码适用于无限管道,其他任何人都可以查看它,但它现在可以使用单管道。
CTC:IRC用户C--

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多