gpt4 book ai didi

c - 在内核模块中命名变量 `current` 会导致 "function declaration isn’ t a prototype”错误

转载 作者:太空狗 更新时间:2023-10-29 17:24:14 24 4
gpt4 key购买 nike

作为初学者,我正在学习为 Linux 编写内核模块。我要做的是使用 DFS 算法将每个任务及其子进程写入内核日志。但是当我使用 Makefile 编译代码时,它显示了上面的错误:

function declaration isn’t a prototype [-Werror=strict-prototypes]
struct task_struct *current;

它指出函数 DFS 处的 task_struct 关键字。这是我的代码:

# 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");
}

谁知道怎么解决这个问题??

最佳答案

内核有一个名为current 的宏,它指向用户当前正在执行的进程。作为this book州,

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#defined到内核中的函数get_current() .使用 current 作为变量名会产生编译时错误!

关于c - 在内核模块中命名变量 `current` 会导致 "function declaration isn’ t a prototype”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32157741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com