解决竞态的途径是“保证对共享资源的互斥访问”
1.直接列出内核中提供的宏定义
变量:atomic_t 整型变量
宏定义:atomic_read
(*(volatile int *)&(v)->counter)
volatile关键词。表示变量每次被访问,执行单元会从内存单元中取值
不带关键词。表示变量在编译的时候可能被"优化"。
保证对特殊地址的稳定访问!
宏定义:atomic_inc atomic_add(1, (v)) 变量加1
宏定义:atomic_dec 变量减1
宏定义:ATOMIC_INIT 赋值
2.如何使用
假设任务单元A申请“共享单元”:
则先读变量,如果0,则对变量加1,如果1,则直接返回
然后对共享资源资源进行操作,操作完毕之后则对变量赋值
假设任务单元B要申请“共享单元”:
则先读变量,如果0,则对变量加1,如果1,则直接返回
1.直接列出内核中提供的宏定义
变量:atomic_t 整型变量
宏定义:atomic_read
(*(volatile int *)&(v)->counter)
volatile关键词。表示变量每次被访问,执行单元会从内存单元中取值
不带关键词。表示变量在编译的时候可能被"优化"。
保证对特殊地址的稳定访问!
宏定义:atomic_inc atomic_add(1, (v)) 变量加1
宏定义:atomic_dec 变量减1
宏定义:ATOMIC_INIT 赋值
2.如何使用
假设任务单元A申请“共享单元”:
则先读变量,如果0,则对变量加1,如果1,则直接返回
然后对共享资源资源进行操作,操作完毕之后则对变量赋值
假设任务单元B要申请“共享单元”:
则先读变量,如果0,则对变量加1,如果1,则直接返回
然后对共享资源资源进行操作,操作完毕之后则对变量赋值\
3、函数
static atomic_t value_atomic = ATOMIC_INIT(0);//定义原子变量,并初始化为0
#define ATOMIC_INIT(i) ( (atomic_t) { (i) } )
#define atomic_read(v) ((v)->counter)
#define atomic_set(v,i) ((v)->counter = (i))
#define atomic_inc_and_test(v) (atomic_inc_return(v) == 0)
#define atomic_sub_and_test(i,v) (atomic_sub_return((i), (v)) == 0)
#define atomic_dec_and_test(v) (atomic_sub_return(1, (v)) == 0)
#define atomic_inc(v) atomic_add(1,(v))
#define atomic_dec(v) atomic_sub(1,(v))
4、实例
驱动程序
#include <linux/init.h>
#include <linux/module.h>
/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>
/*注册杂项设备头文件*/
#include <linux/miscdevice.h>
/*注册设备节点的文件结构体*/
#include <linux/fs.h>
//原子操作的函数头文件
#include <asm/atomic.h>
#include <asm/types.h>
#define DRIVER_NAME "atomic_int"
#define DEVICE_NAME "atomic_int"
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");
//定义原子变量,并初始化为0
static atomic_t value_atomic = ATOMIC_INIT(0);
static int atomic_int_open(struct inode *inode, struct file *file){
printk(KERN_EMERG "atomic_int open in!\n");
if(atomic_read(&value_atomic)){
return -EBUSY;
}
atomic_inc(&value_atomic);
printk(KERN_EMERG "atomic_int open success!\n");
return 0;
}
static int atomic_int_release(struct inode *inode, struct file *file){
printk(KERN_EMERG "atomic_int release\n");
atomic_dec(&value_atomic);
return 0;
}
static struct file_operations atomic_int_ops = {
.owner = THIS_MODULE,
.open = atomic_int_open,
.release = atomic_int_release,
};
static struct miscdevice atomic_int_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &atomic_int_ops,
};
static int atomic_int_probe(struct platform_device *pdv){
printk(KERN_EMERG "\tinitialized\n");
misc_register(&atomic_int_dev);
return 0;
}
static int atomic_int_remove(struct platform_device *pdv){
printk(KERN_EMERG "\tremove\n");
misc_deregister(&atomic_int_dev);
return 0;
}
struct platform_driver atomic_int_driver = {
.probe = atomic_int_probe,
.remove = atomic_int_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
}
};
static int atomic_int_init(void)
{
int DriverState;
printk(KERN_EMERG "HELLO WORLD enter!\n");
DriverState = platform_driver_register(&atomic_int_driver);
printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
return 0;
}
static void atomic_int_exit(void)
{
printk(KERN_EMERG "HELLO WORLD exit!\n");
platform_driver_unregister(&atomic_int_driver);
}
module_init(atomic_int_init);
module_exit(atomic_int_exit);
测试程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc,char **argv){
char *atomic_int = "/dev/atomic_int";
int fd;
if((fd = open(atomic_int,O_RDWR|O_NDELAY))<0){
printf("%s open %s failed!\n",argv[0],atomic_int);
}
else{
printf("%s open %s sucess!\n",argv[0],atomic_int);
}
while(1);
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc,char **argv){
char *atomic_int = "/dev/atomic_int";
int fd;
if((fd = open(atomic_int,O_RDWR|O_NDELAY))<0){
printf("%s open %s failed!\n",argv[0],atomic_int);
}
else{
printf("%s open %s sucess!\n",argv[0],atomic_int);
}
while(1);
}