get_cpu_var,put_cpu_var不更新每cpu变量
在阅读了这本书之后,我试图为每个CPU变量编写一个示例代码,但是在更新其中一个计数器的每个CPU变量之后,我无法获得预期的输出get_cpu_var,put_cpu_var不更新每cpu变量,c,linux,linux-kernel,C,Linux,Linux Kernel,在阅读了这本书之后,我试图为每个CPU变量编写一个示例代码,但是在更新其中一个计数器的每个CPU变量之后,我无法获得预期的输出 #include <linux/kernel.h> #include <linux/module.h> #include <linux/delay.h> MODULE_LICENSE("GPL"); DEFINE_PER_CPU(int, counter); static int test_percpu_init(void) {
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
DEFINE_PER_CPU(int, counter);
static int test_percpu_init(void)
{
int num_cpus = num_online_cpus();
int i = 0;
int val;
pr_info("Number of cpus available:%d\n", num_cpus);
for (i = 0; i < num_cpus; i++) {
int value = per_cpu(counter, i);
pr_info("Value of counter is %d at Processor:%d\n", value, i);
}
val = get_cpu_var(counter);
val = 10;
put_cpu_var(counter);
pr_info("Printing counter value of all processor after updating current processor:%d\n",
smp_processor_id());
for (i = 0; i < num_cpus; i++) {
int value = per_cpu(counter, i);
pr_info("Value of counter is %d at Processor:%d\n", value, i);
}
return 0;
}
static void test_percpu_exit(void)
{
}
module_init(test_percpu_init);
module_exit(test_percpu_exit);
请看一下,为什么当前处理器的值没有更新。我是否在使用API或传递任何错误参数时出错
感谢您的帮助宏
get\u cpu\u var
以左值的形式返回给定cpu上的每个cpu变量,也就是说,它可以以非常类似的方式作为简单变量处理:
仍然需要调用
put\u cpu\u var
以启用内核抢占。
[14516.661529] Number of cpus available:6
[14516.661531] Value of counter is 0 at Processor:0
[14516.661532] Value of counter is 0 at Processor:1
[14516.661532] Value of counter is 0 at Processor:2
[14516.661533] Value of counter is 0 at Processor:3
[14516.661533] Value of counter is 0 at Processor:4
[14516.661534] Value of counter is 0 at Processor:5
[14516.661534] Printing counter value of all processor after updating current processor:5
[14516.661534] Value of counter is 0 at Processor:0
[14516.661535] Value of counter is 0 at Processor:1
[14516.661535] Value of counter is 0 at Processor:2
[14516.661536] Value of counter is 0 at Processor:3
[14516.661536] Value of counter is 0 at Processor:4
[14516.661536] Value of counter is 0 at Processor:5
// read per-CPU variable counter
int value = get_cpu_var(counter);
// write per-CPU variable counter
get_cpu_var(counter) = 10;
// get a pointer to per CPU variable
int* p_counter = &get_cpu_var(counter);
// read per-CPU variable via a pointer
int value = *p_counter;
// write per-CPU variable via a pointer
*p_counter = 10;