gpt4 book ai didi

c++ - 为什么使用 select() 调用对 inotify 实例执行选择性读取

转载 作者:行者123 更新时间:2023-11-28 05:40:50 25 4
gpt4 key购买 nike

基于此http://www.thegeekstuff.com/2010/04/inotify-c-program-example/

示例:https://gist.github.com/pkrnjevic/6016356#file-inotify-example-cpp

while ( run )
{
select( fd + 1, &watch_set, NULL, NULL, NULL ); // non-blocking

int length = read( fd, buffer, EVENT_BUF_LEN );
if ( length < 0 )
{
perror( "read" );
}

对比

while ( run )
{
int length = read( fd, buffer, EVENT_BUF_LEN ); // blocking
if ( length < 0 )
{
perror( "read" );
}

问题> 我需要监控目录中的更改。上面的 select+read 和 read ONLY 方法之间的主要区别是什么。据我所知,select + read 不会阻止读取,它会一次又一次地轮询此调用。这个 select+read 真的给我带来了什么好处吗?

谢谢

最佳答案

此代码实际上是相同的。第一个版本将阻塞直到描述符可读,而不是读取(不会在这次读取时阻塞)。第二个版本将阻塞读取。第二个版本可能(微乎其微)更快,因为它执行一次内核调用而不是两次。

关于c++ - 为什么使用 select() 调用对 inotify 实例执行选择性读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37125002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com