Writing simple Linux kernel modules

Source: Internet
Author: User

The module code is as follows
//main.c#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/sched.h>#include <linux/list.h>static int my_hello(void){    struct task_struct *task = NULL;        printk("Hello\n");        for_each_process(task){//打印系统中的所有进程信息        printk("PID:%d \t NAME:%s \t STATE:%ld\n",task->pid,task->comm,task->state);    }           return 0;}static void my_exit(void){    printk("exit\n");}module_init(my_hello);//指定模块加载时执行的操作( 类似C++中的构造函数)module_exit(my_exit);//指定模块退出时执行的操作( 类似C++中的析构函数)MODULE_LICENSE("GPL");
Makefile Writing
obj-m+=main.o #指定生成的模块all:    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modulesclean:    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
其中:make 中的 -c 选项表示切换到指定目录,这里是切换到当前内核的源码目录下. M 选项表示:要生成模块的代码目录,modules表示生成内核模块整个语句的理解大概可以为: 切换到内核源码目录(因为模块需要用到一些内核头文件),结合自己模块代码生成对应模块

Writing simple Linux kernel modules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.