procfs是比較老的一種使用者態與核心態的資料交換方式,核心的很多資料都是通過這種方式出口給使用者的,核心的很多引數也是通過這種方式來讓使用者方便設定的。除了sysctl出口到/proc下的引數,procfs提供的大部分核心引數是唯讀的。
struct proc_dir_entry *create_proc_entry(const
char *name, mode_t mode, struct proc_dir_entry *parent)
該函式用於建立乙個正常的proc條目,引數name給出要建立的proc條目的名稱,引數mode給出了建立的該proc條目的訪問許可權,引數 parent指定建立的proc條目所在的目錄。如果要在/proc下建立proc條目,parent應當為null。否則它應當為proc_mkdir 返回的struct proc_dir_entry結構的指標。
void remove_proc_entry(const
char *name, struct proc_dir_entry *parent)
該函式用於刪除上面函式建立的proc條目,引數name給出要刪除的proc條目的名稱,引數parent指定建立的proc條目所在的目錄。
struct proc_dir_entry *proc_mkdir(const
char * name, struct proc_dir_entry *parent)
該函式用於建立乙個proc目錄,引數name指定要建立的proc目錄的名稱,引數parent為該proc目錄所在的目錄。
可以通過cat和echo等檔案操作函式來檢視和設定這些proc檔案。特別需要指出,bigprocfile是乙個大檔案(超過乙個記憶體頁),對於這種大檔案,procfs有一些限制,因為它提供的快取,只有乙個頁,因此必須特別小心,並對超過頁的部分做特別的考慮,處理起來比較複雜並且很容易出錯,所有procfs並不適合於大資料量的輸入輸出。
makefile
obj-m := test_procfs.o
test_procfs-objs := procfs.o
kerneldir = /lib/modules/$(shell uname -r)/build
#kerneldir = /lib/modules/2.6
.32-220.el6.x86_64/build
pwd := $(shell pwd)
default:
$(make) -c $(kerneldir) m=$(pwd) modules
clean:
$(make) -c $(kerneldir) m=$(pwd) clean
#include
#include
#include
#include
#include
#include
#define str_max_size 255
static
int int_var;
static
char string_var[256];
static
char big_buffer[65536];
static
int big_buffer_len = 0;
static struct proc_dir_entry * myprocroot;
static
int first_write_flag = 1;
int int_read_proc(char *page, char **start, off_t off, int
count, int *eof, void *data)
int int_write_proc(struct file *file, const char __user *buffer,unsigned long
count, void *data)
* temp = sum;
printk("int write *tmp = %d", *temp);
return len;
}int string_read_proc(char *page, char **start, off_t off,int
count, int *eof, void *data)
int string_write_proc(struct file *file, const char __user *buffer, unsigned long
count, void *data)
copy_from_user(data, buffer, count);
printk("string_write_proc %s", data);
return
count;
}int bigfile_read_proc(char *page, char **start, off_t off, int
count, int *eof, void *data)
if (count > page_size)
if (big_buffer_len - off < count)
memcpy(page, data, count);
*start = page;
return
count;
}int bigfile_write_proc(struct file *file, const char __user *buffer, unsigned long
count, void *data)
if (65536 - big_buffer_len < count)
copy_from_user(p + big_buffer_len, buffer, count);
big_buffer_len += count;
return
count;
}static
int __init procfs_exam_init(void)
entry = create_proc_entry("astring", 0644, myprocroot);
if (entry)
entry = create_proc_entry("bigprocfile", 0644, myprocroot);
if (entry)
#else
printk("this module requires the kernel to support procfs,\n");
#endif
return0;}
static
void __exit procfs_exam_exit(void)
module_init(procfs_exam_init);
module_exit(procfs_exam_exit);
module_license("gpl");
載入
insmod test_procfs.ko
測試 [root@localhost lytest]# cd -
/proc/myproctest
[root@localhost myproctest]# ls
aint astring bigprocfile
[root@localhost myproctest]# echo 1 > aint
[root@localhost myproctest]# cat aint
1 [root@localhost myproctest]# echo 「abc」 >astring
[root@localhost myproctest]# cat astring
abc
[root@localhost myproctest]# dmesg
解除安裝 rmmod test_procfs.ko
核心空間與使用者空間
1.核心態與使用者態的區別 1 核心態 的 不受任何限制,可以訪問 所有處理器指令集 記憶體位址以及i o空間 2 使用者態的 只能訪問其位址空間的頁表項中規定的虛擬位址。2.使用者態到核心態的切換方式 1 系統呼叫 軟中斷的一種 由使用者程序主動發起 2 異常 當cpu在執行執行在使用者態下的程式...
核心空間與使用者空間
首先,這個概念的由來,我認為跟cpu的發展有很大關係,在目前cpu的保護模式下,系統需要對其賴以執行的資料進行保護,為了保證作業系統核心資料,我們把記憶體空間進行劃分,一部分為作業系統核心執行的空間,另一部分是應用程式執行的空間,所謂空間就是記憶體的位址。因此核心空間和使用者空間的概念就出現了。在3...
核心空間與使用者空間
關於虛擬記憶體有三點需要注意 上圖展示了整個程序位址空間的分布,其中4g的位址空間分為兩部分,在使用者空間內,對應了記憶體分布的五個段 資料段 段 bss段 堆 棧。在上篇文章中有詳細的介紹。這個圖示核心使用者空間的劃分,圖中最重要的就是高階記憶體的對映 其中kmalloc和vmalloc函式申請的...