/*
* complete.c -- the writers awake the readers
*/
------------------------------------------------------------
#include
#include
#include /* current and everything */
#include /* printk() */
#include /* everything... */
#include /* size_t */
#include
MODULE_LICENSE("Dual BSD/GPL");
static int complete_major = 253; //指定主设备号
DECLARE_COMPLETION(comp);
ssize_t complete_read (
struct file *filp, char __user *buf,
size_t count, loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) going to sleepn",
current->pid, current->comm);
wait_for_completion(&comp);
printk(KERN_DEBUG "awoken %i (%s)n", current->pid, current->comm);
return 0;
}
ssize_t complete_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);
complete(&comp);
return count;
}
struct file_operations complete_fops = {
.owner = THIS_MODULE,
.read = complete_read,
.write = complete_write,
};
int complete_init(void)
{
int result;
result = register_chrdev(complete_major, "completion", &complete_fops);
if (result < 0)
return result;
if (complete_major == 0)
complete_major = result;
return 0;
}
void complete_cleanup(void)
{
unregister_chrdev(complete_major, "completion");
}
module_init(complete_init);
module_exit(complete_cleanup);
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
#undef KERN_DEBUG
#define KERN_DEBUG "<1>"
static int complete_major=253;
static int reader_count = 0;
DECLARE_COMPLETION(comp);
ssize_t complete_read (struct file *filp,char __user *buf,size_t count,loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) going to sleep,waiting for writern",current->pid,current->comm);
reader_count++;
printk(KERN_DEBUG "In read ,before comletion: reader count = %d n",reader_count);
wait_for_completion(&comp);
reader_count--;
printk(KERN_DEBUG "awoken %s (%i) n",current->comm,current->pid);
printk(KERN_DEBUG "In read,after completion : reader count = %d n",reader_count);
/*如果使用complete_all,则completion结构只能用一次,再次使用它时必须调用此宏进行重新初始化*/
if(reader_count == 0)
INIT_COMPLETION(comp);
return 0;
}
ssize_t complete_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awoking the readers...n",current->pid,current->comm);
printk(KERN_DEBUG "In write ,before do complete_all : reader count = %d n",reader_count);
if(reader_count != 0)
complete_all(&comp);
printk(KERN_DEBUG "In write ,after do complete_all : reader count = %d n",reader_count);
return count;
}
struct file_operations complete_fops={
.owner = THIS_MODULE,
.read = complete_read,
.write = complete_write,
};
int complete_init(void)
{
int result;
result=register_chrdev(complete_major,"complete",&complete_fops);
if(result<0)
return result;
if(complete_major==0)
complete_major =result;
printk(KERN_DEBUG "complete driver test init! complete_major=%dn",complete_major);
printk(KERN_DEBUG "静态初始化completionn");
return 0;
}
void complete_exit(void)
{
unregister_chrdev(complete_major,"complete");
printk(KERN_DEBUG "complete driver is removedn");
}
module_init(complete_init);
module_exit(complete_exit);