成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Epoll 能監聽普通文件嗎?

系統 Linux
epoll 是 Linux 系統中常用的多路復用 I/O 組件,一般用于監聽 socket 是否能夠進行 I/O 操作。那么,epoll 能監聽普通文件嗎?

[[398693]]

本文轉載自微信公眾號「Linux內核那些事」,作者songsong001 。轉載本文請聯系Linux內核那些事公眾號。

epoll 是 Linux 系統中常用的多路復用 I/O 組件,一般用于監聽 socket 是否能夠進行 I/O 操作。那么,epoll 能監聽普通文件嗎?

我們先通過下面的例子來驗證一下,epoll 能不能監聽普通文件:

  1. #include <stdio.h> 
  2. #include <sys/epoll.h> 
  3. #include <fcntl.h> 
  4.  
  5. int main() 
  6.    int epfd, fd; 
  7.    struct epoll_event ev, events[2]; 
  8.    int result; 
  9.  
  10.    epfd = epoll_create(10); 
  11.    if (epfd < 0) { 
  12.        perror("epoll_create()"); 
  13.        return -1; 
  14.   } 
  15.  
  16.    fd = open("./test.txt", O_RDONLY | O_CREAT); 
  17.    if (fd < 0) { 
  18.        perror("open()"); 
  19.        return -1; 
  20.   } 
  21.  
  22.    ev.events = EPOLLIN; 
  23.  
  24.    result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); 
  25.    if (result < 0) { 
  26.        perror("epoll_ctl()"); 
  27.        return -1; 
  28.   } 
  29.  
  30.    epoll_wait(epfd, events, 2, -1); 
  31.  
  32.    return 0; 

編譯并且運行,結果如下:

  1. [vagrant@localhost epoll]$ gcc epoll.c -o epoll 
  2. [vagrant@localhost epoll]$ ./epoll 
  3. epoll_ctl(): Operation not permitted 

可以看到上面的運行結果報 Operation not permitted 的錯誤,這說明 epoll 是不能監聽普通文件的,為什么呢?

尋根究底

我們應該對追尋真相抱著熱衷的態度,所以必須找出 epoll 不能監聽普通文件的原因。

因為在上面的例子中,是 epoll_ctl 函數報的錯,所以我們首先應該從 epoll_ctl 的源碼入手,如下:

  1. SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd, 
  2.                struct epoll_event __user *, event) 
  3.    int error; 
  4.    struct file *file, *tfile; 
  5.  
  6.   ... 
  7.  
  8.    error = -EBADF; 
  9.    file = fget(epfd);  // epoll 句柄對應的文件對象 
  10.    if (!file) 
  11.        goto error_return; 
  12.  
  13.    tfile = fget(fd);   // 被監聽的文件句柄對應的文件對象 
  14.    if (!tfile) 
  15.        goto error_fput; 
  16.  
  17.    error = -EPERM; // Operation not permitted 錯誤號 
  18.    if (!tfile->f_op || !tfile->f_op->poll) 
  19.        goto error_tgt_fput; 
  20.  
  21.   ... 
  22.  
  23. error_tgt_fput: 
  24.    fput(tfile); 
  25. error_fput: 
  26.    fput(file); 
  27. error_return: 
  28.  
  29.    return error; 

從上面代碼可以看出,當被監聽的文件沒有提供 poll 接口時,就會返回 EPERM 的錯誤,這個錯誤就是 Operation not permitted 的錯誤號。

所以,出現 Operation not permitted 的原因就是:被監聽的文件沒有提供 poll 接口。

由于我們的文件系統是 ext4,所以我們來看看 ext4 文件系統中的文件是否提供了 poll 接口(位于文件 /fs/ext4/file.c 中):

  1. const struct file_operations ext4_file_operations = { 
  2.   .llseek         = generic_file_llseek, 
  3.   .read           = do_sync_read, 
  4.   .write          = do_sync_write, 
  5.   .aio_read       = generic_file_aio_read, 
  6.   .aio_write      = ext4_file_write, 
  7.   .unlocked_ioctl = ext4_ioctl, 
  8.   .mmap           = ext4_file_mmap, 
  9.   .open           = ext4_file_open, 
  10.  .release        = ext4_release_file, 
  11.  .fsync          = ext4_sync_file, 
  12.  .splice_read    = generic_file_splice_read, 
  13.  .splice_write   = generic_file_splice_write, 

ext4 文件的文件操作函數集被設置為 ext4_file_operations(也說就是:file->f_op = ext4_file_operations),從上面代碼可以看出,ext4_file_operations 并沒有提供 poll 接口。所以,當調用 epoll_ctl 把文件添加到 epoll 中進行監聽時,就會返回 Operation not permitted 的錯誤。

從上面的分析可知,當文件系統提供 poll 接口時,就可以把文件添加到 epoll 中進行監聽。

 

責任編輯:武曉燕 來源: Linux內核那些事
相關推薦

2021-09-22 15:18:48

HTTPS黑客數據安全

2016-02-18 12:01:00

2021-09-23 09:50:37

LinuxWindows命令

2023-04-19 06:59:55

2017-06-02 09:10:15

架構等效性系統

2023-04-18 23:23:58

2024-04-22 09:12:36

CSSflexgrid

2021-02-22 10:32:46

云計算云廠商SaaS

2009-12-09 10:28:06

2025-05-29 01:00:00

文件監聽函數

2021-07-14 09:48:15

Linux源碼Epoll

2021-04-20 13:40:56

Epoll IO

2021-01-31 17:42:49

比特幣貨幣黃金

2024-03-04 00:10:00

并發并行JavaScript

2021-08-30 07:22:15

Go類型interface

2010-06-21 10:09:47

Java

2022-03-17 16:59:38

人工智能GitHub

2019-09-17 10:19:56

程序員裁員團隊

2020-12-30 15:13:34

Python數據工具

2023-09-04 17:48:06

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久久久电影 | 精品视频一区二区 | 日韩精品在线观看免费 | 国产欧美一级二级三级在线视频 | 亚洲成人精品在线 | 一区二区精品视频 | 国产精品18hdxxxⅹ在线 | 欧美日韩国产一区二区三区 | 色婷婷综合久久久中文字幕 | 欧美一级免费看 | 成人精品国产一区二区4080 | 在线观看免费av网站 | 午夜成人免费视频 | 99久久精品国产一区二区三区 | 天天操天天摸天天爽 | 高清不卡毛片 | 国产传媒在线播放 | 欧美精品在线一区二区三区 | 一区二区三区四区在线 | 日本亚洲一区二区 | 久久国产精品免费一区二区三区 | 国产久| 国产一级免费在线观看 | 欧美成人精品一区二区男人看 | 成人一区二区三区在线观看 | 久久蜜桃资源一区二区老牛 | 97av视频在线 | 久久的色 | 福利一区二区在线 | 伊人网站在线观看 | 99精品国自产在线 | 热99在线| 欧美一级二级视频 | 成人av在线播放 | 亚洲狠狠爱 | 黄色国产大片 | 久久精品色视频 | 国产专区在线 | 日韩欧美国产成人一区二区 | 亚洲精品高清视频在线观看 | 日韩在线一区二区 |