当前位置: 首页 > 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.zskr.cn/news/8720.html

相关文章:

  • 势能分析揭开一些算法的秘密
  • 企业省钱又安全的5款Linux发行版:从Ubuntu到Pop!_OS全面解析
  • 第六章 数组
  • 0133_解释器模式(Interpreter)
  • trick杂记 例题
  • 网络流 最小割、费用流
  • AdMergeX与小川科技最右App合作案例入选中国信通院“高质量数字化转型典型案例集” - 实践
  • 高效测试的第一步:5个用例设计基础思维模型
  • Python笔记总结
  • 8465:马走日
  • 性能调优之NUMA调优
  • 实用指南:光学神经网络与人工智能应用
  • Zabbix 企业级监控架构实战指南:从搭建、可视化到智能告警
  • U522155 数据生成(小心电脑)
  • 实用指南:OSG中osgFX库
  • 2025.9.20——1橙
  • 用 PHP 和 Tesseract OCR 识别英文数字验证码
  • 凝望深渊时,深渊也凝望着你(黑洞与摇钱树)
  • spring项目部署后为什么会生成 logback-spring.xml记录
  • 202509_NBWS_logbool
  • Kubernetes权威指南-深入理解Pod Service
  • 4980:拯救行动
  • java03-wxj
  • AI 智能体与 Coze 工作流实践:小红书对标账号采集 - 实践
  • 对比六种JavaScript全文搜索库 fuse.js 、 lunr 、 flexsearch 、 minisearch 、 search-index 、 js-sea
  • 从零开始: c#纯代码实现完整Json解析器的全过程及注释与自定义格式的支持实现
  • 大模型服务之下的新旧政务智能系统比较 - 指南
  • CentOS7.9上安装MySQL8.4
  • JBoltAI框架:企业级AI开发的革新路径与行业实践 - 那年-冬季
  • JBoltAI:重塑视频创作,开启零门槛智能混剪新时代 - 那年-冬季