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

Linux源码阅读笔记08-进程调度API系统调用案例分析

kthread_create_on_node

kthread_create_on_node函数功能:指定存储节点创建新内核线程。源码如下:

操作实战

#include <linux/module.h>
#include <linux/pid.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/wait.h>int MyThreadFunc(void* argc) {printk("MyThreadFunc\n");printk("MyThreadFuncPID: %d\n", current->pid);printk("Exit MyThreadFunc\n");return 0;
}static int __init KthreadCreateOnNodeInit(void) {struct task_struct* pts = NULL;printk("KthreadCreateOnNodeInit\n");pts = kthread_create_on_node(MyThreadFunc, NULL, -1, "ktconode.c");printk("New thread PID: %d\n", pts->pid);wake_up_process(pts);printk("Curretn thread PID: %d\n", current->pid);return 0;
}static void __exit KthreadCreateOnNodeExit(void) {printk("Exit kernel: KthreadCreateOnNodeExit\n");
}MODULE_LICENSE("GPL");
module_init(KthreadCreateOnNodeInit);
module_exit(KthreadCreateOnNodeExit);

wake_up_process

wake_up_process函数功能:唤醒处于睡眠状态的进程,状态转换为RUNNING状态,让CPU重新调度处理。

  • 唤醒成功返回1
  • 唤醒失败(该线程已经是RUNNING状态)返回0

源代码如下:

操作实战

#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/module.h>
#include <linux/pid.h>
#include <linux/list.h>
#include <linux/delay.h>struct task_struct* pts_thread = NULL;int MyThreadFunc(void* argc) {int iData = -1;printk("MyThreadFunc\n");printk("MyThreadFunc PID: %d\n", current->pid);//查看父进程状态printk("初始化函数状态为: %ld\n", pts_thread->state);iData = wake_up_process(pts_thread);printk("wake_up_process之后父进程状态: %ld\n", pts_thread->state);printk("wake_up_process返回结果为: %d\n", iData);printk("Exit MyThreadFunc\n");return 0;
}static int __init WakeUpProcessInit(void) {int res = 1; // 保存wake_up_process结果char cName[] = "wakeup.c%s";struct task_struct* pResult = NULL;long time_out;wait_queue_head_t head;wait_queue_entry_t data;printk("WakeUpProcessInit\n");//指定节点,创建新的内核线程pResult = kthread_create_on_node(MyThreadFunc, NULL, -1, cName);printk("New thread PID: %d\n", pResult->pid);printk("Current thread PID: %d\n", current->pid);init_waitqueue_head(&head);init_waitqueue_entry(&data, current);add_wait_queue(&head, &data);pts_thread = current;res = wake_up_process(pResult);printk("wake_up_process唤醒新线程之后的结果为: %d\n", res);time_out = schedule_timeout_uninterruptible(2000*10);res = wake_up_process(current);printk("唤醒当前线程的结果为: %d\n", res);printk("调用sched_timeout_uninterruptible返回结果为: %ld\n", time_out);printk("Exit WakeUpProcessInit\n");return 0;
}static void __exit WakeUpProcessExit(void) {printk("Exit WakeUpProcessExit\n");
}module_init(WakeUpProcessInit);
module_exit(WakeUpProcessExit);

http://www.lryc.cn/news/388818.html

相关文章:

  • 短视频抓取:成都柏煜文化传媒有限公司
  • proto的前后端使用
  • 华为解决固态硬盘致命弱点:延长30~50%的SSD寿命
  • 登录验证码高扩展性设计方案
  • Spring MVC数据绑定和响应——数据回写(一)普通字符串的回写
  • 怎样才能更好地保护个人账号的安全
  • react native优质开源项目
  • 速盾:海外cdn有哪些优缺点呢?
  • Unity Shader 软粒子
  • nextTick的应用和原理理解
  • .Net Core 微服务之Consul
  • 速盾:cdn流量调度
  • Windows批处理入门:快速掌握批处理脚本的基本技巧
  • 【C++之unordered_set和unordered_map的模拟实现】
  • 服务器使用别人的conda
  • 农村程序员陈随易2024年中总结
  • Spring Boot中的日志管理最佳实践
  • python基础语法 004-2流程控制- for遍历
  • 【高考志愿】医学
  • 音视频开发31 FFmpeg 编码- avcodec_find_encoder和avcodec_find_encoder_by_name
  • 大模型压缩:基于贝叶斯优化的自适应低秩分解
  • 【Python函数编程实战】:从基础到进阶,打造代码复用利器
  • ZooKeeper 应用场景深度解析
  • 动手学深度学习(Pytorch版)代码实践 -计算机视觉-41目标检测数据集
  • 2.2章节python的变量和常量
  • 豆包文科成绩超了一本线,为什么理科不行?
  • Java多线程编程实践中的常见问题与解决方案
  • WebStorm配置路径别名(jsconfig.json)
  • [吃瓜教程]南瓜书第4章决策树
  • Redis 面试题完整指南:深度解析基础、进阶与高级功能