-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwtest.c
91 lines (72 loc) · 1.76 KB
/
rwtest.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
#include "types.h"
#include "user.h"
#define NCHILD 10
void testReadersWriters(int* pattern , int pattern_size);
int main ()
{
char argv[100];
printf(1,"Enter pattern for Readers/Writers test:\n");
int read_size = read(0,argv,sizeof(argv));
argv[read_size -1] = '\0';
int pattern[100], i;
for (i = 0; argv[i + 1] != '\0'; i++){
if(argv[i + 1] == '0'){
pattern[i] = 0;
}
else if(argv[i + 1] == '1'){
pattern[i] = 1;
}
else{
printf(1,"pattern is not correct");
exit();
}
}
testReadersWriters(pattern,i);
exit();
return 0;
}
void testReadersWriters(int* pattern , int pattern_size){
int pid, i;
rwinit();
pid = fork();
int parent = pid;
for(i = 1; i < pattern_size; i++){
if (pid < 0){
printf(1,"fork failed\n");
exit();
}
else if (pid > 0){
pid = fork();
}
else {
break;
}
}
if (pid < 0){
printf(1,"fork failed\n");
exit();
}
else if (pid == 0) {
// printf(1,"child adding to shared counter \n");
// int res = rwtest (pattern[i-1]);
rwtest (pattern[i-1]);
if (pattern[i - 1] == 0) {
// printf(2,"reader read from shared counter: %d\n",res);
}
else {
// printf(1,"writer added to shared counter\n");
}
}
else {
if (pid == parent){
sleep(1000);
}
for (i = 0; i < pattern_size; i++)
{
wait();
}
printf(1,"user program finished\n");
int res = rwtest(0);
printf(1,"last value of shared counter is: %d", res);
}
}