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

时间:2016-07-21 14:02:00

标签: c linux-kernel global-variables

我是内核编程的新手,并尝试在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变量应该被定义为全局,以便维护后续系统调用的列表。怎么做到这一点?我是否必须在mylist中声明linux/init/main.c变量,或者我只能使用EXPORT_GLOBAL。我还读到了关于使用extern但无法弄清楚声明和定义变量的位置。

1 个答案:

答案 0 :(得分:0)

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