简体   繁体   中英

how to define a global variable inside linux kernel?

I am new to kernel programming and trying to implement a system call in linux kernel 3.19 which keeps track of the processes in a linked list. So every time the system call is invoked from user-space (by some wrapper function) a new process has to be added to that list. My system call looks something like

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 */
}

Now the mylist variable used above should be defined global so as to maintain the list for subsequent system calls. How to accomplish that? Do I have to declare mylist variable in linux/init/main.c or I can simply use EXPORT_GLOBAL . I also read about using extern but couldn't figure out where to declare and define the variable.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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