使用 poll 从管道读取
我试图使用轮询从管道读取数据,这里是代码:
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#define MAX_BUFF_SIZE 128
int main(void){
char buff[MAX_BUFF_SIZE];
char *buf;
int fd,ret,len;
struct pollfd fdinfo[1];
if((fd = open("/home/s/pipe", O_RDONLY)) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
fdinfo[0].fd = fd;
fdinfo[0].events = POLLIN|POLLPRI ;
while(1){
ret = poll(fdinfo,1,-1);
if (ret < 0) printf("\n\r error");
if ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) ){
len = read(fdinfo[0].fd, buff, MAX_BUFF_SIZE);
if (len > 0){
buff[len] = 0;
buf = (char *) malloc(len);
memcpy(buf, buff, len);
printf("\n read len %d\n %s",len, buf);
free(buf);
}
}
}
return 0;
}
它似乎工作不正常 - 在每个正确的数据之后,我在输出中得到很多垃圾,但我不知道它来自哪里。所以我寻求帮助。
提前谢谢
im trying to read data from pipe using poll, here is code:
#include <stdio.h>
#include <fcntl.h>
#include <poll.h>
#define MAX_BUFF_SIZE 128
int main(void){
char buff[MAX_BUFF_SIZE];
char *buf;
int fd,ret,len;
struct pollfd fdinfo[1];
if((fd = open("/home/s/pipe", O_RDONLY)) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
fdinfo[0].fd = fd;
fdinfo[0].events = POLLIN|POLLPRI ;
while(1){
ret = poll(fdinfo,1,-1);
if (ret < 0) printf("\n\r error");
if ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) ){
len = read(fdinfo[0].fd, buff, MAX_BUFF_SIZE);
if (len > 0){
buff[len] = 0;
buf = (char *) malloc(len);
memcpy(buf, buff, len);
printf("\n read len %d\n %s",len, buf);
free(buf);
}
}
}
return 0;
}
It does not seems to work fine - after each properly data i got a lot of garbage in output, but i dont know where it comes frome. So im asking for help.
thx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果至少有
MAX_BUFF_SIZE
字节可用,则len
将设置为MAX_BUFF_SIZE
,并且buff[len]
将写入超过buff
末尾的内容。在那之后,所有的赌注都消失了。您可以通过将MAX_BUFF_SIZE - 1
传递给read()
来修复此问题。然而,更令人担忧的是,您没有在
memcpy()
调用中复制\0
,因为字节数包括>\0
是len + 1
。当我们这样做时:
malloc( )
malloc()
的返回值是否为NULL
poll()
错误,您仍在检查 < code>revents 字段,该字段将不再适用于if ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents& POLLPRI) == POLLPRI) )
最好写成if (fdinfo[0].revents & (POLLIN | POLLPRI))
但无论如何都没有必要,因为你已经要求 poll() 只查找这两件事If there is at least
MAX_BUFF_SIZE
bytes available,len
will be set toMAX_BUFF_SIZE
, andbuff[len]
will write past the end ofbuff
. After that, all bets are off. You can fix this by passingMAX_BUFF_SIZE - 1
toread()
.However, more worryingly, you're not copying the
\0
in yourmemcpy()
call, as the number of bytes including the\0
islen + 1
.While we're at it:
malloc()
malloc()
isn'tNULL
poll()
errors, you're still checking therevents
field, which won't be applicable any moreif ( ((fdinfo[0].revents&POLLIN) == POLLIN) || ((fdinfo[0].revents&POLLPRI) == POLLPRI) )
could better be writtenif (fdinfo[0].revents & (POLLIN | POLLPRI))
but shouldn't be necessary anyway, as you've askedpoll()
to look for these two things only