S.Steverson
S.Steverson

Reputation: 11

Recursively executing Linux pipe commands in c++

My implementation uses recursion to execute commands with multiple pipes so that I would in theory only need 2 pipes. However, after the second iteration read() prevents my program from halting. I can only execute the " ls -la | cat -A" part of "ls -la | cat -A | cat -A | cat -A" but my program will get stuck at the read() and never exit. If I comment out the reads that are causing this problem, the program gets to the end and exits, but of course then I have no output. Does anyone know why this is happening? I've also tried an iterative implementation, but I get the same error.

char* buf[1000];
int recursion_fd[2];
int output_size;
auto pipe_recursion_fd = pipe(recursion_fd);


void execute_pipe(vector<vector<string>> commands)
{

    //PUT THE COMMAND INTO ARRAY TO CALL WITH EXEC
    char* args[] = {0,0,0,0,0,0,0,0,0,0};
    for(int i=0; i < commands.at(0).size();++i)
    {
        int size = commands.at(0).at(i).size() + 1;
        args[i] = new char[size];
        strcpy(args[i],commands.at(0).at(i).c_str()); 
    }

    //CREAT PIPE AND FORK
    int fd[2];
    pipe(fd);
    int pid = fork();

    //CHILD
    if(pid == 0)
    {
        dup2(recursion_fd[0],STDIN_FILENO);
        dup2(fd[1],STDOUT_FILENO);

        //dup(recursion_fd[0]);
        //dup(fd[1]);

        int exec_status = execvp(args[0],args);
        cout << "exec failed - not a valid command" << endl;
        exit(0);
    }

    //PARENT
    if(pid != 0)
    {
        waitpid(pid,NULL,WNOHANG);
        //close(fd[1]);

        //THIS IS THE RECURSION CONDITION, THE LAST ITERATTION SHOULD PRINT 0
        cout << "   num pipes " << num_pipes(commands) << endl;

        if(num_pipes(commands) > 0)
        {
            //THIS CAUSES HOLD
            output_size = read(fd[0],buf,sizeof(buf));

            printf("inside buf:: %s \n",buf); //PRINT TO TEST ITERATIONS
            write(recursion_fd[1],buf,output_size);
            commands.erase(commands.begin()); //erase completed command
            commands.erase(commands.begin()); //erase pipe
            execute_pipe(commands);
        }

        else
        {
            //THIS CAUSES HOLD
            output_size = read(fd[0],buf,sizeof(buf));

            return;
        }

    }

}

void test_execute_pipe()
{
    string command;
    cout << ":: ";
    getline(cin,command);
    vector<vector<string>> test = seperate_command(command);

    cout << "test size = " << test.size() << endl;
    for(int i=0; i < test.size(); ++i)
    {
        for(int c=0; c < test.at(i).size(); ++c)
        {
            cout << test.at(i).at(c) << "~";
        }
        cout <<endl;    
    }

    memset(buf,0,sizeof(buf));
    execute_pipe(test);

    //cout << "right before buf print" << endl;
    printf("buf:: \n");
    write(STDOUT_FILENO,buf,output_size);
}

int main(int argc, char** argv)
{
    test_execute_pipe();
}

Upvotes: 1

Views: 808

Answers (1)

rici
rici

Reputation: 241861

You seem to assume that you can write an arbitrary amount of data into one end of a pipe, and then retrieve all of it from the other end. But you can't. There is a bit of buffer available but not much; if you write more than that before reading, the write will block.

Also, this:

dup2(recursion_fd[0],STDIN_FILENO);

seems to assume that recursion_fd has been set to something, but I don't see where.

Upvotes: 1

Related Questions