forked from gsoosk/POS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockTest.c
166 lines (150 loc) · 3.1 KB
/
lockTest.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "types.h"
#include "stat.h"
#include "fcntl.h" // for using file defines
#include "user.h" // for using from strlen
#define NCHILD 10
void ownerBase(void);
void ticketLockTest(void);
void readerWriterLock(void);
void writerReaderLock(void);
int main(int argc, char *argv[])
{
printf(1, "Which lock do you want to test ? \n");
printf(1, "1.owner base release sleep lock\n");
printf(1, "2.ticket lock shared counter test\n");
printf(1, "3.test reader writer lock with readers priority\n");
printf(1, "4.test reader writer lock with writers priority\n");
char buf[1024];
while(read(1, buf, 1024))
{
if(atoi(buf) == 1)
{
ownerBase();
break;
}
else if(atoi(buf) == 2)
{
ticketLockTest();
break;
}
else if(atoi(buf) == 3)
{
readerWriterLock();
break;
}
else if(atoi(buf) == 4)
{
writerReaderLock();
break;
}
else
printf(1, "enter a valid number please.\n");
}
exit();
}
void ownerBase()
{
acquiresleep_syscalls();
int pid = fork();
if(pid == 0)
{
releasesleep_syscalls();
exit();
}
wait();
printf(1, "child terminated\n");
releasesleep_syscalls();
}
void ticketLockTest()
{
int pid;
ticketlockinit();
delay(100);
pid = fork();
int i;
for(i = 1; i < NCHILD; i++)
{
if(pid > 0)
pid = fork();
if(i % 3 == 0 && pid == 0)
delay(2000);
}
if(pid < 0)
{
printf(2, "fork error\n");
}
else if(pid == 0)
{
delay(100);
printf(1, "child adding to shared counter\n");
ticketlocktest();
}
else
{
int i;
for(i = 0; i < NCHILD; i++)
wait();
printf(1, "user program finished\n");
}
}
int findNumberDigits(uint pattern)
{
int num = 0;
while(1)
{
pattern = pattern / 10;
if(pattern == 0)
break;
num++;
}
return num+1;
}
void readerWriterLock()
{
char buf[1024];
printf(1, "enter pattern : \n");
int numOfChars = read(1, buf, 1024);
buf[numOfChars] = '\0';
rwinit();
int i;
for(i = 1; i < numOfChars - 1; i++)
{
if (fork() == 0)
{
if(buf[i] == '0')
rwtest(0);
else if(buf[i] == '1')
rwtest(1);
exit();
}
else
continue;
}
for(i = 1; i < strlen(buf) - 1; i++)
wait();
}
void writerReaderLock()
{
char buf[1024];
printf(1, "enter pattern : \n");
int numOfChars = read(1, buf, 1024);
buf[numOfChars] = '\0';
wrinit();
// char p[1];
int i;
for(i = 1; i < numOfChars - 1; i++)
{
if (fork() == 0)
{
if(buf[i] == '0')
wrtest(0);
else if(buf[i] == '1')
wrtest(1);
exit();
}
else
continue;
}
for(i = 1; i < strlen(buf) - 1; i++)
wait();
}