#include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> #include <asm/uaccess.h> #include <asm/semaphore.h>
MODULE_LICENSE("GPL");
#define MAJOR_NUM 254
static ssize_t globalvar_read(struct file *, char *, size_t, loff_t*); static ssize_t globalvar_write(struct file *, const char *, size_t, loff_t*); static int globalvar_open(struct inode *inode, struct file *filp); static int globalvar_release(struct inode *inode, struct file *filp);
struct file_operations globalvar_fops = { read: globalvar_read, write: globalvar_write, open: globalvar_open, release: globalvar_release, };
static int global_var = 0; static int globalvar_count = 0; static struct semaphore sem; static spinlock_t spin = SPIN_LOCK_UNLOCKED;
static int __init globalvar_init(void) { int ret; ret = register_chrdev(MAJOR_NUM, "globalvar", &globalvar_fops); if (ret) { printk("globalvar register failure"); } else { printk("globalvar register success"); init_MUTEX(&sem); } return ret; }
static void __exit globalvar_exit(void) { int ret; ret = unregister_chrdev(MAJOR_NUM, "globalvar"); if (ret) { printk("globalvar unregister failure"); } else { printk("globalvar unregister success"); } }
static int globalvar_open(struct inode *inode, struct file *filp) { //获得自选锁 spin_lock(&spin);
//临界资源访问 if (globalvar_count) { spin_unlock(&spin); return - EBUSY; } globalvar_count++;
//释放自选锁 spin_unlock(&spin); return 0; }
|