简体   繁体   English

Kthread:将结构传递给函数

[英]Kthread: Passing struct to function

I'm trying to implement a kthread that needs to work with a thread. 我正在尝试实现需要与线程一起使用的kthread。

I try to pass the struct like this which causes an error at compile time: 我尝试通过这样的结构,这会在编译时导致错误:

    int thread_function(void* data)
{
    killStruct obj = (killStruct) data;
    //msleep((unsigned int) time);
    printk(KERN_INFO"From the inner Thread: PID: %d, Time:%ld\n",obj->pid,obj->millisecondsToKill);
    //kill prozess by ID
    return 0;
}

static void prozess_kill(struct killStruct obj)
{
    task = kthread_run(&thread_function,(void*)obj ,obj->pid);
}

What is the correct way to pass a struct to such an function pointer 将结构传递给此类函数指针的正确方法是什么

You are casting the whole struct to a void pointer, which will not work. 您正在将整个结构转换为void指针,这将无法正常工作。 You have to have pointer pointing to a valid struct and then pass that pointer, instead of a whole struct. 您必须有一个指向有效结构的指针,然后传递该指针,而不是整个结构。

{
    struct killStruct* o = initializethestruct(); //malloc, set members

    prozess_kill( o ) ;
}

static void prozess_kill(struct killStruct* obj)
{
    task = kthread_run(&thread_function,(void*)obj ,obj->pid);
}


int thread_function(void* data)
{
    struct killStruct* obj = (struct killStruct*) data;
   ....

You also used the struct name in two different ways, it should be always struct killStruct or if it is a typedef then always killStruct . 您还以两种不同的方式使用了struct名称,它应该始终为struct killStruct如果它是typedef,则始终为killStruct

将数据指针强制转换为KillStruct *类型,然后取消引用以获得Killstruct对象。

 killStruct obj = *(killStruct *)data;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM