-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.c
98 lines (75 loc) · 1.4 KB
/
tester.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define t 10
#define s 1
const int MTX_OPEN = 331;
const int MTX_CLOSE = 332;
const int MTX_LOCK = 333;
const int MTX_UNLOCK = 334;
const int MTX_LIST = 335;
const int MTX_GRANT = 336;
void play()
{
char x0 = 'a' + rand()%26;
char x1 = 'a' + rand()%26;
while(x0 == x1)
{
x1 = 'a' + rand()%26;
}
char s1[] = {x0, 0};
char s2[] = {x1, 0};
printf("requesting %c and %c\n", x0, x1);
int d[2];
d[0] = syscall(MTX_OPEN, s1);
d[1] = syscall(MTX_OPEN, s2);
int i;
for(i = 0; i < s; i++)
{
int chosen_d = d[rand()&1];
syscall(MTX_LOCK, chosen_d);
sleep(1);
syscall(MTX_UNLOCK, chosen_d);
}
syscall(MTX_CLOSE, d[0]);
syscall(MTX_CLOSE, d[1]);
}
int main()
{
pid_t dd[t];
int i;
for(i = 0; i < t; i++)
{
dd[i] = fork();
if(dd[i] == 0)
{
play();
return 0;
}
}
pid_t *p = malloc(1000 * sizeof(pid_t));
int *d = malloc(1000 * sizeof(int));
for(i = 0; i < t; i++)
{
int many = syscall(MTX_LIST, d, p, 1000);
if(many < 0) {
printf("error\n");
return 0;
}
int i;
for(i = 0; i < many; i++) {
printf("awaiting pair mutex %d proc %d\n", d[i], p[i]);
}
printf("\n\n");
fflush(stdout);
pid_t child = wait(NULL);
printf("child %d returned\n", child);
}
return 0;
}