帮助 inotify C 脚本

发布于 2024-11-24 02:11:49 字数 484 浏览 3 评论 0原文

来源

我以前从未使用过 C。该脚本将监听器添加到目录中,并通过回显到终端来通知用户,然后在文件事件发生时退出。我想修改脚本以不退出,而是继续监视文件夹。我认为关键可能是这一行:

length = read( fd, buffer, BUF_LEN );

但我不太明白这里发生了什么。 read() 函数的描述对于那些真正了解 C 的人来说可能会有所帮助:

使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视(监视是一个路径)和事件集),并使用 read() 方法从描述符接收事件信息。 read() 不会消耗稀有的周期,而是会阻塞直到事件发生。

但我不属于这一类。

Source

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-12-01 02:11:55

该程序之所以存在,只是因为一旦它捕获了一个事件,就没有什么可以阻止它到达 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 from fd = 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 the main is designed to be executed in one run-through and exit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文