首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileObserver不适用于proc目录

FileObserver不适用于proc目录
EN

Stack Overflow用户
提问于 2013-12-04 02:00:24
回答 1查看 1.2K关注 0票数 0

我使用FileObserver监视'/proc/net/arp‘目录,但是onEvent method.The代码中没有任何事件如下:

代码语言:javascript
复制
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从它读取数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-04 02:44:20

文件观察者是基于inotify机制的,但是/proc并不是一般的文件系统。所有的“文件”只是内核的一个接口,您可以通过这个接口从内核获取/设置信息。所有的内容都是动态生成的。这就是inotify不适用于/proc系统的原因。请参阅此page

但是,Ubuntu13.04的用户认为这很好。也许最新的内核支持这样的设施。我在这里复制了这篇文章,原来的页面是here

编译以下程序(inotifyerr.c)

代码语言:javascript
复制
#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上进行了测试,它运行良好:

代码语言:javascript
复制
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也很好

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20365425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档