91在线一级黄片|91视频在线观看18|成人夜间呦呦网站|91资源欧美日韩超碰|久久最新免费精品视频一区二区三区|国产探花视频在线观看|黄片真人免费三级片毛片|国产人无码视频在线|精品成人影视无码三区|久久视频爱久久免费精品

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
通過(guò)Epoll能監(jiān)聽(tīng)普通文件嗎?

epoll 是 Linux 系統(tǒng)中常用的多路復(fù)用 I/O 組件,一般用于監(jiān)聽(tīng) socket 是否能夠進(jìn)行 I/O 操作。那么,epoll 能監(jiān)聽(tīng)普通文件嗎?

成都創(chuàng)新互聯(lián)專注于宕昌網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供宕昌營(yíng)銷型網(wǎng)站建設(shè),宕昌網(wǎng)站制作、宕昌網(wǎng)頁(yè)設(shè)計(jì)、宕昌網(wǎng)站官網(wǎng)定制、重慶小程序開(kāi)發(fā)服務(wù),打造宕昌網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供宕昌網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

我們先通過(guò)下面的例子來(lái)驗(yàn)證一下,epoll 能不能監(jiān)聽(tīng)普通文件:

#include  
#include  
#include  
int main()
{
int epfd, fd;
struct epoll_event ev, events[2];
int result;
epfd = epoll_create(10);
if (epfd "epoll_create()");
return -1;
}


fd = open("./test.txt", O_RDONLY | O_CREAT);

if (fd "open()");
return -1;
}

ev.events = EPOLLIN;
result = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);

if (result "epoll_ctl()");
return -1;
}

epoll_wait(epfd, events, 2, -1);

return 0;
}

編譯并且運(yùn)行,結(jié)果如下:

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

可以看到上面的運(yùn)行結(jié)果報(bào) Operation not permitted 的錯(cuò)誤,這說(shuō)明 epoll 是不能監(jiān)聽(tīng)普通文件的,為什么呢?

尋根究底

我們應(yīng)該對(duì)追尋真相抱著熱衷的態(tài)度,所以必須找出 epoll 不能監(jiān)聽(tīng)普通文件的原因。

因?yàn)樵谏厦娴睦又校?epoll_ctl 函數(shù)報(bào)的錯(cuò),所以我們首先應(yīng)該從 epoll_ctl 的源碼入手,如下:

SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
struct epoll_event __user *, event)
{
int error;
struct file *file, *tfile;

...


error = -EBADF;
file = fget(epfd);  // epoll 句柄對(duì)應(yīng)的文件對(duì)象
if (!file)
goto error_return;


tfile = fget(fd);   // 被監(jiān)聽(tīng)的文件句柄對(duì)應(yīng)的文件對(duì)象
if (!tfile)
goto error_fput;


error = -EPERM; // Operation not permitted 錯(cuò)誤號(hào)
if (!tfile->f_op || !tfile->f_op->poll)
goto error_tgt_fput;

...

error_tgt_fput:
fput(tfile);
error_fput:
fput(file);
error_return:

return error;

}

從上面代碼可以看出,當(dāng)被監(jiān)聽(tīng)的文件沒(méi)有提供 poll 接口時(shí),就會(huì)返回 EPERM 的錯(cuò)誤,這個(gè)錯(cuò)誤就是 Operation not permitted 的錯(cuò)誤號(hào)。

所以,出現(xiàn) Operation not permitted 的原因就是:被監(jiān)聽(tīng)的文件沒(méi)有提供 poll 接口。

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

const struct file_operations ext4_file_operations = {
.llseek         = generic_file_llseek,
.read           = do_sync_read,
.write          = do_sync_write,
.aio_read       = generic_file_aio_read,
.aio_write      = ext4_file_write,
.unlocked_ioctl = ext4_ioctl,
.mmap           = ext4_file_mmap,
.open           = ext4_file_open,
.release        = ext4_release_file,
.fsync          = ext4_sync_file,
.splice_read    = generic_file_splice_read,
.splice_write   = generic_file_splice_write,
;

ext4 文件的文件操作函數(shù)集被設(shè)置為 ext4_file_operations(也說(shuō)就是:file->f_op = ext4_file_operations),從上面代碼可以看出,ext4_file_operations 并沒(méi)有提供 poll 接口。所以,當(dāng)調(diào)用 epoll_ctl 把文件添加到 epoll 中進(jìn)行監(jiān)聽(tīng)時(shí),就會(huì)返回 Operation not permitted 的錯(cuò)誤。

從上面的分析可知,當(dāng)文件系統(tǒng)提供 poll 接口時(shí),就可以把文件添加到 epoll 中進(jìn)行監(jiān)聽(tīng)。


網(wǎng)站欄目:通過(guò)Epoll能監(jiān)聽(tīng)普通文件嗎?
當(dāng)前URL:http://m.jiaoqi3.com/article/djoipsd.html