2012-10-01 54 views
0

我尝试编辑我的文章,因为它存在一些问题。C中的多个管道,等待输入的程序

我仍然迷失,试图扩大我的程序。当我运行程序时,进入只需要一些输入的状态 - 就像,也许是因为我没有在我的管道过程中输入第二个程序。

我试图遵循这个职位代码: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]); 
} 
+0

请问[这个问题]的答案(http://stackoverflow.com/questions/948221/does-this-multiple-pipes-code-in-c-makes-sense)有帮助吗? – legoscia

+0

我看到你接受的答案,谢谢。现在,你能准确解释“不起作用”是什么意思吗?你得到了什么,你期望什么? – netcoder

+0

现在自己发现了,现在工作:)! – user1090614

回答

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 

它是固定的警告是一个好主意,警告是你的朋友。