先贴代码: 

  1. #include <linux/init.h>  /* Needed for the macros */    
  2. #include <linux/kernel.h> 
  3. #include <linux/module.h> /* Needed for all modules */    
  4. #include <linux/fs.h> 
  5. #include <linux/cdev.h> 
  6. #include <linux/device.h> 
  7.       
  8. MODULE_LICENSE("Dual BSD/GPL");    
  9. MODULE_AUTHOR("ttxgz");    
  10.    
  11. int hello_major = 555;  
  12. int hello_minor = 0;  
  13. int number_of_devices = 1;  
  14.  
  15. int memalloc_major = 111;  
  16. int memalloc_minor = 1;  
  17.  
  18. struct cdev cdev;  
  19.     dev_t dev = 0;  
  20.  
  21. struct file_operations hello_fops = {  
  22.       .owner = THIS_MODULE 
  23.     };  
  24.  
  25. struct class *my_class;  
  26. struct class *mem_class;  
  27.  
  28. static int __init hello_init(void)    
  29. {    
  30.        int result;  
  31.        dev = MKDEV (hello_major, hello_minor);  
  32.  
  33.  
  34.  /* create your own class under /sysfs */  
  35.      my_class = class_create(THIS_MODULE, "my_class");  
  36.      if(IS_ERR(my_class))   
  37.      {  
  38.           printk("Err: failed in creating class.\n");  
  39.           return -1;   
  40.       }   
  41.       device_create( my_class, NULL, MKDEV(hello_major, hello_minor), NULL, "hello%d",0);  
  42.  
  43.       mem_class = class_create(THIS_MODULE, "mem_class");  
  44.       device_create( mem_class, NULL, MKDEV(memalloc_major, memalloc_minor), NULL, "memalloc");  
  45.  
  46.     printk(KERN_ALERT "Hello, world!/n");    
  47.     return 0;    
  48. }    
  49.  
  50. static void __exit hello_exit(void)    
  51. {    
  52.  
  53.  dev_t devno = MKDEV (hello_major, hello_minor);  
  54.     device_destroy(my_class, MKDEV(hello_major, 0));         //delete device node under /dev  
  55.        class_destroy(my_class);                               //delete class created by us  
  56.  unregister_chrdev_region (devno, number_of_devices);  
  57.  
  58.  
  59.  devno = MKDEV(memalloc_major,memalloc_minor);  
  60.     device_destroy(mem_class, MKDEV(memalloc_major, memalloc_minor));         //delete device node under /dev  
  61.        class_destroy(mem_class);                               //delete class created by us  
  62.  
  63.    printk (KERN_INFO "char driver cleaned up\n");  
  64.     printk(KERN_ALERT "Goodbye, cruel world/n");    
  65. }    
  66.    
  67. module_init(hello_init);    
  68. module_exit(hello_exit);     

makefile是这样的:

  1. ifeq ($(KERNELRELEASE),)    
  2.     
  3. # Assume the source tree is where the running kernel was built    
  4. # You should set KERNELDIR in the environment if it's elsewhere    
  5.     
  6. KERNELDIR ?= /lib/modules/$(shell uname -r)/build    
  7.     
  8. # The current directory is passed to sub-makes as argument    
  9.     
  10. PWD := $(shell pwd)    
  11.     
  12. modules:    
  13.  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules    
  14.     
  15. modules_install:    
  16.  $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install    
  17.     
  18. clean:    
  19.  rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions    
  20.     
  21. .PHONY: modules modules_install clean    
  22.     
  23. else    
  24.     
  25. # called from kernel build system: just declare what our modules are    
  26.     
  27. obj-m :hello.o    
  28.     
  29. endif    
  30.  

直接在虚拟机下挂载,可以看见在dev下出现了hello0和memalloc两个设备,printk的信息可以通过dmesg | tail查看