本文介绍了如何在 kthread_run 中使用函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Linux中编写系统调用,为此,我想使用 kthread_run 创建多个线程.但是,当我传递函数指针时,会得到:

I am writing a system call in Linux, and for it I want to create several threads using kthread_run. However when I pass the function pointer I get:

error: passing argument 1 of ‘kthread_create_on_node’ from incompatible pointer type

以下是相关代码:

//method to do nothing for 100 milliseconds
int exist()
{
   mdelay(100);
   return 0;
}

//function pointer to exist
int (*exist_ptr)(void) = ∃

//create processes and delta queues
for (i = PROC_NUM - 1; i >= 0; i--)
{
    char name[6] = {'d', 'e', 'l', 't', 'a', i2};

    delta_entry de = {
        .task = kthread_run(exist_ptr, NULL, name);
        .list = linked_list;
        .delta_time =  PROC_NUM * MILSEC_GAP;
        .position = i2;
    }
    enqueue(&linked_list, &de, i2 - 1);
    i2++;
}

这显然不是全部代码,因为我不想把帖子写得太长.谢谢!

This obviously isn't all the code, becuase I didn't want to make the post too long. Thank you!

推荐答案

通过查看linux源代码 kthread_run 是一个调用 kthread_create_on_node 的宏,该宏期望 int(* threadfn)(无效* data).

From looking at the linux source kthread_run is a macro calling kthread_create_on_node, which expects int (*threadfn)(void *data).

int exist()接受未指定数量的参数,因此与该函数签名兼容,但是 int(* exists_ptr)(void)则不接受.( int exist() int exist(void)仅在C ++中是同义词.在C中,()表示未指定的提升参数和(void)表示没有参数.)

int exist() takes an unspecified number of arguments as is therefore compatible with that function signature but int (*exists_ptr)(void) takes none. (int exist() and int exist(void) are synonymous only in C++. In C, () means unspecified promoted arguments and (void) means no arguments.)

在现代C语言中通常不建议使用未原型的函数定义.您应该从一开始就使 exist 函数的签名 int存在(无效*未使用).

Unprototyped function definitions are generally discouraged in modern C.You should make the exist function's signature int exists(void *unused) right from the start.

这篇关于如何在 kthread_run 中使用函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:45
查看更多