繁体   English   中英

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

[英]how to define a global variable inside linux kernel?

我是内核编程的新手,正在尝试在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,因为这对于可加载模块也是可见的,并将其声明为正在使用的文件或本地头文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM