我基于源代码的编写监控文件的程序:https://github.com/kvikas/file-monitor-service/blob/master/异步读取失败
我的程序使用的boost ::支持ASIO :: stream_descriptor :: async_read_some()从异步读取inotify的描述http://linux.die.net/man/7/inotify
我的代码如下:
构造:
void init(){
int fd = inotify_init1(IN_NONBLOCK);
int wd = inotify_add_watch(fd_, "./test.txt", IN_ALL_EVENTS);
stream_.reset(new boost::asio::posix::stream_descriptor(io_service_, fd_)));
}
的异步读取:
template<typename Monitor_Handler>
void async_monitor(Monitor_Handler handler) {
stream_->async_read_some(boost::asio::buffer(buffer_),
boost::bind(&monitor::handle_monitor<Monitor_Handler>,
shared_from_this(), boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred, handler));
}
的处理程序:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
}
的错误是,在第一handle_monitor是在第一个变化调用几次(多个事件如MODIFY,Access中打开...)受监控的文件。之后async_read_some方法被再次调用,但我没有信号了(该handle_monitor不再被调用)
然而,当我试图重启inotify的描述,并再次重新进行添加的监视的文件==>它的工作中, handle_monitor被调用来监视这些文件中的新变化。
修改代码:
template<typename Monitor_Handler>
void handle_monitor(const boost::system::error_code &ec,
std::size_t bytes_transferred, Monitor_Handler handler) {
//process buffer
async_monitor(handler);
init();//for resetting the inotify desciptor
}
难道你们帮我解释一下这个????我渴望你的答案.....
谢谢您的回答,我重写FD变量。但这不是问题。我在这个问题上缩小了我的问题的范围。 http://stackoverflow.com/questions/16397293/the-read-method-on-the-inotify-descriptor-does-not-return你能帮我解释一下吗?希望看到你的回答 – khanhhh89 2013-05-07 02:43:40