C语言中的多级管道

C语言中的多级管道,c,unix,pipe,C,Unix,Pipe,我必须用C语言创建一个多级管道,它可以像Unix控制台一样解释Linux命令。我的代码适用于2级管道,但我必须实现更多(最多16个)。这里的主要问题是,我不确定如何获取前两个命令的输出,然后将其重新路由为第三个命令的输入(对于其他级别也是如此)。我知道我需要使用第一个管道的“1”和第二个管道的“0”,因为它们分别是stdout和stdin,但我不确定如何在实践中实现这一点 // tokenize is a separate function that uses strtok repeatedly

我必须用C语言创建一个多级管道,它可以像Unix控制台一样解释Linux命令。我的代码适用于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;
}
//tokenize是一个单独的函数,它在cmdline上重复使用strtok,设置数组
//分段到由|分隔的字符串,numTokens到命令数
字符*段[最大管道段];
int x=0;
int*numTokens=&x;
标记化(段、cmdline、numTokens、“|”);
//一个命令的代码
如果(*numTokens==1){
pid_t pid=fork();
如果(pid==0){
字符*字符串[最大段长度];
int y=0;
int*numStrings=&y;
标记化(字符串、段[0],numStrings,“”);
execvp(字符串[0],字符串);
}否则{
等待(0);
返回;
}
}
pid_t pid=fork();
如果(pid==0){
int i=0;
对于(i=0;i<*numTokens-1;i++){
pid_t pid2=fork();
如果(pid2==0){
int-ps[2];
管道(ps);
pid_t pid3=fork();
如果(pid3==0){
关闭(1);
dup2(ps[1],1);
关闭(ps[0]);
字符*字符串[最大段长度];
int y=0;
int*numStrings=&y;
标记化(字符串、段[i],numStrings,“”);
execvp(字符串[0],字符串);
}否则{
关闭(0);
dup2(ps[0],0);
关闭(ps[1]);
等待(0);
字符*字符串[最大段长度];
int y=0;
int*numStrings=&y;
标记化(字符串、段[i+1],numStrings,“”;
execvp(字符串[0],字符串);
}
}否则{
等待(0);
}
}
}否则{
等待(0);
返回;
}

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

inti,in,out=dup(1);//保存标准输出描述符
对于(i=0;i0);

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

inti,in,out=dup(1);//保存标准输出描述符
对于(i=0;i0);

这看起来很像你昨天问的问题。@Bodo昨天我无法让2级管道工作,现在我让它工作了,因为循环,我的代码非常不同。现在我需要让它在更多级别上工作。你不必获取前两个命令的输出,然后将其重新路由为第三个命令的输入-你只需获取第二个命令的输出。这看起来与你昨天问的问题非常相似。@Bodo昨天我无法让两个级别的管道工作,现在我让它工作起来了,我的代码因为循环的原因而大不相同。现在我需要让它在更多的层次上工作。你不必获取前两个命令的输出,然后将其重新路由到第三个命令的输入,你只需要获取第二个命令的输出。