繁体   English   中英

如何在Linux内核中重用变量?

[英]how to reuse the variable in linux kernel?

extern unsigned long current_rx_time;
EXPORT_SYMBOL(current_rx_time);
int netif_rx(struct sk_buff *skb) 
{

current_rx_time = jiffies;

}

如上所述,我在dev.c中修改了内核源代码。 稍后,我在procfs中创建一个可加载的内核模块,并使用currentrx_time将其发送到用户空间,如下所示:

static int my_proc_show(struct seq_file *m, void *v)
{
    //I AM JUST PRINTING THAT VALUE BELOW

    seq_printf(m, "%lu\n", current_rx_time *1000/HZ);

    return 0;
}

但是由于未声明current_rx_time在上面编译我的模块时遇到错误。 有人可以告诉我如何解决这个问题吗?

首先,您需要声明变量,然后可以将其导出。

因此只需将其声明为dev.c

unsigned long current_rx_time;

然后像在dev.c中一样导出

EXPORT_SYMBOL(current_rx_time);

以及要使用它的其他可加载模块中(例如在temp2.​​c中)...

extern unsigned long current_rx_time;

现在确保在那时要编译temp2.​​c时,dev.c也正在编译。

第二个代码需要声明外部变量,因此链接器可以知道它来自外部:

extern unsigned long current_rx_time;

暂无
暂无

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

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