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

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

时间:2024-07-02 23:30:41浏览次数:3  
标签:process 08 up API wake printk include pts 源码

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);

标签:process,08,up,API,wake,printk,include,pts,源码
From: https://blog.csdn.net/H520xcodenodev/article/details/140110613

相关文章

  • [email protected](45)路由v5.x(10)源码(2)- history
    目录1,作用1.1,createBrowserHistory1.2,createHashHistory1.3,createMemoryHistory2,history对象的属性2.1,action2.2,push/replace/go/goBack/goForward2.3,location2.4,listen2.5,block/getUserConfirmation2.5,createHref1,作用react-router在控制和监听地址变......
  • java中处理字符串常用的api
    Java中String常用APIString类位于jdk中的java.lang.String包中publicintlength()获取字符串的长度(字符的个数)publiccharcharAt(intindex)获取某个索引位置的字符返回publicchar[]t......
  • WebAPI项目框架仓储模式+导入SqlSuag
    仓储(Respository)是对数据库访问的一个封装解决方案新建Respository文件夹,新建类库Web.Core.IRepository,Web.Core.Repository解决方案新建Services文件夹,新建类库Web.Core.IServices,Web.Core.Services在类库Web.Core.Model下面新建Entity文件夹SqlSugar是国人开发者开发的一......
  • 批量视频创作:PlugLink如何助力大规模视频生成(附源码)
    批量视频创作:PlugLink如何助力大规模视频生成传统的视频制作流程往往需要大量的人力、物力和时间投入,这不仅限制了内容产出的效率,也大大提高了成本。为了解决这一问题,PlugLink,一个开源的自动化框架,为我们提供了一种全新的解决方案。什么是PlugLink?PlugLink是一个旨在帮助......
  • 基于SpringBoot+Vue+uniapp的论文管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • 专题五:Spring源码之初始化容器上下文
    上一篇我们通过如下一段基础代码作为切入点,最终找到核心的处理是refresh方法,从今天开始正式进入refresh方法的解读。publicclassMain{ publicstaticvoidmain(String[]args){ ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext......
  • opencascade AIS_InteractiveContext源码学习7 debug visualization
    AIS_InteractiveContext前言交互上下文(InteractiveContext)允许您在一个或多个视图器中管理交互对象的图形行为和选择。类方法使这一操作非常透明。需要记住的是,对于已经被交互上下文识别的交互对象,必须使用上下文方法进行修改。如果交互对象尚未加载到交互上下文中,您才......
  • WebAPI项目框架JWT权限验证
    JWT是什么?校验逻辑?授权过程?这里就不过多的阐述了,直接上代码在appsettings.json中配置jwt参数的值SecretKey必须大于16个字符1{2"Logging":{3"LogLevel":{4"Default":"Information",5"Microsoft.AspNetCore":"Warni......
  • LLaMA-Factory/scripts/length_cdf.py 源码解析
    这段代码定义了一个函数 length_cdf,用来计算和打印数据集样本长度的累积分布函数(CDF),并在脚本直接运行时通过 fire 库将该函数暴露为命令行接口。我们逐行解释这段代码:python复制fromllmtuner.dataimportget_datasetfromllmtuner.hparamsimportget_train_argsfrom......
  • WebAPI项目框架新建读取配置文件帮助类
    在.netcorewebapi项目中,我们会把配置信息同意放置在appsettings.json中,通过新建读取帮助类,更加简单的读取配置信息。新建公共类库文件夹Common,新建公共类库Web.Core.Common在Web.Core.Common类库下新建Helper文件夹,新建AppSettings帮助类 .NetCore6.0WebAPI项目框架搭......