-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader writer code.txt
91 lines (66 loc) · 1.28 KB
/
reader writer code.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#20K-0134 20K-0355
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/delay.h>
int writer(void* data);
int reader(void* data);
DEFINE_SPINLOCK(mutex);
DEFINE_SPINLOCK(wrt);
char msg[256];
int numreader = 0;
int nread;
int val1 = 32;
int writer(void* data)
{
spin_lock(&wrt);
int temp = val1;
val1 = val1 * 2;
printk("writer modified value %d to: %d ", temp , val1);
spin_unlock(&wrt);
return 0;
}
int reader(void* data)
{
spin_lock(&mutex);
numreader++;
if(numreader == 1) {
spin_lock(&wrt);
}
spin_unlock(&mutex);
printk("reader reads value %d", val1);
spin_lock(&mutex);
numreader--;
if(numreader == 0) {
spin_unlock(&wrt);
}
spin_unlock(&mutex);
return 0;
}
asmlinkage long sys_readerwriter(void)
{
int i;
int numberOfReaders;
struct task_struct *myWriter[5];
numberOfReaders = 10;
struct task_struct *readers[10];
for(i = 0; i < 5; i++) {
myWriter[i] =kthread_create(writer, NULL, "writer_thread");
if(myWriter[i]){
wake_up_process(myWriter[i]);
}
else{
kthread_stop(myWriter[i]);
}
}
for(i = 0; i < 10; i++)
{
readers[i] = kthread_create(reader, NULL, "reader_thread");
if(readers[i]){
wake_up_process(readers[i]);
}
else{
kthread_stop(readers[i]);
}
}
return 0;
}