ImageVerifierCode 换一换
格式:DOC , 页数:83 ,大小:350.50KB ,
资源ID:8988062      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/8988062.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(linux内核编程.doc)为本站上传会员【pc****0】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

linux内核编程.doc

1、Linux内核编程 目 录 1.HELLO, WORLD 3 EXHELLO.C 3 1.1内核模块的编译文件 4 1.2 多文件内核模块 5 2.字符设备文件 8 2.1多内核版本源文件 16 3./PROC文件系统 17 4.使用/PROC进行输入 22 5.和设备文件对话(写和IOCTLS) 30 6.启动参数 44 7.系统调用 47 8.阻塞进程 53 9.替换PRINTK’S 63 10.调度任务 66 11.中断处理程序 71 11.1 Intel 结构上的键盘 72 12

2、.对称多处理 75 常见的错误 76 2.0和2.2版本的区别 76 除此以外 77 其他 78 Goods and Services 78 GNU GENERAL PUBLIC LICENSE 78 注 84 1.Hello, world 当第一个穴居的原始人程序员在墙上凿出第一个“洞穴计算机”的程序时,那是一个打印出用羚羊角上的图案表示的“Hello world”的程序。罗马编程教科书上是以“Salut, Mundi”的程序开始的。我不知道如果人们打破这个传统后会有什么后果,但我认为还是不要去发现这个后果比较安全。 一个内核模块至少包括两个函数:init

3、module,在这个模块插入内核时调用;cleanup_module,在模块被移出时调用。典型情况下,init_module为内核中的某些东西注册一个句柄,或者把内核中的程序提换成它自己的代码(通常是进行一些工作以后再调用原来工作的代码)。Clean_module模块要求撤销init_module进行的所有处理工作,使得模块可以被安全的卸载。 Exhello.c /* hello.c * Copyright (C) 1998 by Ori Pomerantz * * "Hello, world" - the kernel module version. */

4、 /* The necessary header files */ /* Standard in kernel modules */ #include /* We're doing kernel work */ #include /* Specifically, a module */ /* Deal with CONFIG_MODVERSIONS */ #if CONFIG_MODVERSIONS==1 #define MODVERSIONS #include

5、rsions.h> #endif /* Initialize the module */ int init_module() { printk("Hello, world - this is the kernel speaking\n"); /* If we return a non zero value, it means that * init_module failed and the kernel module * can't be loaded */ return 0; } /* Cleanup

6、 - undid whatever init_module did */ void cleanup_module() { printk("Short is the life of a kernel module\n"); } 1.1内核模块的编译文件 一个内核模块不是一个可以独立执行的文件,而是需要在运行时刻连接入内核的目标文件。所以,它们需要用-c选项进行编译。而且,所有的内核模块都必须包含特定的标志: l __KERNEL__——这个标志告诉头文件此代码将在内核模块中运行,而不是作为用户进程。 l MODULE——这个标志告诉头文件要给出适当的内核模块的定义。

7、 l LINUX——从技术上讲,这个标志不是必要的。但是,如果你希望写一个比较正规的内核模块,在多个操作系统上编译,这个标志将会使你感到方便。它可以允许你在独立于操作系统的部分进行常规的编译。 还有其它的一些可被选择包含标志,取决于编译模块是的选项。如果你不能明确内核怎样被编译,可以在in/usr/include/linux/config.h中查到。 l __SMP__——对称多线程。在内核被编译成支持对称多线程(尽管在一台处理机上运行)是必须定义。如果是这样,还需要做一些别的事情(参见第12章)。 l CONFIG_MODVERSIONS——如果CONFIG_MODVERSIONS

8、被激活,你需要在编译是定义它并且包含文件/usr/include/linux/modversions.h。这可以有代码自动完成。 ex Makefile    # Makefile for a basic kernel module CC=gcc MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX hello.o: hello.c /usr/include/linux/version.h $(CC) $(MODCFLAGS) -c hello.c echo insmod hello.o to turn it

9、 on echo rmmod hello to turn if off echo echo X and kernel programming do not mix. echo Do the insmod and rmmod from outside 所以,并不是剩下的事情就是root(你没有把它编译成root,而是在边缘(注1.1)。对吗?),然后就在你的核心内容里插入或移出hello。当你这样做的时候,要注意到你的新模块在/proc/modules里。 而且,编译文件不推荐从X下插入的原因是内核有一条需要用printk打印的消息,它把它送给了控制台。如果你不

