系统相关
首页 > 系统相关> > linux设备驱动中的并发控制

linux设备驱动中的并发控制

作者:互联网

学习目的:学习linux设备驱动中的并发控制

编程练习:在globalmem字符设备基础上增加信号量管理共享资源

意外收获:第一次比着书上代码抄,很多东西理解不了,尤其file_operation什么时候调用,虽然现在也不懂。。。但好歹有个大致猜测理解

1- linux并发控制理解

学过ucos,很多和Linux相通。并发控制就是对共享资源的互斥访问。比如有一把手枪,你我都能想玩,但同一时间只能一个人玩,一个人被玩。。。。没错就是这个道理。

现在你在玩,我想玩,不给我,我就得等你玩腻歪了,你才可能让我玩会。。。好悲催。

嘿假如我开挂了(我是中断),你只是普通玩家(没有屏蔽终端对共享资源的访问),好家伙你玩的时候,我就要从你手里抢过枪来玩了,没办法,谁让哥哥牛逼呢。。。

当然你也可以开挂,加上屏蔽终端下半部分、终端对共享资源的访问,当你玩时我就没辙了,因为你是大爷啊。

 

浑话鬼浑话,书说正文:

linux并发控制手段:

原子操作 :必不可被打断的一种行为,很牛逼的那种

自旋锁:本质就是死等,看书的理解,底层好像是依赖原子操作啊

信号量:感觉可以替代互斥量。。。书上说好像底层依赖自旋锁。。

互斥量:资源等于1的互斥量

 

2- 上代码

代码增加很少,只是在globalmem结构体中增加sem变量;在模块初始化最后增加信号量初始化;在文件操作(ioctl、read、write)操作globalmem内存时,放到临界段访问,访问前后进行获取、释放信号量。

另外别忘了包含linux/sem.h头文件

通过增加打印,发现在echo "hello" >> /dev/globalmen,即往字符设备中写东西时,会先后调用open、read、release

在cat /dev/globalmem,即读字符设备会依次调用open、read、read、release函数。读了两次设备。。。。不知为何。。。

//6- globalmem
#include<linux/module.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/errno.h>
#include<linux/mm.h>
#include<linux/sched.h>
#include<linux/init.h>
#include<linux/cdev.h>
#include <linux/slab.h>
//#include <linux/mutex.h> //增加互斥量
#include<linux/sem.h> //增加互斥量
#include<asm/io.h>
#include<asm/switch_to.h>
//#include<asm/uaccess.h>
#include <linux/uaccess.h>

#define GLOBALMEM_SIZE 0x1000 /*操作的mem内存大小,全局变量大小4k*/
#define MEM_CLEAR 0x1         /*ioctrl命令码*/
#define GLOBALMEM_MAJOR 243 /*设定globalmem的主设备号,查看已经注册的避免冲突cat /proc/devices*/

static int globalmem_major = GLOBALMEM_MAJOR;
/*globalmem结构体*/
struct globalmem_dev
{
	struct cdev cdev; //字符设备结构体
	unsigned char mem[GLOBALMEM_SIZE]; //大小
	struct semaphore sem;
};

struct globalmem_dev *globalmem_devp;

/*以下为文件操作:打开、释放、ioctrl、读写、定位*/

/*文件打开操作*/
int globalmem_open(struct inode *inode, struct file *filp)
{
	filp->private_data = globalmem_devp;
	
	printk(KERN_INFO "funt:%s enter!\n",__FUNCTION__);
	return 0;	
}

/*文件释放*/
int globalmem_release(struct inode *inode, struct file *filp)
{
	printk(KERN_INFO "funt:%s enter!\n",__FUNCTION__);
	
	return 0;
}

/*ioctrl操作*/
static long globalmem_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)//和2.6内核不一样!!
{
	struct globalmem_dev *dev = filp->private_data;
	
	printk(KERN_INFO "funt:%s enter!\n",__FUNCTION__);
	
	switch(cmd)
	{
		case MEM_CLEAR: //增加信号量保护共享资源
		
		/*获取信号量*/
		if(down_interruptible(&dev->sem))
			return -ERESTARTSYS;
		
		/*共享资源操作*/
		memset(dev->mem,0,GLOBALMEM_SIZE);	
		
		/*释放信号量*/
		up(&dev->sem);
		
		printk(KERN_INFO "globalmem is set to zero\n");
		
		break;
		
		default:
		return -EINVAL;
	}
	
	return 0;
}

/*读函数*/
static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
	int ret =0;
	unsigned long p = *ppos;
	unsigned int count = size;
	struct globalmem_dev *dev = filp->private_data;
	
	printk(KERN_INFO "funt:%s enter!\n",__FUNCTION__);
	
	/*参数检查*/
	if(p >= GLOBALMEM_SIZE)
		return count ? -ENXIO : 0;
	if(count > GLOBALMEM_SIZE - p)
		count = GLOBALMEM_SIZE - p;
	
	/*获取信号量*/
	if(down_interruptible(&dev->sem)) 
		return -ERESTARTSYS;	
	
	/*数据拷贝在临界段*/
	if(copy_to_user(buf,(dev->mem +p),count)) //copy_to_user(buff, dev->mem + p, count)
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count; //更新位置、返回读到字节数
		ret = count; 
		printk(KERN_INFO "read %u bytes from %lu\n", count,p);
	}	
	
	/*释放信号量*/
	up(&dev->sem);
	
	return ret;	
}


