当前位置: 首页 > news >正文

安达市建设局网站百家号关键词排名优化

安达市建设局网站,百家号关键词排名优化,四川省成都市疫情动态,网络营销策划书封面目录 一、模块传参 二、模块依赖 三、内核空间和用户空间 四、执行流 五、模块编程与应用编程的比较 六、内核接口头文件查询 七、小作业 一、模块传参 module_param(name,type,perm);//将指定的全局变量设置成模块参数 name:全局变量名 type: 使用符号 …

目录

一、模块传参

二、模块依赖

三、内核空间和用户空间

四、执行流

五、模块编程与应用编程的比较

六、内核接口头文件查询

七、小作业


一、模块传参

module_param(name,type,perm);//将指定的全局变量设置成模块参数

name:全局变量名

type:

    使用符号      实际类型                传参方式

    bool         bool           insmod xxx.ko  变量名=0 或 1

    invbool      bool           insmod xxx.ko  变量名=0 或 1

    charp        char *         insmod xxx.ko  变量名="字符串内容"

    short        short          insmod xxx.ko  变量名=数值

    int          int            insmod xxx.ko  变量名=数值

    long         long           insmod xxx.ko  变量名=数值

    ushort       unsigned short insmod xxx.ko  变量名=数值

    uint         unsigned int   insmod xxx.ko  变量名=数值

    ulong        unsigned long  insmod xxx.ko  变量名=数值

perm:给对应文件 /sys/module/name/parameters/变量名 指定操作权限

(这个name是全局变量名)

    #define S_IRWXU 00700

    #define S_IRUSR 00400

    #define S_IWUSR 00200

    #define S_IXUSR 00100

    #define S_IRWXG 00070

    #define S_IRGRP 00040

    #define S_IWGRP 00020

    #define S_IXGRP 00010

    #define S_IRWXO 00007

    #define S_IROTH 00004

    #define S_IWOTH 00002  //不要用 编译出错

    #define S_IXOTH 00001

(这个文件一般设置成0664.就是perm的位置写0664。忘记什么意思的同学可以看下面同学发的。感觉挺全的)

【Linux】文件的权限_linux文件权限_爽帅_的博客-CSDN博客

module_param_array(name,type,&num,perm);

name、type、perm同module_param,type指数组中元素的类型

&num:存放数组大小变量的地址,可以填NULL(确保传参个数不越界)

    传参方式 insmod xxx.ko  数组名=元素值0,元素值1,...元素值num-1  


 

 

用上面命令可以替换字符串

printk和printf差不多但是不支持浮点型打印

下面就是更改完的程序

#include <linux/module.h>
#include <linux/kernel.h>int gx = 10;
char *gstr = "hello";
int garr[5] = {1,2,3,4,5};module_param(gx, int, 0664);
module_param(gstr, charp, 0664);
module_param_array(garr, int, NULL, 0664);int __init testparam_init(void)
{int i = 0;printk("gx = %d\n", gx);printk("gst = %s\n", gstr);for(i = 0;i < 5;i++){printk("%d ", garr[i]);}printk("\n");return 0;
}
void __exit testparam_exit(void)
{printk("testparam will exit\n");
}
MODULE_LICENSE("GPL");
module_init(testparam_init);
module_exit(testparam_exit);

 在makefile里加上我们新的模块

 执行make编译

sudo insmod testparam.ko

dmesg 

 

sudo rmmod testparam 

sudo dmesg -C
sudo insmod testparam.ko gx=100 gstr="hi" garr=5,6,7,8,9

dmesg

 

可用MODULE_PARAM_DESC宏对每个参数进行作用描述,用法:

`MODULE_PARM_DESC(变量名,字符串常量);`

字符串常量的内容用来描述对应参数的作用

modinfo可查看这些参数的描述信息

二、模块依赖

​       既然内核模块的代码与其它内核代码共用统一的运行环境,也就是说模块只是存在形式上独立,运行上其实和内核其它源码是一个整体,它们隶属于同一个程序,因此一个模块或内核其它部分源码应该可以使用另一个模块的一些全局特性。

        一个模块中这些可以被其它地方使用的名称被称为导出符号,所有导出符号被填在同一个表中这个表被称为符号表。

最常用的可导出全局特性为全局变量函数  

查看符号表的命令:nm

nm查看elf格式的可执行文件或目标文件中包含的符号表,用法:

`nm  文件名`  (可以通过man nm查看一些字母含义)

 (.o和.ko也是elf格式的文件)

第一列是相对地址

D表示全局变量

T一般指函数

B未初始化的全局变量或静态局部变量

R加了const的全局变量

上面比较常用,下面是man nm查到的全部含义


           "A" The symbol's value is absolute, and will not be changed by further linking.

           "B"
           "b" The symbol is in the BSS data section.  This section typically contains zero-
               initialized or uninitialized data, although the exact behavior is system
               dependent.

           "C" The symbol is common.  Common symbols are uninitialized data.  When linking,
               multiple common symbols may appear with the same name.  If the symbol is
               defined anywhere, the common symbols are treated as undefined references.

           "D"
           "d" The symbol is in the initialized data section.

           "G"
           "g" The symbol is in an initialized data section for small objects.  Some object
               file formats permit more efficient access to small data objects, such as a
               global int variable as opposed to a large global array.

           "i" For PE format files this indicates that the symbol is in a section specific to
               the implementation of DLLs.  For ELF format files this indicates that the
               symbol is an indirect function.  This is a GNU extension to the standard set of
               ELF symbol types.  It indicates a symbol which if referenced by a relocation
               does not evaluate to its address, but instead must be invoked at runtime.  The
               runtime execution will then return the value to be used in the relocation.

           "I" The symbol is an indirect reference to another symbol.

           "N" The symbol is a debugging symbol.

           "p" The symbols is in a stack unwind section.

           "R"
           "r" The symbol is in a read only data section.

           "S"
           "s" The symbol is in an uninitialized or zero-initialized data section for small
               objects.

           "T"
           "t" The symbol is in the text (code) section.

           "U" The symbol is undefined.

           "u" The symbol is a unique global symbol.  This is a GNU extension to the standard
               set of ELF symbol bindings.  For such a symbol the dynamic linker will make
               sure that in the entire process there is just one symbol with this name and
               type in use.

           "V"
           "v" The symbol is a weak object.  When a weak defined symbol is linked with a
               normal defined symbol, the normal defined symbol is used with no error.  When a
               weak undefined symbol is linked and the symbol is not defined, the value of the
               weak symbol becomes zero with no error.  On some systems, uppercase indicates
               that a default value has been specified.

           "W"
           "w" The symbol is a weak symbol that has not been specifically tagged as a weak
               object symbol.  When a weak defined symbol is linked with a normal defined
               symbol, the normal defined symbol is used with no error.  When a weak undefined
               symbol is linked and the symbol is not defined, the value of the symbol is
               determined in a system-specific manner without error.  On some systems,
               uppercase indicates that a default value has been specified.

           "-" The symbol is a stabs symbol in an a.out object file.  In this case, the next
               values printed are the stabs other field, the stabs desc field, and the stab
               type.  Stabs symbols are used to hold debugging information.

           "?" The symbol type is unknown, or object file format specific.

       ·   The symbol name.

 

两个用于导出模块中符号名称的宏:

EXPORT_SYMBOL(函数名或全局变量名)

EXPORT_SYMBOL_GPL(函数名或全局变量名)   需要GPL许可证协议验证

使用导出符号的地方,需要对这些符号进行extern声明后才能使用这些符号

B模块使用了A模块导出的符号,此时称B模块依赖于A模块,则:

1. 编译次序:先编译模块A,再编译模块B,当两个模块源码在不同目录时,需要:i. 先编译导出符号的模块A ii. 拷贝A模块目录中的Module.symvers到B模块目录 iii. 编译使用符号的模块B。否则编译B模块时有符号未定义错误

2. 加载次序:先插入A模块,再插入B模块,否则B模块插入失败

3. 卸载次序:先卸载B模块,在卸载A模块,否则A模块卸载失败

 

#include <linux/module.h>
#include <linux/kernel.h>int gx = 19;EXPORT_SYMBOL(gx);int __init modulea_init(void)
{printk("In module_a init gx = %d\n", gx);return 0;
}
void __exit modulea_exit(void)
{printk("modulea will exit\n");
}
MODULE_LICENSE("GPL");module_init(modulea_init);
module_exit(modulea_exit);

 