10、使用X,它就送到了你使用的虚拟终端(你用Alt-F选择的哪个)并且你可以看到。相反的,如果你使用了X,就有两种可能性。如果用xterm –C打开了一个控制台,输出将被送到哪里。如果没有,输出将被送到虚拟终端7——被X“覆盖”的那个。 如果你的内核变得不稳定,你可以在没有X的情况下得到调试消息。在X外,printk可以直接从内核中输出到控制台。而如果在X里,printk输出到一个用户态的进程(xterm –C)。当进程接收到CPU时间,它会将其送到X服务器进程。然后,当X服务器进程接收到CPU时间,它将会显示,但是一个不稳定的内核意味着系统将会崩溃或重起,所以你不希望显示错误的消息,然后

11、可能被解释给你什么发生了错误,但是超出了正确的时间。 1.2 多文件内核模块 有些时候在几个源文件之间分出一个内核模块是很有意义的。在这种情况下,你需要做下面的事情: 1. 在除了一个以外的所有源文件中,增加一行#define __NO_VERSION__。这是很重要的,因为module.h一般包括kernel_version的定义,这是一个全局变量,包含模块编译的内核版本。如果你需要version.h,你需要把自己把它包含进去,因为如果有__NO_VERSION__的话module.h不会自动包含。 2. 象通常一样编译源文件。 3. 把所有目标文件联编成一个。在X86下,用ld

12、–m elf_i386 –r –o .o <1st source file> 这里给出一个这样的内核模块的例子。 ex start.c    /* start.c * Copyright (C) 1999 by Ori Pomerantz * * "Hello, world" - the kernel module version. * This file includes just the start routine */ /* The necessary header files */ /* Standar

13、d in kernel modules */ #include /* We're doing kernel work */ #include /* Specifically, a module */ /* Deal with CONFIG_MODVERSIONS */ #if CONFIG_MODVERSIONS==1 #define MODVERSIONS #include #endif /* Initialize t

14、he module */ int init_module() { printk("Hello, world - this is the kernel speaking\n"); /* If we return a non zero value, it means that * init_module failed and the kernel module * can't be loaded */ return 0; } ex stop.c    /* stop.c * Copyright (C) 1999 by Ori Pom

15、erantz * * "Hello, world" - the kernel module version. This * file includes just the stop routine. */ /* The necessary header files */ /* Standard in kernel modules */ #include /* We're doing kernel work */ #define __NO_VERSION__ /* This isn't "the" file

16、 * of the kernel module */ #include /* Specifically, a module */ #include /* Not included by * module.h because * of the __NO_VERSION__ */ /* Deal with CONFI

17、G_MODVERSIONS */ #if CONFIG_MODVERSIONS==1 #define MODVERSIONS #include #endif /* Cleanup - undid whatever init_module did */ void cleanup_module() { printk("Short is the life of a kernel module\n"); } ex Makefile    # Makefile for a multifile k

18、ernel module CC=gcc MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX hello.o: start.o stop.o ld -m elf_i386 -r -o hello.o start.o stop.o start.o: start.c /usr/include/linux/version.h $(CC) $(MODCFLAGS) -c start.c stop.o: stop.c /usr/include/linux/version.h $(CC) $(MODCFLAGS) -

19、c stop.c 2.字符设备文件 那么,现在我们是原始级的内核程序员,我们知道如何写不做任何事情的内核模块。我们为自己而骄傲并且高昂起头来。但是不知何故我们感觉到缺了什么东西。患有精神紧张症的模块不是那么有意义。 内核模块同进程对话有两种主要途径。一种是通过设备文件(比如/dev 目录中的文件),另一种是使用proc文件系统。我们把一些东西写入内核的一个主要原因就是支持一些硬件设备,所以我们从设备文件开始。 设备文件的最初目的是允许进程同内核中的设备驱动通信,并且通过它们和物理设备通信(modem,终端,等等)。这

