在内核模块中命名变量“ current”会导致“函数声明不是原型”。错误 [英] Naming a variable `current` in a kernel module leads to "function declaration isn’t a prototype" error
问题描述
作为一个初学者,我正在学习为Linux编写内核模块。我要做的是使用DFS算法将每个任务及其子进程写入内核日志。但是,当我使用 Makefile
编译代码时,它显示了以上错误:
I'm learning to write kernel modules for linux as a beginner. What I'm trying to do is to write every task and its child process into the kernel log using DFS algorithm. But when I compile the code using Makefile
, it shows the above error:
function declaration isn’t a prototype [-Werror=strict-prototypes]
struct task_struct *current;
它指出了 task_struct
关键字功能DFS。
这是我的代码:
It points out the task_struct
keyword at the function DFS.
Here's my code:
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
# include <linux/sched.h>
# include <linux/list.h>
void DFS (struct task_struct *task)
{
struct task_struct *current;
struct list_head *list;
list_for_each (list, &task->children)
{
current = list_entry(list, struct task_struct, sibling);
printk(KERN_INFO "%d\t%d\t%s \n", (int)current->state, current->pid, current->comm);
if (current != NULL)
{
DFS(current);
}
}
}
int DFS_init(void)
{
printk(KERN_INFO "Loading the Second Module...\n");
printk(KERN_INFO "State\tPID\tName\n");
DFS(&init_task);
return 0;
}
void DFS_exit(void)
{
printk(KERN_INFO "Removing the Second Module...\n");
}
任何人都知道如何解决此问题??
Anyone knows how to fix this ??
推荐答案
内核有一个名为 current
的宏,该宏指向当前正在执行进程的用户。正如这本书所述,
The kernel has a macro called current
which is pointing to users currently executing process. As this book states,
当前指针指向当前正在执行的用户进程。在执行系统调用(例如打开或读取)的过程中,当前进程是调用该调用的进程。
The current pointer refers to the user process currently executing. During the execution of a system call, such as open or read, the current process is the one that invoked the call.
换句话说,如@ GilHamilton在评论中提到, current
是 #define
d对函数 get_current()
在内核中。使用 current
作为变量名将产生编译时错误!
In other words, as @GilHamilton mentioned in the comments, current
is #define
d to the function get_current()
in the kernel. Using current
as a variable name will give a compile-time error!
这篇关于在内核模块中命名变量“ current”会导致“函数声明不是原型”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!