#include <linux/module.h>
#include <linux/kernel.h>extern int gx;int __init moduleb_init(void)
{printk("In module_b init gx = %d\n", gx);return 0;
}
void __exit moduleb_exit(void)
{printk("moduleb will exit\n");
}
MODULE_LICENSE("GPL");module_init(moduleb_init);
module_exit(moduleb_exit);

 一定要先编译提供方在编译使用方

如果先插入B会报错

 

先插入a在插入b就没错

dmesg

 这里有之前插入出错的信息

先移除modulea会出错提示modulea正在被moduleb使用

这样就成功了

 如果这两个模块在两个目录下

进入a目录更改makefile然后make编译成功

 

 

 但是在b中就会失败,因为他缺少a中的全局变量

 Linux内核中模块的编译一般分为以下步骤:

  • 将每个源文件编译为对应的.o目标文件
  • 将每个单独的目标文件链接成模块文件module.o
  • 生成对应的module.mod文件,该文件保存链接到模块的所有原始 .o目标文件
  • 生成modules.order文件,里面保存的是所有的KO文件
  • 从modules.order中查找所有的KO模块
  • 使用modpost,为每个KO模块创建module.mod.c文件
  • 创建Module.symvers文件,保存模块中通过export导出的符号及其CRC值
  • 生成和模块相关的信息(版本魔幻数、模块信息、License、version、alias)
  • 外部模块的版本验证
  • 通过module.symvers文件,检测模块编译需要的内核符号是否存在

 所以我们把符号表粘过去就能正确编译了

补充说明:

内核符号表(直接当文本文件查看)

   /proc/kallsyms运行时    /boot/System.map编译后

这台虚拟机可能是升级过有五个版本的符号表

 

 在内核的源码里可以直接用nm命令查看vmLinux

 非常的多

也可查看

这里就是符号表,可以在这里查看全部的全局特性

 

三、内核空间和用户空间

        为了彻底解决一个应用程序出错不影响系统和其它app的运行,操作系统给每个app一个独立的假想的地址空间,这个假想的地址空间被称为虚拟地址空间(也叫逻辑地址),操作系统也占用其中固定的一部分,32位Linux的虚拟地址空间大小为4G,并将其划分两部分:

1. 0~3G 用户空间 :每个应用程序只能使用自己的这份虚拟地址空间

2. 3G~4G 内核空间:内核使用的虚拟地址空间,应用程序不能直接使用这份地址空间,但可以通过一些系统调用函数与其中的某些空间进行数据通信

实际内存操作时,需要将虚拟地址映射到实际内存的物理地址,然后才进行实际的内存读写

百度百科-验证

内核启动时。在MMU以前使用的都是真实的物理地址。启动后使用的是虚拟地址(3G~4G)

这些虚拟内存都和实际的物理内存有对应关系。

后面驱动操作内核所以使用的也是3G~4G的虚拟内存

四、执行流

执行流:有开始有结束总体顺序执行的一段独立代码,又被称为代码上下文

计算机系统中的执行流的分类:

执行流:

1. 任务流--任务上下文(都参与CPU时间片轮转,都有任务五状态:就绪态  运行态  睡眠态  僵死态  暂停态)

   1.  进程

   2.  线程

       1.  内核线程:内核创建的线程

       2.  应用线程:应用进程创建的线程

(进程线程都是任务,进程是资源占用多的任务。线程是资源占用少的任务) 

2. 异常流--异常上下文

   1. 中断

   2. 其它异常

 (任务流的优先级高于异常流,执行时整个时间轮转都会暂停。所以这个程序不能写的太占用时间。要不给用户的感觉会很不好)

应用编程可能涉及到的执行流:

1. 进程

2. 线程    

 

内核编程可能涉及到的执行流:  

1. 应用程序自身代码运行在用户空间,处于用户态   -----------------  用户态app

2. 应用程序正在调用系统调用函数,运行在内核空间,处于内核态,即代码是内核代码但处于应用执行流(即属于一个应用进程或应用线程) ----  内核态app

3. 一直运行于内核空间,处于内核态,属于内核内的任务上下文 --------- 内核线程

4. 一直运行于内核空间,处于内核态,专门用来处理各种异常 --------- 异常上下文

(当在用户APP中调用了系统函数时。在执行到这个函数时会跳转到内核执行。完了再回来继续执行。虽然使用到了内核空间。但是他还是有个执行流。)

五、模块编程与应用编程的比较

| 不同点                       |                内核模块                                     | 应用程序       |

