就像标题所说的那样,我注册了一个文件描述符,它是一个epoll目录,它有什么作用?epoll如何处理引用目录的文件描述符?
回答
Nothing - 注册fd的请求(至少对于常见的Linux文件系统)将会以EPERM
失败。
我测试了这个使用下面的演示程序:
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(void) {
int ep = epoll_create1(0);
int fd = open("/tmp", O_RDONLY|O_DIRECTORY);
struct epoll_event evt = {
.events = EPOLLIN
};
if (ep < 0 || fd < 0) {
printf("Error opening fds.\n");
return -1;
}
if (epoll_ctl(ep, EPOLL_CTL_ADD, fd, &evt) < 0) {
perror("epoll_ctl");
return -1;
}
return 0;
}
结果如下:
[[email protected]:/tmp]$ make epoll
cc epoll.c -o epoll
[[email protected]:/tmp]$ ./epoll
epoll_ctl: Operation not permitted
要弄清楚什么是怎么回事,我去了源。 I happen to knowepoll
的大多数行为由对应于目标文件的struct file_operations
上的->poll
函数确定,其取决于所讨论的文件系统。我拿起ext4
作为一个典型的例子,看着fs/ext4/dir.c
,这definesext4_dir_operations
如下:
const struct file_operations ext4_dir_operations = {
.llseek = ext4_dir_llseek,
.read = generic_read_dir,
.readdir = ext4_readdir,
.unlocked_ioctl = ext4_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = ext4_compat_ioctl,
#endif
.fsync = ext4_sync_file,
.release = ext4_release_dir,
};
注缺乏.poll
定义的,这意味着它会被初始化为NULL
。所以,理性回归epoll的,这是在fs/eventpoll.c
定义,我们找了检查poll
为NULL,我们发现在epoll_ctl
系统调用定义一个early on:
/* The target file descriptor must support poll */
error = -EPERM;
if (!tfile->f_op || !tfile->f_op->poll)
goto error_tgt_fput;
由于我们的试验表明,如果目标文件没有按不支持poll
,插入尝试将仅以EPERM
失败。
其他文件系统有可能在其目录文件对象上定义.poll
方法,但我怀疑它们有很多。
''dirfd(opendir(“/ tmp”))''优于'open(path,O_RDONLY | O_DIRECTORY);'?只是一个风格问题。使用'opendir'不会让fs支持民意调查。 – schmichael 2012-08-06 18:22:04
'dirfd(opendir(“...”))'更便于携带,所以一般来说可能是首选。我是Linux内核黑客,所以我个人倾向于默认使用系统调用接口,即使它不是最合适的,因为我知道它更好。显然这里并不重要,因为'epoll'也是Linux特有的。 – nelhage 2012-08-06 18:25:07
- 1. epoll文件描述符操作
- 2. 复制epoll文件描述符
- 3. 使用带很少量文件描述符的epoll有什么好处吗?
- 4. 返回epoll中的文件描述符的顺序是什么?
- 5. 写入文件描述符时的epoll行为
- 6. 正在为epoll线程重新安装文件描述符吗?
- 7. 谷歌如何处理href的描述?
- 8. ISO CopyHere powershell处理泄漏的文件描述符。 Jenkins
- 9. VC++预处理器符号描述
- 10. 处理文件描述符和SIGKILL信号行为
- 11. 在线程之间共享相同的epoll文件描述符可以吗?
- 12. webservice描述符文件
- 13. 关闭文件描述符
- 14. 创建文件描述符
- 15. Linux open()文件描述符
- 16. 写作文件描述符
- 17. 实现文件描述符
- 18. 文件描述符轮询
- 19. c strcpy文件描述符
- 20. 如何使用该目录的文件描述符在内核级别打开目录?
- 21. 调用fdopen:坏的文件描述符
- 22. 套接字描述符与文件描述符
- 23. Artifactory如何管理Ivy描述符
- 24. 如何从Maven Assembly插件的另一个描述符中调用描述符?
- 25. 如何处理由套接字服务器中的select()引起的“错误文件描述符”?
- 26. 用文件描述符调用dlopen?
- 27. 了解用于处理文件描述符的linux内核数据结构
- 28. 文件描述符和打开文件
- 29. 如何访问打开文件的文件描述符
- 30. 我如何找出文件的文件描述符
如果要在Linux上监视文件系统事件,请使用['inotify'](http://linux.die.net/man/7/inotify)。 – jxh 2012-08-06 18:10:17