20、种方法的实现如下: 每个设备驱动都对应着一定类型的硬件设备,并且被赋予一个主码。设备驱动的列表和它们的主码可以在in/proc/devices中找到。每个设备驱动管理下的物理设备也被赋予一个从码。无论这些设备是否真的安装,在/dev目录中都将有一个文件,称作设备文件,对应着每一个设备。 例如,如果你进行ls –l /dev/hd[ab] *操作,你将看见可能联结到某台机器上的所有的IDE硬盘分区。注意它们都使用了同一个主码,3,但是从码却互不相同。(声明:这是在PC结构上的情况,我不知道在其他结构上运行的linux是否如此。) 在系统安装时,所有设备文件在mknod命令下被创建。它们必须

21、创建在/dev目录下没有技术上的原因,只是一种使用上的便利。如果是为测试目的而创建的设备文件,比如我们这里的练习,可能放在你编译内核模块的的目录下更加合适。 设备可以被分成两类:字符设备和块设备。它们的区别是块设备有一个用于请求的缓冲区,所以它们可以选择用什么样的顺序来响应它们。这对于存储设备是非常重要的,读取相邻的扇区比互相远离的分区速度会快得多。另一个区别是块设备只能按块(块大小对应不同设备而变化)接受输入和返回输出,而字符设备却按照它们能接受的最少字节块来接受输入。大部分设备是字符设备,因为它们不需要这种类型的缓冲。你可以通过观看ls -l命令的输出中的第一个字符而知道一个设备文件是块

22、设备还是字符设备。如果是b就是块设备,如果是c就是字符设备。 这个模块可以被分成两部分:模块部分和设备及设备驱动部分。Init_module函数调用module_register_chrdev在内核得块设备表里增加设备驱动。同时返回该驱动所使用的主码。Cleanup_module函数撤销设备的注册。 这些操作(注册和注销)是这两个函数的主要功能。内核中的函数不是象进程一样自发运行的,而是通过系统调用,或硬件中断或者内核中的其它部分(只要是调用具体的函数)被进程调用的。所以,当你向内和中增加代码时,你应该把它注册为具体某种事件的句柄,而当你把它删除的时候,你需要注销这个句柄。 设备驱动完全

23、由四个设备_

24、个函数做什么事情的时候,会从那个函数返回一个错误码(一个负数)。但这在cleanup_module中是不可能的,因为它是一个void型的函数。一旦cleanup_module被调用,这个模块就死掉了。然而有一个计数器记录着有多少个内核模块在使用这个模块,这个计数器称为索引计数器(/proc/modules中没行的最后一个数字)。如果这个数字不是0,删除就会失败。模块的索引计数器包含在变量mod_use_count_中。有定义好的处理这个变量的宏(MOD_INC_USE_COUNT和MOD_DEC_USE_COUNT),所以我们一般使用宏而不是直接使用变量mod_use_count_,这样在以后

25、实现变化的时候会带来安全性。 ex chardev.c    /* chardev.c * Copyright (C) 1998-1999 by Ori Pomerantz * * Create a character device (read only) */ /* The necessary header files */ /* Standard in kernel modules */ #include /* We're doing kernel work */ #include

26、dule.h> /* Specifically, a module */ /* Deal with CONFIG_MODVERSIONS */ #if CONFIG_MODVERSIONS==1 #define MODVERSIONS #include #endif /* For character devices */ #include /* The character device * definition

27、s are here */ #include /* A wrapper which does * next to nothing at * at present, but may * help for compatibility * with future versions

28、 * of Linux */ /* In 2.2.3 /usr/include/linux/version.h includes * a macro for this, but 2.0.35 doesn't - so I add * it here if necessary. */ #ifndef KERNEL_VERSION #define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c)) #endif /* Conditional compilation. LINUX_VERSION_COD

29、E is * the code (as per KERNEL_VERSION) of this version. */ #if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0) #include /* for put_user */ #endif #define SUCCESS 0 /* Device Declarations **************************** */ /* The name for our device, as it will

30、appear * in /proc/devices */ #define DEVICE_NAME "char_dev" /* The maximum length of the message from the device */ #define BUF_LEN 80 /* Is the device open right now? Used to prevent * concurent access into the same device */ static int Device_Open = 0; /* The message the device

