C语言中的多级管道

卡兰·比贾尼(Karan Bijani)

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

// 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;
}
阿玛利

您会看到,以不同的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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章