Chinaunix首页 | 论坛 | 博客
  • 博客访问: 577398
  • 博文数量: 213
  • 博客积分: 6789
  • 博客等级: 准将
  • 技术积分: 1947
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-01 17:11
文章分类

全部博文(213)

文章存档

2012年(9)

2011年(62)

2010年(99)

2009年(43)

分类: LINUX

2009-11-23 10:07:14

uevent:
每当内核注册设备或驱动时都会产生uevent事件,这样用户空间的udev或mdev就有机会捕捉到这些事件,根据匹配的规则作一定的处理,比如在/dev目录下生成设备节点或使用modprobe加载驱动程序,等等。从而实现自动生成设备节点,加载驱动程序等等这些热插拔机制。

inotify
Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除、读、写和卸载操作等。还可以跟踪活动的源头和目标等细节。
使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视器(一个监视器是一个路径和一组事件),然后使用 read() 方法从描述符获取事件信息。read() 并不会用光整个周期,它在事件发生之前是被阻塞的。
更好的是,因为 inotify 通过传统的文件描述符工作,可以利用传统的 select() 系统调用来被动地监控监视器和许多其他输入源。两种方法 — 阻塞文件描述符和使用 select() 都避免了繁忙轮询。

inotify_init()
inotify_init()  initializes  a  new inotify instance and returns a file descriptor associated with a new inotify event queue.
On success, inotify_init() returns a new file descriptor, or -1  if  an error occurred (in which case, errno is set appropriately).
struct inotify_event {
               int      wd;       /* Watch descriptor */
               uint32_t mask;     /* Mask of events */
               uint32_t cookie;   /* Unique cookie associating related
                                     events (for rename(2)) */
               uint32_t len;      /* Size of 'name' field */
               char     name[];   /* Optional null-terminated name */
};

inotify_add_watch()  
int inotify_add_watch(int fd, const char *pathname, uint32_t mask);
       adds  a  new watch, or modifies an existing watch,
       for the file whose location is specified in pathname; the  caller  must
       have read permission for this file.  The fd argument is a file descrip-
       tor referring to the inotify instance whose watch list is to  be  modi-
       fied.   The  events  to  be monitored for pathname are specified in the
       mask bit-mask argument. 
       A  successful  call  to  inotify_add_watch()  returns  the unique watch
       descriptor associated with pathname  for  this  inotify  instance.   If
       pathname  was  not  previously  being watched by this inotify instance,
       then the watch descriptor is newly allocated.  If pathname was  already
       being  watched, then the descriptor for the existing watch is returned.

pathname: watch pathname
fd : fd = inotify_init()
mask: IN_MODIFY, IN_CREATE, IN_DELETE ....

On  success,  inotify_add_watch() returns a non-negative watch descriptor.  On error -1 is returned and errno is set appropriately.

阅读(1382) | 评论(0) | 转发(0) |
0

上一篇:first python

下一篇:MTD

给主人留下些什么吧!~~