/*写函数*/
static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
	int ret =0;
	unsigned long p = *ppos;
	unsigned int count = size;
	struct globalmem_dev *dev = filp->private_data;
	
	printk(KERN_INFO "funt:%s enter!\n",__FUNCTION__);
	
	/*参数检查*/
	if(p >= GLOBALMEM_SIZE)
		return count ? -ENXIO : 0;
	if(count > GLOBALMEM_SIZE - p)
		count = GLOBALMEM_SIZE - p;
	
	/*获取信号量*/
	if(down_interruptible(&dev->sem))
		return -ERESTARTSYS;
	
	/*数据拷贝在临界段*/
	if(copy_from_user((dev->mem +p),buf,count)) //copy_to_user(buff, dev->mem + p, count)
	{
		ret = -EFAULT;
	}
	else
	{
		*ppos += count; //更新位置、返回读到字节数
		ret = count; 
		printk(KERN_INFO "write %u bytes from %lu\n", count,p);
	}	
	
	/*释放信号量*/
	up(&dev->sem);	
	
	return ret;	
}

/*文件定位*/
static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
	loff_t ret = 0;
	
	switch(orig)
	{
		/*从文件开头偏移*/
		case 0: 
		if(offset < 0)
		{
			ret = -EINVAL;
			break;
		}
		if((unsigned int)offset > GLOBALMEM_SIZE)
		{
			ret = -EINVAL;
			break;
		}
		filp->f_pos = (unsigned int )offset;
		ret = filp->f_pos;
		break;
		
		/*从当前位置偏移*/
		case 1:
		if(filp->f_pos + offset < 0)
		{
			ret = -EINVAL;
			break;
		}
		if((filp->f_pos + offset) > GLOBALMEM_SIZE)
		{
			ret = -EINVAL;
			break;
		}
		filp->f_pos += (unsigned int )offset;
		ret = filp->f_pos;
		break;
		
		default:
		ret = -EINVAL;			
		break;
	}	

	return ret;	
}

/*文件操作结构体*/
static const struct file_operations globalmem_fops = 
{
	.owner = THIS_MODULE,
	.llseek = globalmem_llseek,
	.read = globalmem_read,
	.write = globalmem_write,
	.unlocked_ioctl = globalmem_ioctl,
	.open = globalmem_open,
	.release = globalmem_release,
};

/*初始化并向内核注册cdev*/
static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
	int err;
	int devno = MKDEV(globalmem_major,index);
	
	cdev_init(&dev->cdev,&globalmem_fops); //初始化cdev
	dev->cdev.ops = &globalmem_fops;
	err = cdev_add(&dev->cdev,devno,1);//向内核注册cdev
	if(err)
	{
		printk(KERN_NOTICE "Err %d adding LED%d",err,index);		
	}	
}


/*设备驱动模块加载*/
int globalmem_init(void)
{
	int result;	
	dev_t devno = MKDEV(globalmem_major,0);//如果有主设备号,先求出设备号,向系统注册
	
	if(globalmem_major)
	{
		result = register_chrdev_region(devno,1,"globalmem"); //向内核静态申请设备号,设备号devno已知
	}
	else
	{
		result = alloc_chrdev_region(&devno,0,1,"globalmem");//动态申请设备号,
		globalmem_major = MAJOR(devno);//根据申请到的设备号求出主设备号
	}	
	if(result < 0)
	{
		return result;
	}
	
	/*申请内存*/
	globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);
	if(!globalmem_devp)
	{
		result = -ENOMEM;
		goto fail_malloc;
	}
	memset(globalmem_devp,0,sizeof(struct globalmem_dev));
	
	globalmem_setup_cdev(globalmem_devp,0);//向内核注册cdev
	
	sema_init(&globalmem_devp->sem,1); /*初始化信号量*/
	
	printk(KERN_INFO "module_init succ!\n");
	
	return 0;
	
fail_malloc:unregister_chrdev_region(devno,1);//解注册申请的设备号
	return result;
	
}

/*模块卸载*/
void globalmem_exit(void)
{
	cdev_del(&globalmem_devp->cdev);//解注册cdev
	kfree(globalmem_devp);
	unregister_chrdev_region(MKDEV(globalmem_major,0), 1);
	printk(KERN_INFO "module_exit succ!\n");
}

MODULE_AUTHOR("lhk");
MODULE_LICENSE("Dual BSD/GPL");

module_param(globalmem_major, int, S_IRUGO);

module_init(globalmem_init);
module_exit(globalmem_exit);






Makefile

ifneq ($(KERNELRELEASE),)
obj-m:=globalmem.o
else
KDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	rm -rf *.ko *.o *.symvers *.cmd *.cmd.o
endif

 

流水石板路 发布了23 篇原创文章 · 获赞 5 · 访问量 5116 私信 关注

标签:count,struct,int,dev,并发,globalmem,linux,驱动,ret
来源: https://blog.csdn.net/u010743406/article/details/104197255