gpt4 book ai didi

c - 如何在linux内核中定义全局变量?

转载 作者:行者123 更新时间:2023-11-30 16:58:51 26 4
gpt4 key购买 nike

我是内核编程新手,并尝试在 Linux 内核 3.19 中实现一个系统调用,该系统调用跟踪链表中的进程。因此,每次从用户空间(通过某些包装函数)调用系统调用时,都必须将新进程添加到该列表中。我的系统调用看起来像

asmlinkage long sys_newcall(pid_t pid)
{
/*
* mytasks is the name of the structure
* kmalloc() is invoked to create an instance
*/
struct mytasks newTask = kmalloc(sizeof(struct mytasks), GFP_KERNEL);
/* various checks */
/* now adding the new instance to the list */

list_add_tail(&(newTask->list),&(mylist->list));
/* i have put list_head struct in my own structure to make use of above interface */
}

现在上面使用的mylist变量应该定义为全局变量,以便维护后续系统调用的列表。如何做到这一点?我是否必须在 linux/init/main.c 中声明 mylist 变量,或者我可以简单地使用 EXPORT_GLOBAL。我还阅读了有关使用 extern 的内容,但无法弄清楚在哪里声明和定义变量。

最佳答案

使用 EXPORT_SYMBOL 是最好的,因为这对于可加载模块也是可见的,并在您正在使用的文件或本地头文件中声明它

关于c - 如何在linux内核中定义全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38506379/

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