| API来源  |               不能使用任何库函数                      |    各种库函数均可以使用             |

| 运行空间 |             内核空间                        | 用户空间                             |

| 运行权限 |              特权模式运行                                 | 非特权模式运行                       |

| 编译方式 | 静态编译进内核镜像或编译特殊的ko文件       | elf格式的应用程序可执行文件  |

| 运行方式 | 模块中的函数在需要时被动调用                 | 从main开始顺序执行                   |

| 入口函数 | init_module                                  | main                                 |

| 退出方式 |          cleanup_module                     | main函数返回或调用exit               |

| 浮点支持 | 一般不涉及浮点运算,因此printk不支持浮点数据 | 支持浮点运算,printf可以打印浮点数据 |

| 并发考虑 | 需要考虑多种执行流并发的竞态情况             | 只需考虑多任务并行的竞态     |

| 程序出错 | 可能会导致整个系统崩溃                       | 只会让自己崩溃                       |

六、内核接口头文件查询

大部分API函数包含的头文件在include/linux目录下,因此:

1. 首先在include/linux 查询指定函数:grep  名称  ./   -r   -n

2. 找不到则更大范围的include目录下查询,命令同上

 

 

七、小作业

 编写3个内核模块A、B、C,A依赖于B,B依赖于C,完成对它们的编译、运行、卸载

 

 这个写错了,最后一个应该是a

 

有点小瑕疵打印没改

 

 

#include <linux/module.h>
#include <linux/kernel.h>extern char char_a;int __init modulea_init(void)
{printk("In module_a init char = %c\n", char_a);return 0;
}
void __exit modulea_exit(void)
{printk("modulea will exit\n");
}
MODULE_LICENSE("GPL");module_init(modulea_init);
module_exit(modulea_exit);
#include <linux/module.h>
#include <linux/kernel.h>extern char char_b;
char char_a = 'a';EXPORT_SYMBOL(char_a);int __init moduleb_init(void)
{printk("In module_b init char = %c\n", char_b);return 0;
}
void __exit moduleb_exit(void)
{printk("moduleb will exit\n");
}
MODULE_LICENSE("GPL");module_init(moduleb_init);
module_exit(moduleb_exit);
#include <linux/module.h>
#include <linux/kernel.h>char char_c = 'c';
char char_b = 'b';
EXPORT_SYMBOL(char_b);int __init modulec_init(void)
{printk("In module_a init char = %c\n", char_c);return 0;
}
void __exit modulec_exit(void)
{printk("modulec will exit\n");
}
MODULE_LICENSE("GPL");module_init(modulec_init);
module_exit(modulec_exit);

http://www.ds6.com.cn/news/94829.html

相关文章:

  • 想自己做个网站怎么做磁力狗在线引擎
  • 福州网站设计软件推广普通话手抄报简单
  • 人大网站建设存在问题安卓aso
  • 提供佛山网站制作网站怎么建立
  • 寿光专业做网站镇江网络
  • 网站建设与管理收获推广软件的渠道有哪些
  • 企业网站建设策划网站seo优化多少钱
  • 微站电池网站点击软件排名
  • 德庆网站建设千网推软文推广平台
  • 电子商务 独立网站制作外贸网站大全
  • 筛网怎么做网站舆情通
  • 汕尾网站建设 生意好吗google 谷歌
  • wordpress加载优化班级优化大师简介
  • ecshop商城网站建设新手做销售怎么开发客户
  • 做网站需准备些什么软件最有效的网络推广方式
  • 我局 负责 建设 网站2021年新闻摘抄
  • 武汉建设学院在哪里国内专业的seo机构
  • 转做批发的网站厦门百度竞价推广
  • 泸西网站建设网站模板商城
  • 香港美女做旅游视频网站二级子域名ip地址查询
  • 官方网站作用如何开发网站平台
  • 用ps做班级网站河南专业网站建设
  • dedecms网站tag标签静态化上海优化公司
  • 上海哪里做网站好北京线上教学
  • wordpress搭建相册移动端关键词排名优化
  • 专题网站建设方案网站多久被百度收录
  • 制作ppt的网站windows7系统优化工具
  • 网站是广西住房和城乡建设厅新闻稿件
  • 互联网公司是干啥的seo综合查询什么意思
  • 做网站去哪里找客户小说排行榜2020前十名