Why notification chains
Notification chain 应用于Linux内核各子系统之间,有时一个子系统会关心另外一个子系统发生的事件,这时就可以注册一个关心那个时间的chain。典型例子是USB设备拔插,网络接口的使用。
使用限制
1,仅用于内核的子系统之间。应用层请使用常规进程通信手段如信号量,信号,socket,共享内存,共享文件等。内核到上层的接口有proc,sysctl,sysfs等。
2,在多CPU的机器中,有可能存在多个CPU同时调用notifier_call_chain函数来通知同一个notification chain。所以如果回调函数需要处理临界区域,回调函数需要自己维护互斥关系。
数据结构
int (*notifier_call)(struct notifier_block *, unsighed long ,void *); // 回调函数
struct notifier_block *next; //指向下一个注册项
int priority; // 优先级
}
内核中的Sample Chains
inet6addr_chain
netdev_chain
function
notifier_chain_register(struct notifier_block **nl,struct notifier_block *n) //notification 注册
notifier_chain_unregiser(struct notifier_block **nl , struct notifier_block *n) // notification 注销
/* 通知函数,当某事件发生时调用该函数
@nl: 指向需要通知chain的头
@val: 回调函数使用的参数
@v:回调函数使用的指针
@nr_to_call :没明白呢 Numer of notifier function to be called
@nr_call :Records the number of notifications sent
@returns : 回调函数的返回值
*/
notifier_call_chain(struct notifier_block **nl , unsighed long val , void *v ,int nr_to_call, int *nr_calls)
some wrapper function
本文深入探讨了Linux内核中通知链的应用场景、使用限制、数据结构及内核中的一些实例,帮助开发者了解如何在子系统间进行高效通信。
161

被折叠的 条评论
为什么被折叠?



