为什么管道写入端接受一个字节,然后失败并出现错误 EBADF?

发布于 2024-10-10 05:24:41 字数 1898 浏览 4 评论 0原文

我的程序有问题,它使用 IPC 消息队列。虽然 IPC 工作正常,但管道存在问题,我还无法解决。这是我的程序的子进程的代码。它从文件中读取一个字节,然后将其写入管道。

char buf;
  int r;
  r = read(fileR, &buf, 1);
  if(r == 0){//file is empty
   cout<<"Empty"<<endl;
   lseek(fileR, 0, SEEK_SET);
   msgbuf.mtype = subProcessCount+1;
   msgbuf.mtext[0] = whichPid;
   sendAndCheck(queue, 3);
  }else{
   //cout<<whichPid<<" writing "<<buf<<" to pipe"<<endl;
   cout<<"Closing pipe[0]"<<endl;
   close(comPipe[0]);
   if(write(comPipe[1], &buf, 1) == -1){
    switch(errno){
     case EACCES: cout<<"EACCESS"; break;
     case EIDRM: cout<<"EIDRM"; break;
     case ENOENT: cout<<"ENOENT"; break;
     case ENOMEM: cout<<"ENOMEM"; break;
     case ENOSPC: cout<<"ENOSPC"; break;
     case EFAULT: cout<<"EFAULT"; break;
     case EINTR: cout<<"EINTR"; break;
     case EINVAL: cout<<"EINVAL"; break;
     case EPIPE: cout<<"EPIPE"; break;
     case EAGAIN: cout<<"EAGAIN"; break;
     case EBADF: cout<<"EBADF"; break;
     case EFBIG: cout<<"EFBIG"; break;
     case EIO: cout<<"EIO"; break;
     default: cout<<"writefail"<<endl; break;
    }
   }else{
    cout<<"written";
   }
   close(comPipe[1]);
   cout<<"Closing pipe[1]"<<endl;
  }

这是父进程的代码,当子进程完成时应该从这个管道中读取(然后将其写入fifo,但现在并不重要)。

 close(comPipe[1]);   
 cout<<"Closing pipe[1]"<<endl;
 outfifo = open(pathBuf.mtext, O_WRONLY );
while(1){
r = read(comPipe[0], &buffer, BUF_SIZE);
cout<<"Buffer size: "<<r<<endl;
write(outfifo, &buffer, r);
if(r < BUF_SIZE){
  break;
 }
}
close(comPipe[0]);
cout<<"Closing pipe[0]"<<endl;
close(outfifo);

当我测试它时,第一个字节进入管道,但每个下一个字节都会使 write() 返回 -1 并将错误设置为 EBADF。

你知道那里发生了什么吗?提前致谢, 内布里尔

I have a problem with my program, which is using IPC message queue. Althought IPC works fine, there is an issue with pipe, which I wasn't able to resolve yet. This is the code of subprocess of my program. It reads one byte from a file and then is supposed to write it to a pipe.

char buf;
  int r;
  r = read(fileR, &buf, 1);
  if(r == 0){//file is empty
   cout<<"Empty"<<endl;
   lseek(fileR, 0, SEEK_SET);
   msgbuf.mtype = subProcessCount+1;
   msgbuf.mtext[0] = whichPid;
   sendAndCheck(queue, 3);
  }else{
   //cout<<whichPid<<" writing "<<buf<<" to pipe"<<endl;
   cout<<"Closing pipe[0]"<<endl;
   close(comPipe[0]);
   if(write(comPipe[1], &buf, 1) == -1){
    switch(errno){
     case EACCES: cout<<"EACCESS"; break;
     case EIDRM: cout<<"EIDRM"; break;
     case ENOENT: cout<<"ENOENT"; break;
     case ENOMEM: cout<<"ENOMEM"; break;
     case ENOSPC: cout<<"ENOSPC"; break;
     case EFAULT: cout<<"EFAULT"; break;
     case EINTR: cout<<"EINTR"; break;
     case EINVAL: cout<<"EINVAL"; break;
     case EPIPE: cout<<"EPIPE"; break;
     case EAGAIN: cout<<"EAGAIN"; break;
     case EBADF: cout<<"EBADF"; break;
     case EFBIG: cout<<"EFBIG"; break;
     case EIO: cout<<"EIO"; break;
     default: cout<<"writefail"<<endl; break;
    }
   }else{
    cout<<"written";
   }
   close(comPipe[1]);
   cout<<"Closing pipe[1]"<<endl;
  }

This is the code of the parent process, which should read from this pipe when the child process finishes (and then write it to fifo, but now it's not important).

 close(comPipe[1]);   
 cout<<"Closing pipe[1]"<<endl;
 outfifo = open(pathBuf.mtext, O_WRONLY );
while(1){
r = read(comPipe[0], &buffer, BUF_SIZE);
cout<<"Buffer size: "<<r<<endl;
write(outfifo, &buffer, r);
if(r < BUF_SIZE){
  break;
 }
}
close(comPipe[0]);
cout<<"Closing pipe[0]"<<endl;
close(outfifo);

When I test it, first byte goes into the pipe, but every next byte makes write() return -1 and set error to EBADF.

Have you got a clue about what is going on there? Thanks in advance,
Nebril

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

全部不再 2024-10-17 05:24:41

一次写入后您将关闭管道。据我了解,一旦关闭就无法恢复。对该文件描述符的后续写入(我实际上在代码中的任何地方都没有看到)将设置错误“坏文件”,因为管道不再存在。

You are closing the pipe after one write. As I understand it there is no getting it back once it is closed. Subsequent writes to that file descriptor (which I don't actually see anywhere in your code) will set the error "bad file" because the pipe no longer exists.

花想c 2024-10-17 05:24:41

您的问题可能在这里:

r = read(comPipe[0], &buffer, BUF_SIZE);
cout<<"Buffer size: "<<r<<endl;  
write(outfifo, &buffer, r);
if(r < BUF_SIZE){ 

r r r r r r r r r r BUF_SIZE,但是读取一个字节后,r == 1。相反,请尝试:

written = write(outfifo, &buffer, r);
if (written < r) {

当您的服务器无法再写入 outfifo 时,这将中断并退出。

Your problem is likely here:

r = read(comPipe[0], &buffer, BUF_SIZE);
cout<<"Buffer size: "<<r<<endl;  
write(outfifo, &buffer, r);
if(r < BUF_SIZE){ 

You are exiting your loop when r < BUF_SIZE, but after you read one byte, r == 1. Instead, try:

written = write(outfifo, &buffer, r);
if (written < r) {

That will break and exit when your server can no longer write to outfifo.

梦中的蝴蝶 2024-10-17 05:24:41

您的代码似乎在写入一个字节后关闭了管道。我没有看到任何循环。

你的读者看起来也有点奇怪。

Your code appears to close the pipe after writing one byte. I don't see any looping.

Your reader looks a little odd also.

橘香 2024-10-17 05:24:41

我开始绝望,做了一些随机的事情。一个确实有效!我扔掉了每个 close(comPipe[1]); 调用,现在它还活着!感谢大家的帮助:)。仍然不知道为什么它会这样,但也许它会帮助某人......有时:D

I started beeing desperate and I did some random things. One actually worked! I threw every close(comPipe[1]); call away and now it's alive! Thanks for help guys :). Still no idea why did it behave like that, but maybe it will help someone... sometime :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文