31、 will give when asked */ static char Message[BUF_LEN]; /* How far did the process reading the message * get? Useful if the message is larger than the size * of the buffer we get to fill in device_read. */ static char *Message_Ptr; /* This function is called whenever a process * at

32、tempts to open the device file */ static int device_open(struct inode *inode, struct file *file) { static int counter = 0; #ifdef DEBUG printk ("device_open(%p,%p)\n", inode, file); #endif /* This is how you get the minor device number in * case you have more than on

33、e physical device using * the driver. */ printk("Device: %d.%d\n", inode->i_rdev >> 8, inode->i_rdev & 0xFF); /* We don't want to talk to two processes at the * same time */ if (Device_Open) return -EBUSY; /* If this was a process, we would have had to * be more

34、careful here. * *In the case of processes, the danger would be *that one process might have check Device_Open *and then be replaced by the schedualer by another *process which runs this function. Then, when *the first process was back on the CPU, it would assume *the device is still no

35、t open. * However, Linux guarantees that a process won't * be replaced while it is running in kernel context. * * In the case of SMP, one CPU might increment *Device_Open while another CPU is here, right after the check. *However, in version 2.0 of the kernel this is not a problem *b

36、ecause there's a lock to guarantee only one CPU will *be kernel module at the same time. *This is bad in terms of performance, so version 2.2 changed it. *Unfortunately, I don't have access to an SMP box *to check how it works with SMP. */ Device_Open++; /* Initialize the mes

37、sage. */ sprintf(Message, "If I told you once, I told you %d times - %s", counter++, "Hello, world\n"); /* The only reason we're allowed to do this sprintf * is because the maximum length of the message * (assuming 32 bit integers - up to 10 digits * with the minu

38、s sign) is less than BUF_LEN, which * is 80. BE CAREFUL NOT TO OVERFLOW BUFFERS, * ESPECIALLY IN THE KERNEL!!! */ Message_Ptr = Message; /* Make sure that the module isn't removed while * the file is open by incrementing the usage count * (the number of opened ref

39、erences to the module, if * it's not zero rmmod will fail) */ MOD_INC_USE_COUNT; return SUCCESS; } /* This function is called when a process closes the * device file. It doesn't have a return value in * version 2.0.x because it can't fail (you must ALWAYS * be able to

40、close a device). In version 2.2.x it is * allowed to fail - but we won't let it. */ #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) static int device_release(struct inode *inode, struct file *file) #else static void device_release(struct inode *inode, struct file *file) #e

41、ndif { #ifdef DEBUG printk ("device_release(%p,%p)\n", inode, file); #endif /* We're now ready for our next caller */ Device_Open --; /* Decrement the usage count, otherwise once you * opened the file you'll never get rid of the module. */ MOD_DEC_USE_COUNT; #if LI

42、NUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) return 0; #endif } /* This function is called whenever a process which * have already opened the device file attempts to * read from it. */ #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) static ssize_t device_read(struct file *file,

43、 char *buffer, /* The buffer to fill with data */ size_t length, /* The length of the buffer */ loff_t *offset) /* Our offset in the file */ #else static int device_read(struct inode *inode, struct file *file, char *buffer, /* The buffer to fill wit

44、h * the data */ int length) /* The length of the buffer * (mustn't write beyond that!) */ #endif { /* Number of bytes actually written to the buffer */ int bytes_read = 0; /* If we're at the end of the message, return 0 * (which signifies e

45、nd of file) */ if (*Message_Ptr == 0) return 0; /* Actually put the data into the buffer */ while (length && *Message_Ptr) { /* Because the buffer is in the user data segment, * not the kernel data segment, assignment wouldn't * work. Instead, we have to use put

46、user which * copies data from the kernel data segment to the * user data segment. */ put_user(*(Message_Ptr++), buffer++); length --; bytes_read ++; } #ifdef DEBUG printk ("Read %d bytes, %d left\n", bytes_read, length); #endif /* Read functio

47、ns are supposed to return the number * of bytes actually inserted into the buffer */ return bytes_read; } /* This function is called when somebody tries to write * into our device file - unsupported in this example. */ #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0) static ss

48、ize_t device_write(struct file *file, const char *buffer, /* The buffer */ size_t length, /* The length of the buffer */ loff_t *offset) /* Our offset in the file */ #else static int device_write(struct inode *inode, struct file *file,

49、 const char *buffer, int length) #endif { return -EINVAL; } /* Module Declarations ***************************** */ /* The major device number for the device. This is * global (well, static, which in this context is global * within this file) b

50、ecause it has to be accessible * both for registration and for release. */ static int Major; /* This structure will hold the functions to be * called when a process does something to the device * we created. Since a pointer to this structure is * kept in the devices table, it can't b

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服