簡體   English   中英

inotify_add_watch 的 fd 參數是什么?

[英]What is inotify_add_watch's fd parameter?

我正在開發一個監視文件夾和文件的項目,但我不知道inotify_add_watch是如何工作的。 我在網上搜索以查看fd的作用,但我不明白 手冊頁

fd參數是一個文件描述符,它引用要修改其監視列表的 inotify 實例。

這意味着什么?

fd 是inotify_init()inotify_init1()的返回值。

first,  use inotify_init() or inotify_init1() to initialize an inotify 
instance;
second, return a file descriptor (with a new inotify event queue) fd ;
third, use the returned fd in inotify_add_watch to monitor the file path 
you want to monitor.

示例代碼:

  int fd = inotify_init();                
  int wd = inotify_add_watch(fd, argv[1], IN_ALL_EVENTS);                  
  
  for (;;) 

  {   
       fd_set fds;   
       FD_ZERO(&fds);                
       FD_SET(fd, &fds);   


       if (select(fd + 1, &fds, NULL, NULL, NULL) > 0)       

       {   
           int len, index = 0;   
           while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));      
           while (index < len) 

           {   
                  event = (struct inotify_event *)(buf + index);                      
                  _inotify_event_handler(event);                                             
                  index += sizeof(struct inotify_event) + event->len;             
           }   
       }   
  }   
  
  inotify_rm_watch(fd, wd);             
  
  return 0;  

文件描述符基本上是某些輸入/輸出資源的標識符。 使用 inotify 時,需要使用inotify_init初始化實例,該實例返回文件描述符,然后您可以使用inotify_add_watch將要監視的文件添加到實例中。

因此,回答您的問題, inotify_add_watch中的 fd 參數是 inotify 實例的文件描述符,由inotify_init調用返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM