<<linux device driver,third edition>> Chapter 2: Building and Running Modules
Kernel Modules Versus Applications
Kernel modules programming is similar to event driven programming.
the task of the module's initialization function is to perpare for later invocation of the module's functions;it's as though the module were saying,"Here I am,and this is what I can do."The module's exit function gets invoked just before the module is unloaded.it should tell the kernel,"I'm not there anymore;don't ask me to do anything else".
A module,is linked only to the kernel,and the only functions it can call are the ones exported by the kernel
Concurrency in the kernel
- interrupt handlers
- symmetric multiprocessor(SMP)
- kernel code has been made preemptible
As a result,Linux kernel code,including driver code,must be reentrant.
A Few Other Details
Kernel code cannot do floating point arithmetic.
Kernel has very small stack.
Often,as you look at the kernel API,you will encounter function name starting with a double undersore(__).Essentially,it says to the programmer:"if you call this function,be sure you know what you are doing"
Compiling Modules
obj-m := hello.o
hello-objs := main.o add.o
all:
make -C /usr/src/XXX/ M=`pwd` modules
clean:
make -C /usr/src/XXX/ M=`pwd` clean
Loading and Unloading Modules
insmod
The program loads the module code and data into the kernel,which,in turn,performs a function similar to the of ld,in that it links any unresolved symbol in the module to the symbol table of the kernel
modprobe
like insmod,load a module into the kernel.It differs in that it will look at the module to be loaded to see whether it references any symbols that are not currently defined in the kernel.If any such references are found,modprobe looks for other modules in the current module search path that define the relevant symbols.When modprobe finds those modules,it loads them into the kernel as well.
The kernel symbol table
EXPORT_SYMBOL(name)
EXPORT_SYMBOL_GPL(name)
Module Parameters
insmod hello.ko howmany=10 whom="Mom"
Parameters are declared with the module_param macro,which is defined in moduleparam.h .module_param takes three parameters: the name of the variable,its type , and a permissions mask to be used for an accompanying sysfs entry.
eg:
static char *whom="world";
static int howmany=1;
module_param(howmany,int,S_IRUGO);
module_param(whom,charp,S_IRUGO);
<<linux device driver,third edition>> Chapter 2: Building and Running Modules的更多相关文章
- <<linux device driver,third edition>> Chapter 4:Debugging Techniques
Debugging by Printing printk lets you classify messages accoring to their severity by associating di ...
- <<linux device driver,third edition>> Chapter 3:Char Drivers
The Internal Representation of Device Numbers Within the kernel,the dev_t type(defined in linux/type ...
- linux device driver —— 环形缓冲区的实现
还是没有接触到怎么控制硬件,但是在书里看到了一个挺巧妙的环形缓冲区实现. 此环形缓冲区实际为一个大小为bufsize的一维数组,有一个rp的读指针,一个wp的写指针. 在数据满时写进程会等待读进程读取 ...
- Linux Device Driver 学习(1)
Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...
- how to write your first linux device driver
how to write your first linux device driver 0. environment-ubuntu 1804 64bit 1. apt-get install linu ...
- Linux Device Driver && Device File
catalog . 设备驱动程序简介 . I/O体系结构 . 访问设备 . 与文件系统关联 . 字符设备操作 . 块设备操作 . 资源分配 . 总线系统 1. 设备驱动程序简介 设备驱动程序是内核的关 ...
- How to learn linux device driver
To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...
- linux device driver —— ioctl
实现了应用程序和设备驱动通过ioctl通信.还是对设备驱动没什么感觉,贴一下代码吧. 在Ubuntu 16.04 64bit中测试通过 ioctldemo.c #include <linux/m ...
- linux device driver —— 字符设备
现在对linux设备驱动还没有什么认识,跟着书上敲了一个字符驱动,这里把代码贴一下. 测试环境是 Ubuntu 16.04 64bit 驱动程序: #include <linux/fs.h> ...
随机推荐
- Java基础——Ajax(二)
一.jQuery 实现 ajax $(function(){ $("#userName").blur(function(){ // 发ajax请求 用的函数原型: $.get(ur ...
- C#特性之数据类型
这篇文章主要通过演示类在不同发展中的不通过定义方法,来向读者表述它们之间的区别和联系. 在C#1时代,我们喜欢这样定义类: public class Product { private string ...
- Python全栈学习_day009知识点
今日大纲: . 函数的初识 . 函数的返回值 . 函数的参数 1. 函数的初识 统计字符串s的总个数(不能用len) s='fkahfkahofijalkfkadhfkjadhf' count = f ...
- Python 利用字典实现类似 java switch case 功能
def add(): print('add') def sub(): print('sub') def exit(): print('exit') choice = { '1' : add, '2' ...
- JS输入框正则校验
1. 开发中需要对etl组件统一进行input输入框校验,允许为空,可以不校验,默认校验长度和特殊字符,代码如下,记录以备复用. /** * 数据值校验工具类 */ var checkService ...
- 根据需要扩展java中的ThreadPoolExecutor
经常被重写的三个方法 ThreadPoolExecutor是可扩展的,通过查看源码可以发现,它提供了几个可以在子类化中改写的方法:beforeExecute,afterExecute,terminat ...
- JSP内置对象——pageContext对象和config对象
它对应的常用方法有: 现在,我新建一个“pageContext.jsp”页面,可以获得“session_page1.jsp”这个页面中保存的用户名: pageContext.jap: session_ ...
- Python交互模式下代码自动补全
这个功能是以lib的形式提供的,配置写到home下的.pythonrc文件中, 并设置好环境变量让python启动时执行初始化: # ~/.pythonrc # enable syntax compl ...
- [HDFS_1] HDFS 的概念和特性
0. 参考 HDFS你一定要知道,要考的 大数据开发实战:HDFS和MapReduce优缺点分析 SecondaryNamenode的作用详解 1. HDFS 是什么 HDFS :一种分布式文件系统, ...
- [Hive_add_2] Hive 数据类型
Hive 数据类型 正常数据类型 # 整型,4个字节 int # 大整型,8个字节 bigint # 字符串,最大长度2G String 复杂数据类型 # 数组,相同类型元素的数组 array< ...