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

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

最佳答案

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

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

相关文章:

linux - 如何在内核代码中查找 ssh 连接的 IP?

linux-kernel - kmalloc 和 vmalloc 之间的主要区别是什么?

c - "undefined reference to strerror_r"与 OpenSSL-1.1.0e 和 MinGW

c - 模式搜索朴素方法

c - 使用C中的两个管道或一个管道进行2次以上的读/写?如何?

linux - 什么是内核部分不匹配?

r - 我们如何在构建 R 包时设置常量变量?

c++ - C++ 中的全局变量

Javascript 全局变量不起作用

python - libccv - 如何从内存中的字节读取图像