簡體   English   中英

在C ++中實現管道

[英]Implementing a Pipe in C++

我在實施管道時遇到麻煩。 它從文本文件中讀取Unix終端命令,以獲取諸如“ ls | wc”之類的命令,並在其中打開管道,以便將ls的輸出用於wc。

我已經實現了它來解析程序名稱(“ ls”,“ wc”)並將它們存儲在兩個單獨的數組(arguments和arguments2)中,派生一個子進程,然后讓該子派生另一個子進程,然后第二個子進程調用execvp()命令並傳遞要執行的第一個程序。

然后,通過更改標准輸出,將(“ ls”)的輸出寫入管道。 然后,前一個子進程execvp()是另一個進程(“ wc”),並通過更改標准輸入從管道讀取數據。

但是,wc無限循環,並且似乎不計算ls中的單詞數。 Ls在有字數的目錄中執行。

有小費嗎? 非常感謝您,冗長的解釋對不起。

這是一個簡單的示例:它分叉,創建管道並實現“ ls”,並將其輸出寫入管道,返回到父級並從管道讀取“ ls”的輸出。 它似乎永遠無法閱讀或無法正常工作。

//
//  main.cpp
//  Pipe_Test
//
//  Created by Dillon Sheffield on 9/28/15.
//  Copyright © 2015 Dillon Sheffield. All rights reserved.
//

#include <iostream>
using namespace std;

int main()
{
    char* arguments[2];
    char* programArguments[1];
    int fd[2];

    arguments[0] = new char[2];
    arguments[1] = new char[2];
    programArguments[0] = new char[1];
    programArguments[0][0] = '\0';
    string ls = "ls";
    string wc = "wc";
    for (int i = 0; i < 1; i++) {
        arguments[0] = &ls.at(i);
        arguments[1] = &wc.at(i);
    }

    pid_t pid = fork();
    pipe(fd);

    if (pid < 0) {
        perror("Failed.\n");
    } else if (pid == 0) {
        dup2(fd[1], STDOUT_FILENO);

        execvp(arguments[0], programArguments);
    }

    wait(NULL);

    dup2(fd[0], STDIN_FILENO);
    execvp(arguments[1], programArguments);

    close(0);
    close(1);

    return 0;
}

這是我的原始代碼:

//
//  main.cpp
//  homework2
//
//  Created by Dillon Sheffield on 9/19/15.
//  Copyright © 2015 Dillon Sheffield. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;

// Global Variable(s)
const short inputLineSize = 10; // Size of programName, arguments, and argument name.
char *arguments[inputLineSize];
char *arguments2[inputLineSize];
ifstream inputFile;
char* input;

void readLine()
{
    // Create new char array(s)
    input = new char[inputLineSize];

    // Initialize the char array(s)
    for (int i = 0; i < inputLineSize; i++)
    {
        input[i] = '\0';
    }

    // Read a line and skip tabs, spaces, and new line characters
    for (int i = 0; !inputFile.eof() && inputFile.peek() != '\n'; i++)
    {
        while (inputFile.peek() == '\n' || inputFile.peek() == '\t' || inputFile.peek() == ' ') inputFile.get();
        inputFile.get(input[i]);
    }

    // If the file is multi-spaced, keep reading new line char(s) to clear them
    while (inputFile.peek() == '\n') inputFile.get();
}

