当前位置: 首页 > news >正文

Linux源码阅读笔记12-RCU案例分析

在之前的文章中我们已经了解了RCU机制的原理和Linux的内核源码,这里我们要根据RCU机制写一个demo来展示他应该如何使用。

RCU机制的原理

  • RCU(全称为Read-Copy-Update),它记录所有指向共享数据的指针的使用者,当要修改构想数据时,首先创建一个副本,并在副本中修改,所哟访问线程都离开读临界区后,使用者的指针指向修改后的副本,并且删除旧数据。

  • 他是一种在共享数据结构中实现高效读取和低延迟写入操作的技术。在Linux内核中,RCU是一种基于时间窗口的锁机制,通过充分利用多核处理器和内存系统的特性,在保证并发性的同时提供高性能。

代码示例

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>struct RCUStruct {int a;struct rcu_head rcu;
};static struct RCUStruct* Global_pointer;static struct task_struct* RCURDThread1, *RCURDThread2, *RCUWTThread;static int RCURDThreadFunc1(void* argc) {struct RCUStruct* pointer = NULL;while(1) {msleep(5);rcu_read_lock();mdelay(10);pointer = rcu_dereference(Global_pointer);if(pointer)printk("%s : read a = %d\n", __func__, pointer->a);rcu_read_unlock();}return 0;
}static int RCURDThreadFunc2(void* argc) {struct RCUStruct* pointer = NULL;while(1) {msleep(5);rcu_read_lock();mdelay(10);pointer = rcu_dereference(Global_pointer);if(pointer)printk("%s : read a = %d\n", __func__, pointer->a);rcu_read_unlock();}return 0;
}static void MyRCUDel(struct rcu_head* rcuh) {struct RCUStruct* p = container_of(rcuh, struct RCUStruct, rcu);printk("%s : a = %d\n", __func__, p->a);kfree(p);
}static int RCUWTThreadFunc(void* argc) {struct RCUStruct* old_pointer;struct RCUStruct* new_pointer;int value = (unsigned long)argc;while(1) {msleep(10);new_pointer = kmalloc(sizeof(struct RCUStruct), GFP_KERNEL);old_pointer = Global_pointer;*new_pointer = *old_pointer;new_pointer->a = value;rcu_assign_pointer(Global_pointer, new_pointer);call_rcu(&old_pointer->rcu, MyRCUDel);printk("%s : write to new %d\n", __func__, value);value++;}return 0;
}static int __init RCUFuncInit(void) {int value = 2;printk("Prompt:Successfully initialized the kernel module.\n");Global_pointer = kzalloc(sizeof(struct RCUStruct), GFP_KERNEL);RCURDThread1 = kthread_run(RCURDThreadFunc1, NULL, "RCURD1");RCURDThread2 = kthread_run(RCURDThreadFunc2, NULL, "RCURD2");RCUWTThread = kthread_run(RCUWTThreadFunc, (void*)(unsigned long)value, "RCUWT");return 0;
}static void __exit RCUFuncExit(void) {printk("Prompt:Successfully uninstalled kernel module!\n");kthread_stop(RCURDThread1);kthread_stop(RCURDThread2);kthread_stop(RCUWTThread);if(Global_pointer)kfree(Global_pointer);
}module_init(RCUFuncInit);
module_exit(RCUFuncExit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lenn louis");
  • Makefile
obj-m:=rcu.o	CURRENT_PAHT:=$(shell pwd) 
LINUX_KERNEL:=$(shell uname -r)   LINUX_KERNEL_PATH:=/usr/src/linux-headers-$(LINUX_KERNEL)
all:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PAHT) modulesclean:make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PAHT) cleals

运行结果

相关文章:

  • 『MySQL 实战 45 讲』22 - MySQL 有哪些“饮鸩止渴”提高性能的方法?
  • onTouch()与onTouchEvent()的区别
  • cpu,缓存,辅存,主存之间的关系及特点
  • Vue报错:Component name “xxx” should always be multi-word vue/multi-word-component
  • Swift 中强大的 Key Paths(键路径)机制趣谈(上)
  • 深入理解Vue生命周期钩子函数
  • 昇思25天学习打卡营第8天|DCGAN生成漫画头像
  • 三分钟看懂SMD封装与COB封装的差异
  • 存储系统的理解-磁盘、RAID和SAN
  • 普元EOS学习笔记-低开实现图书的增删改查
  • 009、MongoDB的分片策略
  • 【高中数学/基本不等式】已知:x,y皆大于1,且x+2y=4 求:1/(x-1)+1/(y-1)的最小值为?
  • 单段时间最优S型速度规划算法
  • antfu/ni 在 Windows 下的安装
  • 大模型日报 2024-07-02
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • Docker 笔记(2):Dockerfile
  • express如何解决request entity too large问题
  • Java程序员幽默爆笑锦集
  • Linux中的硬链接与软链接
  • mysql_config not found
  • Mysql数据库的条件查询语句
  • nodejs实现webservice问题总结
  • select2 取值 遍历 设置默认值
  • tensorflow学习笔记3——MNIST应用篇
  • windows下mongoDB的环境配置
  • 包装类对象
  • 服务器从安装到部署全过程(二)
  • 高度不固定时垂直居中
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 项目实战-Api的解决方案
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 主流的CSS水平和垂直居中技术大全
  • 自制字幕遮挡器
  • 湖北分布式智能数据采集方法有哪些?
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • # Kafka_深入探秘者(2):kafka 生产者
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #07【面试问题整理】嵌入式软件工程师
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (27)4.8 习题课
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (AtCoder Beginner Contest 340) -- F - S = 1 -- 题解
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (vue)页面文件上传获取:action地址
  • (八)Flask之app.route装饰器函数的参数
  • (补)B+树一些思想
  • (超详细)2-YOLOV5改进-添加SimAM注意力机制
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (九)信息融合方式简介
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (十八)SpringBoot之发送QQ邮件