epoll_ctl: 操作不允许的错误 - C程序

8
  1 #include <sys/epoll.h>
  2 #include <stdio.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include <string.h>
  7 #include <sys/uio.h>
  8 
  9 int main() {
 10   struct epoll_event event ;
 11   int ret,fd, epfd ;
 12 
 13   fd = open("doc", O_RDONLY);
 14   if( fd < 0 )
 15     perror("open");
 16 
 17   event.data.fd = fd ;
 18   event.events = EPOLLIN|EPOLLOUT ;
 19 
 20   epfd = epoll_create(50);
 21   printf("%d", epfd );
 22 
 23   if( epfd < 0 )
 24     perror("epoll_create");
 25 
 26   ret = epoll_ctl( epfd, EPOLL_CTL_ADD, fd, &event ) ;
 27   if( ret < 0 )
 28     perror("epoll_ctl");
 29 
 30 }

编译这段代码时没有错误。

gcc -o epoll epoo.c

但是当我尝试执行程序“epoll”时,出现了错误信息:

epoll_ctl: 操作不允许。

我已经尝试将“doc”文件的模式更改为0777,但没有起作用。

问题出在哪里呢?谢谢 :)

2个回答

14

来自epoll_ctl(2)

   EPERM  The target file fd does not support epoll.

我猜测doc是一个普通文件。普通文件总是准备好进行read(2)write(2)操作,所以在普通文件上使用epoll(7)select(2)没有意义。

如果doc是一个管道或Unix域套接字,请在此评论(这样我就知道删除我的帖子),并修改您的问题,以免其他人犯同样的错误。 :)


你是对的!Doc是一个常规文件。我是编程新手,所以你的答案对我非常有帮助。谢谢 :) - webnoon

1
在这种情况下,您正在打开一个普通文件。 epoll()select()poll()对于普通文件没有意义。
如果它是一个管道或套接字,则:
$mkfifo doc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接