当前位置: 技术问答>linux和unix
LKM 中一个例子的问题
来源: 互联网 发布时间:2016-07-17
本文导语: #include /* We're doing kernel work */ #include /* Specifically, a module */ #include /* Necessary because we use proc fs */ #include /* For putting processes to sleep and waking them up */ #include /* for get_user and put_user */ ...
#include /* We're doing kernel work */
#include /* Specifically, a module */
#include /* Necessary because we use proc fs */
#include /* For putting processes to sleep and
waking them up */
#include /* for get_user and put_user */
#define MESSAGE_LENGTH 80
static char Message[MESSAGE_LENGTH];
static struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "sleep"
static ssize_t module_output(struct file *file, /* see include/linux/fs.h */
char *buf, /* The buffer to put data to
(in the user segment) */
size_t len, /* The length of the buffer */
loff_t * offset)
{
static int finished = 0;
int i;
char message[MESSAGE_LENGTH + 30];
if (finished) {
finished = 0;
return 0;
}
sprintf(message, "Last input:%sn", Message);
for (i = 0; i owner = THIS_MODULE;
Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
Our_Proc_File->proc_fops = &File_Ops_4_Our_Proc_File;
Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 80;
if (Our_Proc_File == NULL) {
rv = -ENOMEM;
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
printk(KERN_INFO "Error: Could not initialize /proc/testn");
}
return rv;
}
void cleanup_module()
{
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
}
以上带颜色的部分有什么用呢,我把它注掉没发现有什么区别?
高手解答,谢谢了
#include /* Specifically, a module */
#include /* Necessary because we use proc fs */
#include /* For putting processes to sleep and
waking them up */
#include /* for get_user and put_user */
#define MESSAGE_LENGTH 80
static char Message[MESSAGE_LENGTH];
static struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "sleep"
static ssize_t module_output(struct file *file, /* see include/linux/fs.h */
char *buf, /* The buffer to put data to
(in the user segment) */
size_t len, /* The length of the buffer */
loff_t * offset)
{
static int finished = 0;
int i;
char message[MESSAGE_LENGTH + 30];
if (finished) {
finished = 0;
return 0;
}
sprintf(message, "Last input:%sn", Message);
for (i = 0; i owner = THIS_MODULE;
Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
Our_Proc_File->proc_fops = &File_Ops_4_Our_Proc_File;
Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 80;
if (Our_Proc_File == NULL) {
rv = -ENOMEM;
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
printk(KERN_INFO "Error: Could not initialize /proc/testn");
}
return rv;
}
void cleanup_module()
{
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
}
以上带颜色的部分有什么用呢,我把它注掉没发现有什么区别?
高手解答,谢谢了
|
每个进程中一旦有阻塞的信号(即blocked),进程就会把它挂到pending链上,
所以一旦当前进程被抢占,当前进程的pending.signal.sig[i] 位图就会与
blocked.sig[i]位图不一致
for (i = 0; i pending.signal.sig[i] & ~current->
blocked.sig[i];
该句就是判断这两个位图是否一致,如果不一致is_sig就是1,
这时候说明当前进程被抢占。
进程一旦被抢占就不能保证能够再次执行,也就不能保证释放引用的模块
所以当is_sig为1时手工释放模块
if (is_sig) {
module_put(THIS_MODULE);
return -EINTR;[color=#FF0000]
}[/color]
所以一旦当前进程被抢占,当前进程的pending.signal.sig[i] 位图就会与
blocked.sig[i]位图不一致
for (i = 0; i pending.signal.sig[i] & ~current->
blocked.sig[i];
该句就是判断这两个位图是否一致,如果不一致is_sig就是1,
这时候说明当前进程被抢占。
进程一旦被抢占就不能保证能够再次执行,也就不能保证释放引用的模块
所以当is_sig为1时手工释放模块
if (is_sig) {
module_put(THIS_MODULE);
return -EINTR;[color=#FF0000]
}[/color]