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