1
我嘗試在迷你外殼中添加管道。 當我輸入ls |時,我很困惑排序,沒有顯示,我不明白爲什麼:我不明白爲什麼:Pipe()fork()和exec
int fd[2];
if (tube == 1){
int pipeling = pipe(fd);
if (pipeling == -1){
perror("pipe") ;
}
}
tmp = fork(); //FORK A
if (tmp < 0){
perror("fork");
continue;
}
if (tmp != 0) { //parent
while(wait(0) != tmp) ;
continue ;
}
if (tube == 1) { //there is a pipe
if (tmp != 0){ //parent A
close(fd[1]);
}
if (tmp == 0){ //Child A
close(fd[0]);
dup2(fd[1], 1);
close(fd[1]);
execv(mot[0], mot);
}
int tmp2 = fork() ; //FORK B
if (tmp2 != 0) { //Parent B
close(fd[0]);
while(wait(0) != tmp2) ;
continue ;
}
if (tmp2 == 0){ //Child B
close(fd[1]);
dup2(fd[0], 0);
close(fd[0]);
execvp(mot[1], mot);
}
}
我已閱讀所有關於該主題,但它不起作用。 你能幫我嗎?
編輯:第二個代碼,我嘗試改變結構。
謝謝。
看看http://stackoverflow.com/questions/5953386/writing-to-child-process-file-descriptor/5954324#5954324 – 2011-06-02 20:39:37
檢查系統調用的錯誤 - 你至少dup_'ing一個close'd fd在一個點上。什麼是「mot」?我*猜測*是你的execvp參數不正確。 – pilcrow 2011-06-02 20:42:23
@pilcrow,我編輯了代碼,mot是命令行給出的命令。 – lilawood 2011-06-03 06:55:19