开发者

Bad File descriptor

开发者 https://www.devze.com 2022-12-25 20:09 出处:网络
Doesanyone see a problem with this, its not working saying bad file descriptor not sure why? pipe(pipefd[0]);

Does anyone see a problem with this, its not working saying bad file descriptor not sure why?

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(pipef开发者_C百科d[0][0]);
  close(pipefd[0][1]);


Based on your code, I'm guessing pipefd is defined as:

int pipefd[2][2];

Now, when you do:

pipe(pipefd[0])

This only populates pipefd[0][0] and pipefd[0][1].

So when you do:

# Bad descriptor
close(pipefd[1][1]);

you are referencing random junk (you never set pipefd[1][0] or pipefd[1][1]).

From the code shown, I can't see why you aren't just doing:

int pipefd[2];
pipe(pipefd);


The indexes in the second block look suspect.

0

精彩评论

暂无评论...
验证码 换一张
取 消