亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 6182 | 回復(fù): 12
打印 上一主題 下一主題

[C] 監(jiān)控多個文件目錄 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-12-28 10:50 |只看該作者 |倒序瀏覽
我現(xiàn)在要開發(fā)一個程序,程序的功能是監(jiān)控30個目錄。一旦該目錄下有新文件就對新文件作處理,但這30個目錄產(chǎn)生新文件的時間都不統(tǒng)一。我想用多線程的方式去監(jiān)控處理這些目錄,一個線程監(jiān)控一個,如果這個目錄下沒有文件,該線程就休眠。但領(lǐng)導(dǎo)的意思是用單線程的方式來監(jiān)控,這樣會在維護上比較好控制。

大家看看有哪種辦法更可靠方便,或者有啥更好的想法呢

論壇徽章:
0
2 [報告]
發(fā)表于 2009-12-28 11:06 |只看該作者
inotify

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
3 [報告]
發(fā)表于 2009-12-28 11:09 |只看該作者
你需要考慮四個問題:
1,啟動的時候,對啟動前已經(jīng)存在的目錄和文件如何處理;
2,運行的過程中,如果目錄被刪除了如何處理。
3,對性能的要求
4,對實時性的要求


可以用 inotify(7) 做,但是要考慮必要性,因為這會讓程序變得復(fù)雜。

論壇徽章:
1
黑曼巴
日期:2020-02-27 22:54:26
4 [報告]
發(fā)表于 2009-12-28 11:12 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動屏蔽

論壇徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:45
5 [報告]
發(fā)表于 2009-12-28 11:20 |只看該作者
如果采用輪詢的方式,一個線程監(jiān)控30個目錄跟一個線程監(jiān)控1個目錄有多大的區(qū)別?

論壇徽章:
0
6 [報告]
發(fā)表于 2009-12-28 12:40 |只看該作者
FAM
lighttpd里用的這個來監(jiān)控web資源文件是否被修改,lz可以參考一下。

http://oss.sgi.com/projects/fam/index.html
http://www.gnome.org/~veillard/gamin/index.html

論壇徽章:
0
7 [報告]
發(fā)表于 2009-12-28 15:02 |只看該作者
還是inotify吧

論壇徽章:
0
8 [報告]
發(fā)表于 2009-12-28 15:31 |只看該作者
tripwire

論壇徽章:
0
9 [報告]
發(fā)表于 2009-12-29 12:07 |只看該作者

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/select.h>
  4. #include <errno.h>
  5. #include <sys/inotify.h>

  6. typedef struct wd_name {
  7.         int wd;
  8.         char * name;
  9. }WD;

  10. #define MONITOR_NUM  3
  11. char* files[MONITOR_NUM] = {
  12.         "./a",
  13.         "./b",
  14.         "./c"
  15. };

  16. WD wd_array[MONITOR_NUM];

  17. static void inotify_event_handler(struct inotify_event *event)
  18. {  
  19.         int i = 0;
  20.         if (event->mask & IN_ISDIR)
  21.          printf("Object type: %s\n", "Direcotory");
  22.         else
  23.         printf("Object type: %s\n", "file");
  24.        
  25.         for (i=0; i<MONITOR_NUM; i++)
  26.          {
  27.                 if (event->wd != wd_array[i].wd)
  28.                         continue;
  29.                 printf("Object name: %s\n", wd_array[i].name);
  30.                 break;
  31.          }
  32.        
  33.          if ( event->mask & IN_CREATE )
  34.                  {
  35.      if ( event->mask & IN_ISDIR )
  36.       printf( "The directory %s was created.\n", event->name );      
  37.      else
  38.       printf( "The file %s was created.\n", event->name );
  39.     }
  40.    else if ( event->mask & IN_DELETE )
  41.     {
  42.      if ( event->mask & IN_ISDIR )
  43.        printf( "The directory %s was deleted.\n", event->name );      
  44.      else
  45.        printf( "The file %s was deleted.\n", event->name );
  46.     }
  47.    else if ( event->mask & IN_MODIFY )
  48.            {
  49.      if ( event->mask & IN_ISDIR )
  50.        printf( "The directory %s was modified.\n", event->name );
  51.      else
  52.        printf( "The file %s was modified.\n", event->name );
  53.     }
  54. }

  55. int main(int argc, char **argv)
  56. {  
  57. int fd = 0;
  58. int wd = 0;
  59. int i = 0;
  60. int len = 0;
  61. int index = 0;  
  62.        
  63. unsigned char buf[1024] = {0};  
  64. struct inotify_event *event = {0};  

  65. fd = inotify_init();
  66. if (fd < 0) {
  67.                 printf("Fail to initialize inotify.\n");
  68.                 return -1;
  69.         }
  70.        
  71. for (i=0; i<MONITOR_NUM; i++) {
  72.                 wd_array[i].name = files[i];
  73.                 wd = inotify_add_watch(fd, wd_array[i].name, IN_CREATE | IN_MODIFY | IN_DELETE);
  74.                 if (wd < 0) {
  75.                         printf("Can't add watch for %s.\n", wd_array[i].name);
  76.                         return -1;
  77.                 }
  78.                 wd_array[i].wd = wd;
  79.         }
  80.        

  81. for (;;)
  82. {   
  83.         index = 0;
  84.         len = 0;
  85.         while (((len = read(fd, &buf, sizeof(buf))) < 0) && (errno == EINTR));     
  86.   
  87.   while (index < len)
  88.                   {        
  89.                           event = (struct inotify_event *)(buf + index);      
  90.                           inotify_event_handler(event);        
  91.                           index += sizeof(struct inotify_event) + event->len;      
  92.                   }   
  93.         }          
  94.   

  95. for (i=0; i<MONITOR_NUM; i++)
  96.         inotify_rm_watch(fd, wd_array[i].wd);

  97. close(fd);

  98. return 0;
  99. }

復(fù)制代碼

我以前寫的一個玩的inotify程序

論壇徽章:
0
10 [報告]
發(fā)表于 2009-12-29 12:07 |只看該作者
你這種情況只用IN_CREATE就可以了
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP