为什么管道写入端接受一个字节,然后失败并出现错误 EBADF?
我的程序有问题,它使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一次写入后您将关闭管道。据我了解,一旦关闭就无法恢复。对该文件描述符的后续写入(我实际上在代码中的任何地方都没有看到)将设置错误“坏文件”,因为管道不再存在。
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.
您的问题可能在这里:
当
r
r
r
r
r
r
r
r
r
r
BUF_SIZE
,但是读取一个字节后,r == 1
。相反,请尝试:当您的服务器无法再写入
outfifo
时,这将中断并退出。Your problem is likely here:
You are exiting your loop when
r < BUF_SIZE
, but after you read one byte,r == 1
. Instead, try:That will break and exit when your server can no longer write to
outfifo
.您的代码似乎在写入一个字节后关闭了管道。我没有看到任何循环。
你的读者看起来也有点奇怪。
Your code appears to close the pipe after writing one byte. I don't see any looping.
Your reader looks a little odd also.
我开始绝望,做了一些随机的事情。一个确实有效!我扔掉了每个
close(comPipe[1]);
调用,现在它还活着!感谢大家的帮助:)。仍然不知道为什么它会这样,但也许它会帮助某人......有时:DI 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