我使用FileObserver监视'/proc/net/arp‘目录,但是onEvent method.The代码中没有任何事件如下:
public class MyFileObserver extends FileObserver{
private Set<OnClientConnectListener> mListeners;
private boolean mWatching = false;
public MyFileObserver(String path) {
super(path);
}
@Override
public void onEvent(int event, String path) {
Log.d("conio","event:"+event+" , path:"+path);
switch(event) {
case FileObserver.MODIFY:
Log.d("conio","event modify");
ArrayList<String> ips = WifiHelper.getClientList(true, 3000);
if(mListeners != null) {
for(OnClientConnectListener lis : mListeners) {
lis.onConnectChange(ips);
}
}
break;
}
}我如何监视'/proc/net/arp',我检查了它是否对这个文件进行了读取,并且我可以使用FileInputStream从它读取数据。
发布于 2013-12-04 02:44:20
文件观察者是基于inotify机制的,但是/proc并不是一般的文件系统。所有的“文件”只是内核的一个接口,您可以通过这个接口从内核获取/设置信息。所有的内容都是动态生成的,。这就是inotify不适用于/proc系统的原因。请参阅此page。
但是,Ubuntu13.04的用户认为这很好。也许最新的内核支持这样的设施。我在这里复制了这篇文章,原来的页面是here。
编译以下程序(inotifyerr.c)
#include <stdlib.h>
#include <stdio.h>
#include <sys/inotify.h>
int main(int argc, char* argv[]){
int fd = inotify_init();
if (fd == -1){
perror("inotify_init");
}
char path[256];
sprintf(path,"/proc/%s",argv[1]);
printf("watching %s\n",path);
int wd = inotify_add_watch(fd,path,IN_ALL_EVENTS);
if (wd == -1){
perror("inotify_add_watch");
}
char buf[1024];
ssize_t siz = read(fd,buf,1024);
if (siz == -1){
perror("inotify read");
}
printf("read done, bytes: %d\n",siz);
}gcc inotifyerr.c
在Ubuntu 13.04上进行了测试,它运行良好:
sworddragon@ubuntu:~/data$ sleep 20 &
[1] 3009
sworddragon@ubuntu:~/data$ ls /proc/3009
attr cgroup comm cwd fd latency map_files mountinfo net oom_adj pagemap sched smaps statm task
autogroup clear_refs coredump_filter environ fdinfo limits maps mounts ns oom_score personality schedstat stack status wchan
auxv cmdline cpuset exe io loginuid mem mountstats numa_maps oom_score_adj root sessionid stat syscall
sworddragon@ubuntu:~/data$ ./a.out 3009
watching /proc/3009
read done, bytes: 128在/proc上使用inotifywait也很好
https://stackoverflow.com/questions/20365425
复制相似问题