void parseTokens()
{
    //----------Parse the read line into tokens--------------------------------------------//

    // Get the program name
    for (int i = 0; i < inputLineSize; i++)
    {
        arguments[i] = new char[inputLineSize];
        for (int j = 0; j < inputLineSize; j++)
            arguments[i][j] = '\0';
    }

    int i = 0;
    int j = 0;
    while (input[i] != '\0' && input[i] != '-' && input[i] != '|')
    {
        arguments[j][i] = input[i];
        i++;
    }

    // Tokenize arguments if supplied
    j++;
    int k;
    while (input[i] == '-')
    {
        k = 0;
        arguments[j][k] = input[i];
        i++;
        k++;

        while (input[i] != '-' && input[i] != '\0')
        {
            arguments[j][k] = input[i];
            i++;
            k++;
        }
        j++;
    }

    // Delete unused arguments
    while (j < inputLineSize)
    {
        delete arguments[j];
        arguments[j] = NULL;
        j++;
    }

    // Check if the pipe command '|' is supplied
    if (input[i] == '|')
    {
        i++;

        // Get the other program name
        for (int x = 0; x < inputLineSize; x++)
        {
            arguments2[x] = new char[inputLineSize];
            for (int y = 0; y < inputLineSize; y++)
                arguments2[x][y] = '\0';
        }

        int x = 0;
        int j = 0;
        while (input[i] != '\0' && input[i] != '-' && input[i] != '|')
        {
            arguments2[j][x] = input[i];
            i++;
            x++;
        }

        // Tokenize arguments if supplied
        j++;
        int k;
        while (input[i] == '-')
        {
            k = 0;
            arguments2[j][k] = input[i];
            i++;
            k++;

            while (input[i] != '-' && input[i] != '\0')
            {
                arguments2[j][k] = input[i];
                i++;
                k++;
            }
            j++;
        }

        // Delete unused arguments
        while (j < inputLineSize)
        {
            delete arguments2[j];
            arguments2[j] = NULL;
            j++;
        }
    }
}

int main()
{
    // Variable(s)
    pid_t pid;
    pid_t pid2;
    int fd[2];

//--Open the file named "input"-------------------------------------------------------//

    inputFile.open("input", ios::in);

    // Check if opening the file was successful
    if (inputFile.is_open())
    {
        // Read until the file has reached the end
        while (!inputFile.eof())
        {
            // Read a line and parse tokens
            readLine();
            parseTokens();

//----------Now create a new process with parsed Program Name and Arguments-----------//

            // Create a pipe
            pipe(fd);

            // Fork
            pid = fork();
            if (pid < 0)
            {
                perror("Fork failed.\n");
                return -2;
            }
            else if (pid == 0)
            {
                // Fork again
                pid2 = fork();

                if (pid2 < 0)
                {
                    perror("Fork failed.\n");
                    return -2;
                }
                else if (pid2 == 0)
                {
                    // Change standard output
                    if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) perror("dup2 error to stdout.\n");

                    // Execute the given program
                    execvp(arguments[0], arguments);
                }


                // Change the standard input to the pipe
                if (dup2(fd[0], STDIN_FILENO) != STDIN_FILENO) perror("dup2 error to stdin.\n");

                int returnValue = execvp(arguments2[0], arguments2);
                if (returnValue == -1) perror("Error has occurred.\n");

                // Close the pipe and exit
                close(fd[0]);
                close(fd[1]);
                exit(0);
            }

            // Wait for the child so it doesn't become a Zombie
            wait(NULL);


//----------Clean up-----------------------------------------------------------------//
            delete input;
            input = NULL;
            int i = 0;
            while (arguments[i] != NULL)
            {
                delete arguments[i];
                arguments[i] = NULL;
                i++;
            }
            i = 0;
        }
    }
    else perror("Cannot open file.\n");

    inputFile.close();
    return 0;
}

首先執行pipe() ,然后執行fork() ,然后子進程(執行一些附加工作)將執行命令。

這里的問題是,兩個pipe() d文件描述符都在原始父進程中保持打開狀態。 管道的讀取端,更重要的是管道的寫入端。 從各個方面來看,這些文件描述符的確為您的子進程正確設置,但是由於文件描述符在父進程中也保持打開狀態,因此即使在寫入管道寫端的進程終止后,管道也永遠不會關閉。 。

由於管道的寫入端保持打開狀態,因此在父進程中,正在讀取管道讀取端的子進程將繼續讀取。 永遠。

您需要做的是,在派生之后,在父進程中,關閉管道的讀取和寫入側。 一旦初始進程被派生,父進程就不需要管道,並且其打開文件描述符也將成為障礙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM