如何将数据传递给 kthread_run
How to pass data to kthread_run
我正在尝试使用多线程制作简单的内核模块。
所以我使用 linux/kthread.h,内核 v. 5.2.11
问题:我无法将 char 数组传递给线程:分段错误。
这就是我正在做的事情:
typedef struct {
int num;
char origin[MAXSTR]; //part of input for current thread
struct completion wait_for_thread; //completion struct
} kthread_arg;
然后:
struct task_struct *task;
static kthread_arg kta_first_thread;
kta_first_thread.num = 1;
strncpy(kta_first_thread.origin, dat1, MAXSTR );
//Here I have normal char array 'origin'
init_completion(&kta_first_thread.wait_for_thread);
task = kthread_run(&thread_function, (void*)&kta_first_thread, "one");
之后我遇到了错误。此外,如果您从结构中删除数组,则一切正常。
我确定做错了什么?
传递给 kernel_run 的参数必须是 kmalloced,你的参数在堆栈中。我遇到了同样的问题,你的代码应该是这样的:
struct your_struct* test=NULL;
struct task_struct* t=NULL;
test=(struct your_struct*)kmalloc(sizeof(struct your_struct),GFP_KERNEL);
t=kthread_run(your_function,(void*)test,name);
我正在尝试使用多线程制作简单的内核模块。 所以我使用 linux/kthread.h,内核 v. 5.2.11
问题:我无法将 char 数组传递给线程:分段错误。
这就是我正在做的事情:
typedef struct {
int num;
char origin[MAXSTR]; //part of input for current thread
struct completion wait_for_thread; //completion struct
} kthread_arg;
然后:
struct task_struct *task;
static kthread_arg kta_first_thread;
kta_first_thread.num = 1;
strncpy(kta_first_thread.origin, dat1, MAXSTR );
//Here I have normal char array 'origin'
init_completion(&kta_first_thread.wait_for_thread);
task = kthread_run(&thread_function, (void*)&kta_first_thread, "one");
之后我遇到了错误。此外,如果您从结构中删除数组,则一切正常。 我确定做错了什么?
传递给 kernel_run 的参数必须是 kmalloced,你的参数在堆栈中。我遇到了同样的问题,你的代码应该是这样的:
struct your_struct* test=NULL;
struct task_struct* t=NULL;
test=(struct your_struct*)kmalloc(sizeof(struct your_struct),GFP_KERNEL);
t=kthread_run(your_function,(void*)test,name);