【发布时间】:2016-07-23 14:43:17
【问题描述】:
我是内核编程的新手,并试图在 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 的信息,但不知道在哪里声明和定义变量。
【问题讨论】:
-
“如何在 linux 内核中定义一个全局变量?”你不会... ;-)
标签: c linux-kernel global-variables