在linux開(kāi)發(fā)過(guò)程中,經(jīng)常會(huì)遇到在/proc或者/tmpfs下增加文件的情況,/tmpfs下的文件通常都是對(duì)采集時(shí)間有要求,因此采集完成后保存在相關(guān)的文件中,系統(tǒng)需要時(shí)進(jìn)行獲取。對(duì)應(yīng)的數(shù)據(jù)通常也不是實(shí)時(shí)的,除非利用定時(shí)器或者信號(hào)配合先采集后進(jìn)行獲取。對(duì)于/tmpfs下的文件,可使用"ls -lh"查看出文件的實(shí)際大小。這說(shuō)明文件實(shí)際是被寫入了內(nèi)容的,通常/tmpfs文件會(huì)在應(yīng)用層進(jìn)行讀寫操作。
對(duì)應(yīng)地/proc文件一般只是類似一個(gè)殼,實(shí)際上并不是把內(nèi)容寫入文件中,使用“l(fā)s -lh”查看大小通常為0。/proc文件是在獲取時(shí)(執(zhí)行cat)實(shí)時(shí)采集并顯示出來(lái),結(jié)果是實(shí)時(shí)的并且不占空間,跟/tmpfs相比,/proc文件保存在內(nèi)核內(nèi)存中, 因?yàn)樽x取性能及對(duì)系統(tǒng)影響較小。
下面通過(guò)一個(gè)例子來(lái)展示/proc文件的開(kāi)發(fā)過(guò)程:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
struct proc_dir_entry *proc_ctcwifi_dir = NULL;struct proc_dir_entry *ctcwifi_diag_enable = NULL;
unsigned char g_ctcwifiDiagEnable;
ssize_t start_ctcwifiDiagEnable_write(struct file *fp,const char __user *buffer,size_t count,loff_t *data)
{
int i = 0;
unsigned char tmp = 0;
while(buffer[i] == ' ')
{
i++;
}
tmp = buffer[i] - '0';
if((tmp == 1) || (tmp == 0))
{
g_ctcwifiDiagEnable = tmp;
}
else
{
printk("the input parameter out of range\n");
}
return count;}
static int ctcwifiDiagEnable_proc_read(struct seq_file *m, void *v)
{
int len = 0;
len = seq_printf(m, "%d\n", g_ctcwifiDiagEnable);
return len;
}
static int ctcwifiDiagEnable_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, ctcwifiDiagEnable_proc_read, NULL);
}
};
int wlan_ctcwifi_init(void)
/*create /proc/ctcwifi/diag_enable*/
if (( ctcwifi_diag_enable = proc_create("diag_enable", 0644, proc_ctcwifi_dir, &diag_enable_ops)) == NULL )if (ctcwifi_diag_enable)
{
remove_proc_entry("diag_enable", proc_ctcwifi_dir);}
/* remove /proc/ctcwifi*/
if (proc_ctcwifi_dir)module_exit(wlan_ctcwifi_exit);
MODULE_AUTHOR("ZC");本站文章版權(quán)歸原作者及原出處所有 。內(nèi)容為作者個(gè)人觀點(diǎn), 并不代表本站贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé),本站只提供參考并不構(gòu)成任何投資及應(yīng)用建議。本站是一個(gè)個(gè)人學(xué)習(xí)交流的平臺(tái),網(wǎng)站上部分文章為轉(zhuǎn)載,并不用于任何商業(yè)目的,我們已經(jīng)盡可能的對(duì)作者和來(lái)源進(jìn)行了通告,但是能力有限或疏忽,造成漏登,請(qǐng)及時(shí)聯(lián)系我們,我們將根據(jù)著作權(quán)人的要求,立即更正或者刪除有關(guān)內(nèi)容。本站擁有對(duì)此聲明的最終解釋權(quán)。