04lock_03spin_lock
1 #include <linux/module.h> //MODULE_LICENSE("GPL"); 2 #include <linux/init.h> //module_init module_exit 3 #include <linux/kernel.h> //printk 4 #include <linux/io.h> //ioremap iounremap 5 #include <linux/ioport.h> //request_mem_region 6 7 8 #include <linux/miscdevice.h> 9 #include <linux/fs.h> //file_operations 结构体的定义 10 #include <linux/slab.h> //kmalloc 11 #include <linux/delay.h> 12 13 #include <asm/uaccess.h> //copy_to_user copy_form_user 14 15 #include <linux/spinlock.h> //自旋锁 16 17 /* 18 该设备只能同时被一个进程打开 19 */ 20 21 22 #define DEVNAME "my_led" 23 #define MEMSIZE 100 24 25 struct ldm_info 26 { 27 struct miscdevice dev; //设备节点 28 struct file_operations ops; //文件操作 29 unsigned char mem[MEMSIZE] ; //数组, 内核的空间 30 spinlock_t mylock; //自旋锁 31 }; 32 33 //struct ldm_info ldm; 34 struct ldm_info ldm; //结构体的指针,分配空间 35 36 static int ldm_open(struct inode * inode, struct file * file) 37 { 38 printk("kernel: ldm_open\n"); 39 40 return 0; 41 } 42 43 /* 44 copy_to_user 45 copy_form_user 46 */ 47 48 static ssize_t ldm_write(struct file * file, const char __user * buf, size_t size, loff_t * offt) 49 { 50 int i =0; 51 52 spin_lock(&ldm.mylock); 53 //临界区开始 54 55 // if(!spin_trylock(&ldm.mylock)) { 56 // printk("当前资源被占用,请稍后\n"); 57 // return -1; 58 // } 59 60 for(;i < size; i++) { 61 //每拷贝一个字节,睡眠1s 62 copy_from_user(ldm.mem + i, buf + i, 1); 63 printk("write:%c\n", ldm.mem[i]); 64 ssleep(1); //延迟1s 65 } 66 spin_unlock(&ldm.mylock); 67 //临界区结束 68 69 return 0; 70 } 71 72 73 ssize_t ldm_read(struct file * file, char __user * buf, size_t size, loff_t * offt) 74 { 75 //每隔一秒拷贝一个字节到用户层 76 int i =0; 77 78 spin_lock(&ldm.mylock); 79 // if(!spin_trylock(&ldm.mylock)) { 80 // printk("当前资源被占用,请稍后\n"); 81 // return -1; 82 // } 83 84 for(;i < size; i++) { 85 //每拷贝一个字节,睡眠1s 86 copy_to_user(buf + i, ldm.mem + i, 1); 87 printk("read:%c\n", ldm.mem[i]); 88 ssleep(1); //延迟1s 89 } 90 spin_unlock(&ldm.mylock); 91 92 93 return 0; 94 } 95 96 int ldm_release(struct inode * inode, struct file *file) 97 { 98 printk("kernel: close\n"); 99 100 return 0; 101 } 102 103 static int test_init(void) 104 { 105 int ret = 0; 106 107 printk("%s:%s:%d init\n", __FILE__, __FUNCTION__, __LINE__); 108 109 110 ldm.dev.minor = MISC_DYNAMIC_MINOR; //系统自动分配次设备 111 ldm.dev.name = DEVNAME;//该名称将决定节点名称, 成功注册 linux 系统中 112 ldm.dev.fops = &ldm.ops; //关联文件操作 113 ldm.ops.open = ldm_open; 114 ldm.ops.write = ldm_write; 115 ldm.ops.read = ldm_read; 116 ldm.ops.release = ldm_release; 117 118 119 ret = misc_register(&ldm.dev); 120 121 spin_lock_init(&ldm.mylock);//初始化自旋锁 122 123 if(ret < 0) { 124 printk("misc_register failed\n"); 125 goto err_misc_register; 126 } 127 128 return 0; 129 err_misc_register: 130 return ret; 131 132 } 133 134 //卸载 135 static void test_exit(void) 136 { 137 printk("%s:%s:%d init\n", __FILE__, __FUNCTION__, __LINE__); 138 139 140 //注销misc 141 misc_deregister(&ldm.dev); 142 //释放映射的虚拟地址 143 144 } 145 146 module_init(test_init); 147 module_exit(test_exit); 148 149 150 MODULE_LICENSE("GPL"); //加入GPL许可