文章目录
1.内核中的竞态
1.1内核中竞态产生的原因
竞态产生的原因:如果在用户空间同时有多个进程需要访问同一个硬件,此时竞态就产生了。
(如果有多个进程同时访问临界资源的时候,竞态就会产生了)
1.2竞态产生的根本原因
1.对于单核cpu来说,如果内核支持抢占,就会产生竞态。
2.多核cpu来说,核与核之间本身就会产生竞态
3.中断和进程之间也会产生竞态
错误的说法:在arm架构下,中断和中断间也会产生竞态
1.3解决竞态的方法
(1)中断屏蔽(了解)
(2)自旋锁(重点掌握)
(3)信号量(重点掌握)
(4)互斥体(会用)
(5)原子操作(会用)
2.中断屏蔽(了解)
中断屏蔽:就是将中断临时关闭,中断屏蔽只针对单核处理器有效。
中断屏蔽的时间有尽可能的短,如果中断屏蔽的时间比较长,就会导致
内核的崩溃或者用户数据的丢失。
local_irq_disable(); //关闭中断
//临界区
local_irq_enable(); //开启中断
3自旋锁(重点掌握)
3.1什么是自旋锁
自旋锁:当一个进程获取到自旋锁的时候,另外一个进程也想获取这把锁,此时后一个进程
就处于自旋锁状态(忙等锁)。
3.2自旋锁的特点
1.自旋状态是需要消耗cpu资源的
2.自旋锁保护的临界区比较小,不能够在临界区中使用延时,耗时,甚至休眠的函数。
并且在临界区中不能够使用copy_to_user、copy_from_user的函数。
3.自旋锁可能会产生死锁
4.自旋锁可以用于中断上下文
5.自旋锁在上锁前会关闭抢占
6.自旋锁本身就是为多核心设计的
3.3自旋锁的API
1.定义自旋锁
spinlock_t lock;
2.初始化自旋锁
spin_lock_init(&lock);
3.上锁
spin_lock(&lock);
4.解锁
spin_unlock(&lock)
3.4实例
myled.c
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "myled"
#define COUNT 3
struct cdev* cdev;
int major = 238;
int minor = 0;
char kbuf[128] = { 0 };
struct class* cls;
struct device* dev;
//定义标志位
int flags=0;
//定义自旋锁
spinlock_t lock;
int myled_open(struct inode* inode, struct file* filp)
{
spin_lock(&lock);
if(flags != 0){
spin_unlock(&lock);
return -EBUSY;
}
flags = 1;
spin_unlock(&lock);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file* filp,
char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error\n");
return -EINVAL;
}
return size;
}
ssize_t myled_write(struct file* filp,
const char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error\n");
return -EINVAL;
}
return size;
}
int myled_close(struct inode* inode, struct file* filp)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// spin_lock(&lock);
flags=0;
// spin_unlock(&lock);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
static int __init myled_init(void)
{
int ret, i;
dev_t devno;
// 1.分配对象
cdev = cdev_alloc();
if (cdev == NULL) {
printk("alloc cdev memory error\n");
ret = -ENOMEM;
goto ERR1;
}
// 2.对象初始化
cdev_init(cdev, &fops);
// 3.设备号的申请
if (major == 0) {
//动态申请
ret = alloc_chrdev_region(&devno, minor, COUNT, CNAME);
if (ret) {
printk("dynamic:request device number error\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
} else {
//静态指定
ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
if (ret) {
printk("static:request device number error\n");
goto ERR2;
}
}
// 4.字符设备驱动的注册
ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
if (ret) {
printk("register chardev error\n");
goto ERR3;
}
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls)) {
printk("class create error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
for (i = minor; i < minor + 3; i++) {
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev)) {
printk("device create error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
//初始化自旋锁
spin_lock_init(&lock);
return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
for (--i; i >= minor; i--) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i;
//注销设备节点
for (i = minor; i < minor + 3; i++) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//字符设备驱动的注销
cdev_del(cdev);
//设备号释放
unregister_chrdev_region(MKDEV(major, minor), COUNT);
//释放cdev的内存空间
kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
test.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
char buf[128] = { 0 };
int main(int argc, const char* argv[])
{
int fd;
if ((fd = open("/dev/myled0", O_RDWR)) < 0) {
perror("open error");
exit(EXIT_FAILURE);
}
write(fd, buf, sizeof(buf));
sleep(10);
read(fd, buf, sizeof(buf));
close(fd);
return 0;
}
4信号量(重点掌握)
4.1信号量的原理
信号量:当一个进程获取到信号量的时候,另外一个进程也想获取这个信号,后一个进程处于
休眠状态
4.2信号量的特点
1.休眠的状态不需要消耗cpu资源
2.信号量不会产生死锁
3.信号量保护的临界区可以很大,里面可以有延时,耗时,甚至休眠的操作。
4.信号量工作在进程上下文
5.信号量不会关闭抢占
4.3信号量的API接口
1.定义信号量
struct semaphore sem;
2.信号量的初始化
void sema_init(struct semaphore *sem, int val)
//val初始化为1才具备互斥的效果
3.获取信号量
void down(struct semaphore *sem)
int down_trylock(struct semaphore *sem)
//获取信号量成功返回0,如果失败返回1(不会阻塞)
4.释放信号量
void up(struct semaphore *sem)
4.4信号量的使用的实例
myled.c
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "myled"
#define COUNT 3
struct cdev* cdev;
int major = 238;
int minor = 0;
char kbuf[128] = { 0 };
struct class* cls;
struct device* dev;
//定义信号量
struct semaphore sem;
int myled_open(struct inode* inode, struct file* filp)
{
//down(&sem);
if(down_trylock(&sem)){
return -EBUSY;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file* filp,
char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error\n");
return -EINVAL;
}
return size;
}
ssize_t myled_write(struct file* filp,
const char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error\n");
return -EINVAL;
}
return size;
}
int myled_close(struct inode* inode, struct file* filp)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
up(&sem);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
static int __init myled_init(void)
{
int ret, i;
dev_t devno;
// 1.分配对象
cdev = cdev_alloc();
if (cdev == NULL) {
printk("alloc cdev memory error\n");
ret = -ENOMEM;
goto ERR1;
}
// 2.对象初始化
cdev_init(cdev, &fops);
// 3.设备号的申请
if (major == 0) {
//动态申请
ret = alloc_chrdev_region(&devno, minor, COUNT, CNAME);
if (ret) {
printk("dynamic:request device number error\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
} else {
//静态指定
ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
if (ret) {
printk("static:request device number error\n");
goto ERR2;
}
}
// 4.字符设备驱动的注册
ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
if (ret) {
printk("register chardev error\n");
goto ERR3;
}
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls)) {
printk("class create error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
for (i = minor; i < minor + 3; i++) {
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev)) {
printk("device create error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
//初始化信号量
sema_init(&sem,1);
return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
for (--i; i >= minor; i--) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i;
//注销设备节点
for (i = minor; i < minor + 3; i++) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//字符设备驱动的注销
cdev_del(cdev);
//设备号释放
unregister_chrdev_region(MKDEV(major, minor), COUNT);
//释放cdev的内存空间
kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
test.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
char buf[128] = { 0 };
int main(int argc, const char* argv[])
{
int fd;
if ((fd = open("/dev/myled0", O_RDWR)) < 0) {
perror("open error");
exit(EXIT_FAILURE);
}
write(fd, buf, sizeof(buf));
sleep(10);
read(fd, buf, sizeof(buf));
close(fd);
return 0;
}
5互斥体
5.1互斥体的原理
互斥体:当一个进程获取到互斥体的时候,另外一个进程也想获取这个互斥体,后一个进程处于
休眠状态
5.2互斥体的特点
1.休眠的状态不需要消耗cpu资源
2.互斥体不会产生死锁
3.互斥体保护的临界区可以很大,里面可以有延时,耗时,甚至休眠的操作。
4.互斥体工作在进程上下文
5.互斥体不会关闭抢占
6.在不确定临界区大小的时候,优先选择互斥体。因为互斥体在进入休眠前为稍微等一会儿。
如果临界区比较小,那么此时使用互斥体就进程就不需要发生状态的切换,效率比较高。
5.3互斥体的API
1.定义互斥体
struct mutex lock;
2.互斥体的初始化
mutex_init(&lock);
3.上锁
mutex_lock(&lock);
int mutex_trylock(struct mutex *lock)
//尝试上锁,不会阻塞,如果获取锁成功返回1,失败返回0
4.解锁
mutex_unlock(&lock);
5.4互斥体的实例
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "myled"
#define COUNT 3
struct cdev* cdev;
int major = 238;
int minor = 0;
char kbuf[128] = { 0 };
struct class* cls;
struct device* dev;
//定义互斥体
struct mutex lock;
int myled_open(struct inode* inode, struct file* filp)
{
if(!mutex_trylock(&lock)){
return -EBUSY;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file* filp,
char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error\n");
return -EINVAL;
}
return size;
}
ssize_t myled_write(struct file* filp,
const char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error\n");
return -EINVAL;
}
return size;
}
int myled_close(struct inode* inode, struct file* filp)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
mutex_unlock(&lock);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
static int __init myled_init(void)
{
int ret, i;
dev_t devno;
// 1.分配对象
cdev = cdev_alloc();
if (cdev == NULL) {
printk("alloc cdev memory error\n");
ret = -ENOMEM;
goto ERR1;
}
// 2.对象初始化
cdev_init(cdev, &fops);
// 3.设备号的申请
if (major == 0) {
//动态申请
ret = alloc_chrdev_region(&devno, minor, COUNT, CNAME);
if (ret) {
printk("dynamic:request device number error\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
} else {
//静态指定
ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
if (ret) {
printk("static:request device number error\n");
goto ERR2;
}
}
// 4.字符设备驱动的注册
ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
if (ret) {
printk("register chardev error\n");
goto ERR3;
}
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls)) {
printk("class create error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
for (i = minor; i < minor + 3; i++) {
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev)) {
printk("device create error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
//初始化
mutex_init(&lock);
return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
for (--i; i >= minor; i--) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i;
//注销设备节点
for (i = minor; i < minor + 3; i++) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//字符设备驱动的注销
cdev_del(cdev);
//设备号释放
unregister_chrdev_region(MKDEV(major, minor), COUNT);
//释放cdev的内存空间
kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
6原子操作
6.1原子操作的原理
typedef struct {
int counter;
} atomic_t;
原子操作:原子操作的含义就是对这个原子变量值的修改是一个不可被分割的整体。原子类型内部就是
一个整型的变量。但是内核对这个变量的修改过程是通过内联汇编完成。对变量值的修改是直接放在寄
存器中完成的。在对原子变量修改的时候内核只会往一个核执行。
6.2原子操作的API接口
1.定义原子变量及初始化
atomic_t lock = ATOMIC_INIT(1);
atomic_t lock = ATOMIC_INIT(-1);
2.上锁
atomic_dec_and_test(&lock)
减1之后和0比较,如果结果为0,表示获取锁成功了
atomic_inc_and_test(&lock)
加1之后和0比较,如果结果为0,表示获取锁成功了
3.解锁
atomic_inc(&lock);
atomic_dec(&lock);
6.3原子操作使用实例
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "myled"
#define COUNT 3
struct cdev* cdev;
int major = 238;
int minor = 0;
char kbuf[128] = { 0 };
struct class* cls;
struct device* dev;
//定义并初始化原子操作
atomic_t atm = ATOMIC_INIT(1);
int myled_open(struct inode* inode, struct file* filp)
{
//减去1之后和0比较,如果结果为0,表示获取锁成功,获取锁
//成功返回真。
if(!atomic_dec_and_test(&atm)){
//失败会进入这个if语句,当进来的时候这个数据就从0减到了-1
//在退出open函数前,需要将它减去的数值在加回来
atomic_inc(&atm);
return -EBUSY;
}
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file* filp,
char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_to_user(ubuf, kbuf, size);
if (ret) {
printk("copy data to user error\n");
return -EINVAL;
}
return size;
}
ssize_t myled_write(struct file* filp,
const char __user* ubuf, size_t size, loff_t* offs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret) {
printk("copy data from user error\n");
return -EINVAL;
}
return size;
}
int myled_close(struct inode* inode, struct file* filp)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
atomic_inc(&atm);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.read = myled_read,
.write = myled_write,
.release = myled_close,
};
static int __init myled_init(void)
{
int ret, i;
dev_t devno;
// 1.分配对象
cdev = cdev_alloc();
if (cdev == NULL) {
printk("alloc cdev memory error\n");
ret = -ENOMEM;
goto ERR1;
}
// 2.对象初始化
cdev_init(cdev, &fops);
// 3.设备号的申请
if (major == 0) {
//动态申请
ret = alloc_chrdev_region(&devno, minor, COUNT, CNAME);
if (ret) {
printk("dynamic:request device number error\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
} else {
//静态指定
ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
if (ret) {
printk("static:request device number error\n");
goto ERR2;
}
}
// 4.字符设备驱动的注册
ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
if (ret) {
printk("register chardev error\n");
goto ERR3;
}
// 5.自动创建设备节点
cls = class_create(THIS_MODULE, CNAME);
if (IS_ERR(cls)) {
printk("class create error\n");
ret = PTR_ERR(cls);
goto ERR4;
}
for (i = minor; i < minor + 3; i++) {
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
if (IS_ERR(dev)) {
printk("device create error\n");
ret = PTR_ERR(dev);
goto ERR5;
}
}
return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
for (--i; i >= minor; i--) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i;
//注销设备节点
for (i = minor; i < minor + 3; i++) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//字符设备驱动的注销
cdev_del(cdev);
//设备号释放
unregister_chrdev_region(MKDEV(major, minor), COUNT);
//释放cdev的内存空间
kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
v = device_create(cls, NULL, MKDEV(major, i), NULL, “myled%d”, i);
if (IS_ERR(dev)) {
printk(“device create error\n”);
ret = PTR_ERR(dev);
goto ERR5;
}
}
return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
for (–i; i >= minor; i–) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit myled_exit(void)
{
int i;
//注销设备节点
for (i = minor; i < minor + 3; i++) {
device_destroy(cls, MKDEV(major, i));
}
class_destroy(cls);
//字符设备驱动的注销
cdev_del(cdev);
//设备号释放
unregister_chrdev_region(MKDEV(major, minor), COUNT);
//释放cdev的内存空间
kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE(“GPL”);