帮助 inotify C 脚本
我以前从未使用过 C。该脚本将监听器添加到目录中,并通过回显到终端来通知用户,然后在文件事件发生时退出。我想修改脚本以不退出,而是继续监视文件夹。我认为关键可能是这一行:
length = read( fd, buffer, BUF_LEN );
但我不太明白这里发生了什么。 read()
函数的描述对于那些真正了解 C 的人来说可能会有所帮助:
使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视(监视是一个路径)和事件集),并使用 read() 方法从描述符接收事件信息。 read() 不会消耗稀有的周期,而是会阻塞直到事件发生。
但我不属于这一类。
I've never used C before. this script adds a listener onto a directory and notifies the user by echoing to the terminal and then exiting whenever a file event happens. I want to modify the script to NOT exit but instead continuing monitoring the folder. I think the key might be this line:
length = read( fd, buffer, BUF_LEN );
but I don't really understand what is going on here. The description of the read()
function is probably helpful for those who know C really well:
Using inotify is simple: Create a file descriptor, attach one or more watches (a watch is a path and set of events), and use the read() method to receive event information from the descriptor. Rather than burn scarce cycles, read() blocks until events occur.
but I don't fall into that category.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该程序之所以存在,只是因为一旦它捕获了一个事件,就没有什么可以阻止它到达
exit( 0 );
。您可以将从fd = inotify_init();
到( void ) close( fd );
的所有内容包装在一个循环中,只要您愿意,它就会重新开始。问题不在于
length = read( fd, buffer, BUF_LEN );
。该部分只是等待事件发生,并且不会要求程序退出。事实上,main
被设计为在一次运行和退出中执行。The program exists simply because nothing stops it from getting to
exit( 0 );
once it has caught an event. You could wrap everything fromfd = inotify_init();
to( void ) close( fd );
in a loop and it will start over as long as you want it to.The problem isn't in
length = read( fd, buffer, BUF_LEN );
. That part just waits for an event to happen and it doesn't call for the program to exit. It's really that themain
is designed to be executed in one run-through and exit.