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

linux kernel synchronization 1

cat /proc/cpuinfo check all processors

ps -eaF the PSR shows process is runing on processor number

preemption

user space

Under Linux, user-space programs have always been preemptible: the kernel interrupts user-space programs to switch to other threads, using the regular clock tick.

kernel space

kernel preemption has been introduced in 2.6 kernels, and one can enable or disable it using the CONFIG_PREEMPT option.
Kernel preemption can occur:
​ When returning to kernel-space from an interrupt handler
​ When kernel code becomes preemptible again
​ If a task in the kernel explicitly calls schedule()
​ If a task in the kernel blocks (which results in a call to schedule())

processor id

smp_processor_id() gives you the current processor number on which kernel is running
在多处理器系统中,Linux内核调度器会动态地将线程调度到不同的CPU上运行,导致每次调用smp_processor_id()返回不同的值

点击查看代码
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include<linux/slab.h>                 //kmalloc()
#include<linux/uaccess.h>              //copy_to/from_user()
#include <linux/kthread.h>             //kernel threads
#include <linux/sched.h>               //task_struct 
#include <linux/delay.h>static struct task_struct *my_thread;int thread_function(void *pv)
{int i=0;while(!kthread_should_stop()) {printk(KERN_INFO "Processor id:%d\t In Thread Function %d\n", smp_processor_id(), i++);msleep(1000);}return 0;
}static int __init my_driver_init(void)
{pr_info("%s: processor id:%d\n", __func__, smp_processor_id());my_thread = kthread_create(thread_function,NULL,"myThread");if(my_thread) {wake_up_process(my_thread);return 0;} else {printk(KERN_ERR "Cannot create kthread\n");return -1;}
}void __exit my_driver_exit(void)
{kthread_stop(my_thread);pr_info("%s:Device Driver Remove...Done\tProcessor Id:%d\n", __func__, smp_processor_id());
}module_init(my_driver_init);
module_exit(my_driver_exit);MODULE_LICENSE("GPL");
http://www.jsqmd.com/news/678/

相关文章:

  • 势能分析揭开一些算法的秘密
  • 企业省钱又安全的5款Linux发行版:从Ubuntu到Pop!_OS全面解析
  • 【深度学习新浪潮】数字孪生研发进展:从技术突破到代码实践 - 实践
  • how to count
  • 第六章 数组
  • basic - graph theory
  • 详细介绍:阻塞 IO为什么叫BIO,非阻塞IO为什么叫NIO,异步IO为什么叫AIO
  • Ubuntu系统使用gcc和Makefile编译C程序
  • 构造选记
  • 0133_解释器模式(Interpreter)
  • trick杂记 例题
  • 代码随想录算法训练营第四天 | leetcode 24
  • 网络流 最小割、费用流
  • DP tricks
  • 碎碎念(十七)
  • OpenCV的一些API的使用
  • AdMergeX与小川科技最右App合作案例入选中国信通院“高质量数字化转型典型案例集” - 实践
  • 在 macOS 上准备 CentOS 7.5 离线迁移文件的完整指南
  • 2971:抓住那头牛
  • 高效测试的第一步:5个用例设计基础思维模型
  • MFC Button 控件完全指南:从基础到进阶 - 指南
  • Python笔记总结
  • vulnhub靶机:GoldenEye-v1
  • 8465:马走日
  • 性能调优之NUMA调优
  • 深入解析:SpringMVC静态资源与Servlet容器指南
  • CCPC Online 2025 游寄
  • CentOS 7 容器时遇到了 yum update 报错
  • MIT新论文:数据即上限,扩散模型的关键能力来自图像统计规律,而非复杂架构
  • 实用指南:光学神经网络与人工智能应用