file: complete.c
1
/*
2
* complete.c -- the writers awake the readers
3
*
4
* Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet
5
* Copyright (C) 2003 O'Reilly & Associates
6
*
7
* The source code in this file can be freely used, adapted,
8
* and redistributed in source or binary form, so long as an
9
* acknowledgment appears in derived source files. The citation
10
* should list that the code comes from the book "Linux Device
11
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
12
* by O'Reilly & Associates. No warranty is attached;
13
* we cannot take responsibility for errors or fitness for use.
14
*
15
*/
16
17
#include <linux/module.h>
18
#include <linux/init.h>
19
20
#include <linux/sched.h>
21
#include <linux/kernel.h>
22
#include <linux/fs.h>
23
#include <linux/types.h>
24
#include <linux/cdev.h>
25
#include <linux/completion.h>
26
27
MODULE_LICENSE("Dual BSD/GPL");
28
29
static int /*X*/ complete_major = 0;
30
31
DECLARE_COMPLETION( /*X*/ comp);
32
33
ssize_t /*X*/ complete_read(struct file *filp, char __user *buf, size_t count, loff_t *pos)
34
{
35
printk(KERN_DEBUG "process %i (%s) going to sleep\n",
36
current->pid, current->comm);
37
wait_for_completion(&comp);
38
printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
39
return 0; /* EOF */
40
}
41
42
ssize_t /*X*/ complete_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos)
43
{
44
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
45
current->pid, current->comm);
46
complete(&comp);
47
return count; /* avoid retries by returning success of entire buffer */
48
}
49
50
struct file_operations /*X*/ complete_fops = {
51
.owner = THIS_MODULE,
52
.read = complete_read,
53
.write = complete_write,
54
};
55
56
static int __init /*X*/ complete_init(void)
57
{
58
int result;
59
60
/*
61
* Register major number
62
*/
63
result = register_chrdev(complete_major, "complete", &complete_fops);
64
if (result < 0)
65
return result;
66
if (complete_major == 0)
67
complete_major = result; /* dynamic */
68
return 0;
69
}
70
71
static void __exit /*X*/ complete_cleanup(void)
72
{
73
unregister_chrdev(complete_major, "complete");
74
}
75
76
module_init( /*X*/ complete_init);
77
module_exit( /*X*/ complete_cleanup);
C to HTML Conversion by ctoohtml