c - 在 C 中实现多个管道时遇到问题

标签 c shell unix

我正在尝试在 C 中实现一些 Shell 命令,但是管道给我带来了一些麻烦(我踢掉了大部分代码以便你们可以编译它 - 如果需要的话)

我非常接近他在 2011 年发布的 Mkabs 解决方案 old post

完成后,我以为我已经把它放下了,但它不起作用。

简单的例子:>> **ls |排序-r **

  • 第一个 child 使用第一个管道 1作为标准输出{1}
  • 第二个 child 使用第一个管道 [0] 作为标准输入{0}

但是两个 exec() 都失败了:ENOENT, No such file or directory
无论我是从 userInput 读取命令还是硬编码给定字符串,它总是失败。

因此错误必须在 ExecutePipe() fkt 中,无需通过其余部分。

    #define _POSIX_C_SOURCE 1
    #include <alloca.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/times.h>
    #include <sys/wait.h>


    typedef char* string;
    static int keepRunning = 1;
    char inputBuffer[512];
    string satz[256];
    int j,comCount, modePipe,mode;

    void Tokens(string);
    void ExecutePipe(void);
    int ScanInput(void);


    void Tokens(string token){
        int len;
        char* ptrC;
        j=0;
        satz[j] = strtok (inputBuffer, token); 
        while (satz[j] != NULL)
        {
            j++;
            satz[j] = strtok (NULL, token); 
        }
        comCount =j;  


        /*REPLACE NEWLINE of LAST COMMAND */
        len =strlen(satz[j-1]);
        ptrC = satz[j-1];
        for (j=0;j<len;j++){
            if (*ptrC == '\n'){
                *ptrC = '\0';
            }
            ptrC++;
        }
    }

    void ExecutePipe(void){
        int* ptrPipe;
        int i,k=0, numPipes;
        pid_t pid, status;
        int  stdin_dupfd,stdout_dupfd;
        string* ptrSatz = satz;
        modePipe=1;
        Tokens("|");
        stdin_dupfd = dup(0);
        stdout_dupfd = dup(1);

        numPipes = comCount-1;

        ptrPipe = (int*) alloca(numPipes*2);

        for(i = 0; i < numPipes; i++){
            if(pipe(ptrPipe + i*2) < 0) {
                printf("Error: pipe(%d)\n",i);
                return;
            }
        }

        while(k<comCount) {
            pid = fork();
            if(pid < 0){
                fprintf(stderr,"Error: PID.%d\n", pid);
                exit(9);
            }
            else if(pid == 0) {  /* child gets input from the previous command*/
                if(k > 0){      /*if not first command*/
                    if(dup2(ptrPipe[(k-1) * 2], 1) < 0){ /*ptr[0]. ptr[2], ptr[4], ...*/    
                        fprintf(stderr,"Error: dup2(firstCommand)\n");
                        exit(10);
                    }
                    else fprintf(stderr,"k[%d]:dupe2([%d], 0)\n",k,(k-1)*2);
                }
                if(k != (comCount-1) ){ /*if not last command*/
                    if(dup2(ptrPipe[k * 2 + 1], 1) < 0){ /*ptr[1]. ptr[3], ptr[5], ...*/    
                        fprintf(stderr,"Error: dup2(notLastCommand)\n");
                        exit(11);
                    }
                    else fprintf(stderr,"k[%d]:dupe2([%d], 1)\n",k,k*2+1);
                }
                for(i = 0; i < 2*numPipes; i++){
                    close(ptrPipe[i]);
                }
                if( execlp(*ptrSatz, *ptrSatz) < 0 ){   /*  */
                    fprintf(stderr,"Error: ");
                    fprintf(stderr,"exec(%s)(%d)\n", *ptrSatz, errno);
                    exit(12);
                }
            }

            fprintf(stderr,"[%d]: %s\n", k,*ptrSatz);
            ptrSatz++;
            k++;
        } 

        for(i = 0; i < 2 * numPipes; i++){
            close(ptrPipe[i]);
            /*printf("loop %d of %d\n ",i, 2*numPipes);*/
        }
        for(i = 0; i < numPipes + 1; i++){
            wait(&status);
        }

        dup2(stdin_dupfd, 0);
        dup2(stdout_dupfd, 1);
        close(stdin_dupfd);     
        close(stdout_dupfd);    

        inputBuffer[0] = '\0';
        modePipe=0;
    }   

    int main (void)
    {
        while (keepRunning){
            printf(">> ");
            fgets(inputBuffer, 512, stdin);   /* input buffer, max.Input(char), whereFrom?*/
            mode = ScanInput(); /*checks inputBuffer on keywords, returns int 1-13*/
            /*printf(">> mode:%d\n", mode);*/
            switch(mode){
                case 11 :
                    ExecutePipe();
                    break; 
            }
        }
        return 0;
    }

    int ScanInput(void){
        char * pch;

        pch = strstr (inputBuffer," | ");
        if (pch != NULL)
            return 11;
        else 
            return 1;
    }

谢谢!

最佳答案

exec 只能调用一个二进制文件。您在这里给出了一个 shell 命令。使用 system() 函数,您也可以调用 shell 命令,但它之所以起作用,只是因为它调用了 bash 来解释它。

最简单的解决方案是通过 system 调用这一行,然后在它之后 exit,例如

exec("complex|shell|command >&3");

需要改成

system("complex|shell|command >&3");
exit(0);

关于c - 在 C 中实现多个管道时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22715271/

相关文章:

c - 一行代码中的多个逻辑运算符

python - python中奇怪的换行符错误

linux - 如何使用 bash 删除文件中的前 N ​​行并保留剩余行

c - 为什么我的父进程没有等待第二次? C/Unix

unix - sed/awk 或其他 : one-liner to increment a number by 1 keeping spacing characters

linux - 如何计算 Shell 中的累积值?

检查在 C 中成功创建的数组

c - 为什么使用 4096 个元素作为 char 数组缓冲区?

c++ - 对结构进行操作并将结果分配给同一结构是不好的做法吗?为什么?

linux - 查看 linux 目录中正在增长的文件