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



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

示例:https://gist.github.com/pkrnjevic/6016356#file-示例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并没有阻止读取,它会一次又一次地轮询这个调用。这个精选+阅读真的给我带来了什么好处吗?

感谢

这段代码实际上是相同的。第一个版本将阻止直到描述符可读,然后读取(在读取时不会阻止)。第二个版本将阻止读取。第二个版本可能会更快,因为它只调用一个内核,而不是两个。

最新更新