#include
#include
#include /* current and everything */
#include /* printk() */
#include /* everything... */
#include /* size_t */
#include
MODULE_LICENSE("Dual BSD/GPL");
static int sleepy_major = 0;
static DECLARE_WAIT_QUEUE_HEAD(wq); /*定义等待队列*/
static int flag = 0;
ssize_t sleepy_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) going to sleep\n",
current->pid, current->comm);
wait_event_interruptible(wq, flag != 0); /*使进程在给定等待队列上睡眠,直到给定条件为真*/
flag = 0;
printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
return 0; /* EOF */
}
ssize_t sleepy_write (struct file *filp, const char __user *buf, size_t count,
loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
current->pid, current->comm); /*current->pid进程ID 和 current->command当前进程的命令名*/
flag = 1;
wake_up_interruptible(&wq); /* 唤醒在等待队列 wq 上睡眠的进程 */
return count; /* succeed, to avoid retrial */
}
struct file_operations sleepy_fops = {
.owner = THIS_MODULE,
.read = sleepy_read,
.write = sleepy_write,
};
int sleepy_init(void)
{
int result;
/*
* Register your major, and accept a dynamic number
*/
result = register_chrdev(sleepy_major, "sleepy", &sleepy_fops);
if (result < 0)
return result;
if (sleepy_major == 0)
sleepy_major = result; /* dynamic */
return 0;
}
void sleepy_cleanup(void)
{
unregister_chrdev(sleepy_major, "sleepy");
}
module_init(sleepy_init);
module_exit(sleepy_cleanup);
阅读(452) | 评论(0) | 转发(0) |