[英]Bad File descriptor
有人看到這個問題嗎,說不正確的文件描述符不知道為什么不起作用?
pipe(pipefd[0]);
if ((opid = fork()) == 0) {
dup2(pipefd[0][1],1);/*send to output*/
close(pipefd[0][0]);
close(pipefd[0][1]);
execlp("ls","ls","-al",NULL);
}
if((cpid = fork())==0){
dup2(pipefd[0][1],0);/*read from input*/
close(pipefd[0][0]);
close(pipefd[1][1]);
execlp("grep","grep",".bak",NULL);
}
close(pipefd[0][0]);
close(pipefd[0][1]);
根據您的代碼,我猜pipefd被定義為:
int pipefd[2][2];
現在,當您這樣做時:
pipe(pipefd[0])
這只會填充pipefd[0][0]
和pipefd[0][1]
。
因此,當您這樣做時:
# Bad descriptor
close(pipefd[1][1]);
您引用的是隨機垃圾(您從未設置pipefd[1][0]
或pipefd[1][1]
)。
從顯示的代碼中,我看不到為什么您不只是這樣做:
int pipefd[2];
pipe(pipefd);
第二個塊中的索引看起來很可疑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.