zoukankan      html  css  js  c++  java
  • linux内核编程helloworld(中级)

    传入参数

    [root@bogon modules]# cat first.c
    #include<linux/kernel.h>
    #include<linux/stat.h>
    #include<linux/moduleparam.h>
    #include<linux/init.h>
    #include<linux/module.h>
    static short int a=1;
    static int b=2;
    static long int c=3;
    static char *d="bp";
    static int myintArray[2]={-1,-1};
    static int arr_argc=0;
    
    module_param(a,short,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
    MODULE_PARM_DESC(a,"a short integer");
    module_param(b,int ,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    MODULE_PARM_DESC(b,"an integer");
    module_param(c,long,S_IRUSR);
    MODULE_PARM_DESC(c,"a long integer");
    module_param(d,charp,0000);
    MODULE_PARM_DESC(d,"a char string");
    module_param_array(myintArray,int,&arr_argc,0000);
    MODULE_PARM_DESC(myintArray,"an array of integers");
    static int __initdata hellodata=3;
    static int __init bp_init(void){
        int i;
        printk(KERN_ALERT "hello world
    ");
        printk(KERN_ALERT "a is a short integer:%d
    ",a);
        printk(KERN_ALERT "b is a integer:%d
    ",b);
        printk(KERN_ALERT "c is a long integer:%d
    ",c);
        printk(KERN_ALERT "d is a string:%s
    ",d);
        for(i=0;i<(sizeof(myintArray)/sizeof(int));i++)
            printk(KERN_ALERT "myintArray[%d] is %d
    ",i,myintArray[i]);
        printk(KERN_ALERT   "
    hi,this is bp %d 
    ",hellodata);
        return 0;
    }
    static void __exit bp_exit(void){
        printk(KERN_ALERT   "
    goobye bp
    ");
    }
    module_init(bp_init);
    module_exit(bp_exit);
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("by bp");
    MODULE_DESCRIPTION("this is test of bp");
    MODULE_SUPPORTED_DEVICE("testdevice");
    [root@bogon modules]# cat Makefile 
    obj-m=first.o
    default:
        make -C /usr/src/kernels/`uname -r` M=$(PWD) modules
    clean:
        make -C /usr/src/kernels/`uname -r` M=$(PWD) clean
    [root@bogon modules]# make
    make -C /usr/src/kernels/`uname -r` M=/root/modules modules
    make[1]: Entering directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
      Building modules, stage 2.
      MODPOST 1 modules
    make[1]: Leaving directory `/usr/src/kernels/3.10.0-514.el7.x86_64'
    [root@bogon modules]# insmod first.ko a=100
    [root@bogon modules]# rmmod first
    [root@bogon modules]# 
    

    运行效果
    这里写图片描述

  • 相关阅读:
    css3
    css3
    npm 安装包无法继续下载? 卡住
    tcp/ip协议中的SYN, ACK的数值变化
    【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
    ES6 中 Symbol.split的用法
    Why does Typescript use the keyword “export” to make classes and interfaces public?
    es6中的import,export浏览器已经支持
    Understanding the JavaScript Engine—— two phase
    【转】js-ES6学习笔记-Symbol
  • 原文地址:https://www.cnblogs.com/biaopei/p/7730596.html
Copyright © 2011-2022 走看看