上节回顾:Linux驱动_并发与竞争_anieoo的博客-CSDN博客
之前学习了原子操作指的是由多步操作组成的一个操作。如果该操作不能原子地执行,则要么执行完所有步骤,要么一步也不执行,不可能只执行所有步骤的一个子集。
本节实验我们利用原子变量模拟原子操作,通过此实验可以实现在一个驱动被应用占用的过程中,如果另一个驱动再次访问就会返回错误。
步骤如下:
①在驱动的入口函数中中定义一个原子变量:
atomic_set(&gpioled.lock, 1); /*初始化原子变量为1*/
②当应用打开驱动的时候执行以下函数:
if(!atomic_dec_and_test(&gpioled.lock)){
atomic_inc(&gpioled.lock);
return -EBUSY;
}
其中atomic_dec_and_test函数的意思是,将原子变量减1,并判断是否为0,如果为0则返回真。 很明显,在驱动入口函数中我们将原子变量初始化为1,此时执行atomic_dec_and_test函数会返回真,但是又于前面加了!号,因此不会执行if语句。
③此时当其他应用再次打开驱动的时候又会执上一步的函数,此时if判断为真,会执行其中的语句,即返回驱动-EBUSY.达到驱动一次只能由一个应用访问的效果。
④当应用关闭驱动的时候会执行函数:
atomic_inc(&dev->lock);
即原子变量加1,回到应用打开驱动前的状态。
完整代码:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#define GPIOLED_COUNT 1
#define GPIOLED_NAME "atomic"
#define LEDOFF 0
#define LEDON 1
/*gpio设备结构体*/
struct gpioled_dev{
dev_t devid; /*设备号*/
int major; /*主设备号*/
int minor; /*次设备号*/
struct cdev cdev;
struct class *class; /*创建类*/
struct device *device; /*创建设备*/
struct device_node *node; /*设备节点*/
int led_gpio;
atomic_t lock; /*定义原子变量*/
};
struct gpioled_dev gpioled;
static int gpioled_open(struct inode *inode, struct file *filp){
if(!atomic_dec_and_test(&gpioled.lock)){ /*原子变量减1并判断是否为0, 为0返回真, 不为0返回假*/
atomic_inc(&gpioled.lock);/* 小于 0 的话就加 1,使其原子变量等于 0 */
return -EBUSY; /* LED 被使用,返回忙 */
}
filp->private_data = &gpioled; /* 设置私有数据 */
return 0;
}
static ssize_t gpioled_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char databuf[1];
ret = copy_from_user(databuf, buf, cnt);
if(ret < 0){
return -EINVAL;
}
if(databuf[0] == LEDON){
gpio_set_value(gpioled.led_gpio, 0);
}
else if(databuf[0] == LEDOFF){
gpio_set_value(gpioled.led_gpio, 1);
}
return 0;
}
static int gpioled_release(struct inode *inode, struct file *filp){
struct gpioled_dev *dev = filp->private_data;
/* 关闭驱动文件的时候释放原子变量 */
atomic_inc(&dev->lock);
return 0;
}
/*定义字符操作集*/
static const struct file_operations gpioled_fops = {
.owner = THIS_MODULE,
.write = gpioled_write,
.open = gpioled_open,
.release = gpioled_release,
};
/*驱动入口函数*/
static int __init led_init(void){
int ret = 0;
atomic_set(&gpioled.lock, 1); /*初始化原子变量为1*/
gpioled.major = 0; /*linux内核自动申请设备号*/
/*注册字符设备驱动*/
if(gpioled.major){ /*给定主设备号*/
gpioled.devid = MKDEV(gpioled.major, 0);
ret = register_chrdev_region(gpioled.devid, GPIOLED_COUNT, GPIOLED_NAME);
}
else{ /*没给定设备号*/
ret = alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_COUNT, GPIOLED_NAME);
gpioled.major = MAJOR(gpioled.devid); /*保存主设备号*/
gpioled.minor = MINOR(gpioled.devid); /*保存次设备号*/
}
if(ret < 0){
goto failed_devid;
}
printk("gpioled major = %d ,minor = %d \r\n",gpioled.major,gpioled.minor);
/*初始化cdev*/
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev, &gpioled_fops);
/*添加cdev*/
ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_COUNT);
if(ret < 0){
goto failed_cdev;
}
/*自动创建设备节点*/
/*创建类*/
gpioled.class = class_create(THIS_MODULE, GPIOLED_NAME);
if(IS_ERR(gpioled.class)){ /*判断是否创建类成功*/
ret = PTR_ERR(gpioled.class);
goto failed_class;
}
/*创建设备*/
gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
if(IS_ERR(gpioled.device)){ /*判断是否创建类成功*/
ret = PTR_ERR(gpioled.device);
goto failed_device;
}
/*获取设备节点*/
gpioled.node = of_find_node_by_path("/gpioled");
if(gpioled.node == NULL){ /*寻找节点失败*/
ret = -EINVAL;
goto failed_findnode;
}
/*获取led所对应的gpio*/
gpioled.led_gpio = of_get_named_gpio(gpioled.node, "led-gpios", 0);
if(gpioled.led_gpio < 0){
printk("can't find led gpio \r\n");
ret = -EINVAL;
goto failed_findnode;
}
printk("led gpio num = %d \r\n",gpioled.led_gpio);
/*申请gpio*/
ret = gpio_request(gpioled.led_gpio, "led-gpios");
if(ret){
printk("Failed to request gpio \r\n");
ret = -EINVAL;
goto failed_findnode;
}
/*使用IO,申请为输出*/
ret = gpio_direction_output(gpioled.led_gpio, 1); /*设置为输出,高电平不点亮*/
if(ret < 0){
goto failed_setoutput;
}
/*输出低电平,点亮gpio*/
gpio_set_value(gpioled.led_gpio, 0);
return 0;
failed_setoutput:
gpio_free(gpioled.led_gpio);
failed_findnode:
failed_device:
class_destroy(gpioled.class);
failed_class:
cdev_del(&gpioled.cdev);
failed_cdev:
unregister_chrdev_region(gpioled.devid, GPIOLED_COUNT);
failed_devid:
return ret;
}
/*驱动出口函数*/
static void __exit led_exit(void){
/*关灯*/
/*输出高电平,关闭gpio*/
gpio_set_value(gpioled.led_gpio, 1);
/*注销字符设备驱动*/
cdev_del(&gpioled.cdev);
unregister_chrdev_region(gpioled.devid, GPIOLED_COUNT);
device_destroy(gpioled.class, gpioled.devid);
class_destroy(gpioled.class);
/*释放IO*/
gpio_free(gpioled.led_gpio);
}
/*模块入口和出口*/
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZYC");
完整应用程序
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
/*
*argc:应用程序参数个数
*argv[]文件参数名,字符串形式
*./chardevbaseApp <filename> <0:1> 0表示关灯,1表示开灯
*./chardevbaseApp /dev/led 0 关灯
*./chardevbaseApp /dev/led 1 开灯
*/
#define LEDOFF 0
#define LEDON 1
int main(int argc, int *argv[])
{
int fd, retval;
int cnt = 0;
char *filename;
unsigned char databuf[1];
if(argc != 3){
printf("Error use\r\n");
return -1;
}
/*保存设备文件名*/
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd < 0){
printf("open file failed \r\n");
return -1;
}
/*将字符转化为数字*/
databuf[0] = atoi(argv[2]);
retval = write(fd, databuf, sizeof(databuf));
if(retval < 0){
printf("led write failed\r\n");
close(fd);
return -1;
}
/* 模拟占用 25S LED */
while(1) {
printf("App running times:%d\r\n", cnt);
sleep(5);
cnt+=5;
if(cnt >= 25) break;
}
printf("App running finished!");
close(fd);
return 0;
}
在应用程序中,实现了对驱动占用25s,在此时间段内可以测试用其他app访问的效果!
实验测试
加载驱动:
模拟占用:
模拟其他应用访问 :
从图中可以发现,open failed字样,证明试验成功!!