Linux module(Linux 模块)

来源:互联网 发布:基于oracle的sql优化 编辑:程序博客网 时间:2024/06/05 20:47

Linux操作系统的内核(kernel)是完全由C语言编写而成。,符合POSIX标准,以GNU通用公共许可证发布。

我们可以说Linux的kernel就是Linux, 可见kernel对于操作系统的重要性。


Linux module是什么?

Linux module 就是能够在运行的时候加入到Linux kernel, 并且可以从Linux kernel中去除的程序。 即 Binary code that is inserted and unloaded out of the static kernel image at run time。


Linux module 的好处(benefit)是什么呢?

首先, 我们知道Linux kernel 本身是一个monolithic kernel(单内核)。当我们的电脑is  booted(被引导), 这个static binary(静态二进制的)的 就开始运行。  Linux kernel具有很多的functionality, 但是为了使得我们的Linux操作系统更加的灵活(flexible), 动态(dynamic), 

当我们想要动态的向kernel中添加更多的functionalities, so that you donot have to recompile the entire kernel from scrach

and reboot。  所以Linux 操作系统引入了模块机制。  这样, 当我们plug in a new device 的时候, you donnot have to rewrite the entire operating system from scrach。 Just add the new functionality to recognize your device。 我们所做的就是写一个module, 当我们使用这个device的时候 , 我们只需要将我们写的识别module插入到kernel中。 当我们不使用这个device的时候, uninstall the module from the kernel。



Linux module 是如何工作的?

当我们想要计算机加入一个新的device, 我们希望我们的kernel具有一个新的functionality 去和这个device 交流。 此时我们需要写一个module, 从而处理这个加入的device。

kernel是唯一能够和硬件交流的东西, 所以我们编译(compile)我们的module, 然后将其加入到kernel中去。 加入module到kernel用到的指令是:

insmod.


使用指令insmod, 在module内部调用了函数init_modules, 于是将这个module插入到kernel中了(上图未画出新的的device(硬件))(上述kernel中的register——capability 表示module的完成的功能, 对该如何处理新的device的相关信息)。

于是我们就可以通过kernel(利用编写的module)想加入的新的device实现读写操作了。

当我们结束的时候, 我们可以使用指令rmmode 指令将这个module从kernel中删除:







0 0
原创粉丝点击