如何将数据传递到kthread_run

问题描述 投票:0回答:1

我试图通过多线程制作简单的内核模块。所以我正在使用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");

然后我出现了错误。而且,如果您从struct中删除数组,那么一切都会正常。我确定做错了什么?

c multithreading linux-kernel kernel-module
1个回答
0
投票

传递给kernel_run的args必须进行kmalloc,您的args在堆栈中。我遇到了同样的问题,您的代码应如下所示:

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);
© www.soinside.com 2019 - 2024. All rights reserved.