【问题标题】:Multi-level pipes in CC中的多级管道
【发布时间】:2019-10-16 06:24:37
【问题描述】:

我必须在 C 中创建一个多级管道,它可以像 Unix 控制台一样解释 Linux 命令。我的代码适用于 2 级管道,但我必须实现更多(最多 16 个)。这里的主要问题是我不确定如何获取前两个命​​令的输出,然后将其重新路由为第三个命令的输入(其他级别等)。我知道我需要使用第一个管道的“1”和第二个管道的“0”,因为它们分别是标准输出和标准输入,但我不确定如何在实践中实现这一点。

// tokenize is a separate function that uses strtok repeatedly on cmdline, setting the array 
// segments to the strings separated by | and numTokens to the number of commands
char* segments[MAX_PIPE_SEGMENTS];
int x = 0;
int* numTokens = &x;
tokenize(segments, cmdline, numTokens, "|");

// the code for one command
if (*numTokens == 1) {
    pid_t pid = fork();
    if (pid == 0) {
        char* strings[MAX_SEGMENT_LENGTH];
        int y = 0;
        int* numStrings = &y;
        tokenize(strings, segments[0], numStrings, " ");
        execvp(strings[0], strings);
    } else {
        wait(0);
        return;
    }
}

pid_t pid = fork();
if (pid == 0) {
    int i = 0;
    for (i = 0; i < *numTokens-1; i++) {
        pid_t pid2 = fork();
        if (pid2 == 0) {
            int ps[2];
            pipe(ps);
            pid_t pid3 = fork();
            if (pid3 == 0) {
                close(1);
                dup2(ps[1], 1);
                close(ps[0]);
                char* strings[MAX_SEGMENT_LENGTH];
                int y = 0;
                int* numStrings = &y;
                tokenize(strings, segments[i], numStrings, " ");
                execvp(strings[0], strings);
            } else {
                close(0);
                dup2(ps[0], 0);
                close(ps[1]);
                wait(0);
                char* strings[MAX_SEGMENT_LENGTH];
                int y = 0;
                int* numStrings = &y;
                tokenize(strings, segments[i+1], numStrings, " ");
                execvp(strings[0], strings);
            }
        } else {
            wait(0);
        }
    }
} else {
    wait(0);
    return;
}

【问题讨论】:

  • 这看起来与您昨天提出的问题stackoverflow.com/questions/58374595 非常相似。
  • @Bodo 昨天我无法让 2 级管道工作,现在我让它工作了,由于循环,我的代码完全不同。现在我需要让它为更多关卡工作。
  • 您不必获取前两个命​​令的输出,然后将其重新路由为第三个命令的输入 - 您只需获取 秒的输出。

标签: c unix pipe


【解决方案1】:

你看,在不同的if/else 块中处理第一个和第二个命令不能推广到两个以上的命令。可行的是尽可能对管道命令和操作一视同仁,只为第一个和最后一个添加特殊条件:

    int i, in, out = dup(1);    // save standard output descriptor
    for (i = 0; i < x; i++)
    {
        int ps[2];
        if (i < x-1) pipe(ps);  // if not last in line, make a pipe
        pid_t pid = fork();
        if (pid == 0)
        {
            // if not first in line, connect standard input to pipe
            if (i) dup2(in, 0), close(in);
            // if not last in line, connect standard output to pipe
            if (i < x-1) dup2(ps[1], 1), close(ps[1]);
            // if last in line, restore standard output to original
            else dup2(out, 1), close(out);
            char* strings[MAX_SEGMENT_LENGTH];
            int y = 0;
            int* numStrings = &y;
            tokenize(strings, segments[i], numStrings, " ");
            execvp(strings[0], strings);
            exit(1);
        }
        if (i) close(in);
        close(ps[1]);
        in = ps[0]; // the current pipe's read end is the new input
    }
    close(out);
    do ; while (wait(0) > 0);

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多