【问题标题】:Multiple pipes in C, program waiting for inputC中的多个管道,程序等待输入
【发布时间】:2012-10-01 17:12:21
【问题描述】:

我尝试编辑我的帖子,因为它存在一些问题。

我仍然迷路,试图多管我的程序。当我运行该程序时,它会进入一种只需要一些输入的状态 - 就像,也许是因为我没有在我的管道过程中获得第二个程序的输入。

我已尝试遵循此帖子中的代码:Does this multiple pipes code in C makes sense?

我的代码如下所示:

int status;
int newpipe[2];
int oldpipe[2];

pid_t pid;
int countcmds = 0;
while (firstCmd != NULL) {
    printf("En iteration \n");
    if (firstCmd -> next != NULL) {
        pipe(newpipe);
    }
    pid = fork();

    if(pid == 0){
        if (firstCmd -> prev != NULL) {
            dup2(oldpipe[0],0);
            close(oldpipe[0]);
            close(oldpipe[1]);
        }
        if (firstCmd -> next != NULL) {
            close(newpipe[0]);
            dup2(newpipe[1],1);
            close(newpipe[1]);
        }
        char** file = firstCmd -> cmd;
        char* specfile = *file;
        execvp(specfile, file);
    }
    else{
        waitpid(pid, &status, 0);
        if (firstCmd -> prev != NULL) {
            close(oldpipe[0]);
            close(oldpipe[1]);
        }
        if(firstCmd -> next != NULL){
            oldpipe[0] = newpipe[0];
            oldpipe[1] = newpipe[1];
        }
        countcmds++;
        firstCmd = firstCmd -> next;
    }
}
if(countcmds){
    close(oldpipe[0]);
    close(oldpipe[1]);
}

【问题讨论】:

  • this question的答案有帮助吗?
  • 我看到你接受了答案,谢谢。现在,你能准确解释一下“不起作用”是什么意思吗?你得到了什么,你期望什么?
  • 我自己发现了,现在可以工作了:)!

标签: c pipe piping


【解决方案1】:

您的execvp(cmd,numberFile); 参数离题了,原型是:

int execvp(const char *file, char *const argv[]);

不知道您的 cmd 是什么样的,但您正在为第二个参数提供 int

execute_piping() 中的execvp 看起来也很可疑,因为您似乎将完整的参数列表提供给第一个参数。

注意:char *argv[] 表示argv 是指向char 的指针数组。我在任何地方都没有看到,但你应该显示结构 CmdShellCmd 是什么。

我从你的代码中得到了这些警告:

gash.c:33: warning: passing argument 1 of ‘execvp’ from incompatible pointer type
gash.c:33: warning: passing argument 2 of ‘execvp’ makes pointer from integer without a cast

修复警告是个好主意,警告是你的朋友。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    相关资源
    最近更新 更多