相关疑难解决方法(0)

在C中实现多个管道

我正在尝试在C中的shell中实现多个管道.我在这个网站上找到了一个教程,我所做的功能就是基于这个例子.这是功能

void executePipes(cmdLine* command, char* userInput) {
    int numPipes = 2 * countPipes(userInput);
    int status;
    int i = 0, j = 0;
    int pipefds[numPipes];

    for(i = 0; i < (numPipes); i += 2)
        pipe(pipefds + i);

    while(command != NULL) {
        if(fork() == 0){

            if(j != 0){
                dup2(pipefds[j - 2], 0);
            }

            if(command->next != NULL){
                dup2(pipefds[j + 1], 1);
            }    

            for(i = 0; i < (numPipes); i++){
                close(pipefds[i]);
            }
            if( execvp(*command->arguments, command->arguments) < 0 ){
                perror(*command->arguments);
                exit(EXIT_FAILURE);
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c linux shell pipe

12
推荐指数
2
解决办法
5万
查看次数

标签 统计

c ×1

linux ×1

pipe ×1

shell ×1