質問編集履歴
2
文書の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -16,95 +16,7 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
/*
|
20
19
|
|
21
|
-
* gcc -o a a.c -lpthread
|
22
|
-
|
23
|
-
*/
|
24
|
-
|
25
|
-
#プログラム
|
26
|
-
|
27
|
-
.#include <pthread.h>
|
28
|
-
|
29
|
-
.#include <stdio.h>
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
|
34
|
-
|
35
|
-
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
void *f1(void *arg) {
|
40
|
-
|
41
|
-
while (1) {
|
42
|
-
|
43
|
-
// デッドロックが発生する可能性がある m1,m2 の操作
|
44
|
-
|
45
|
-
// ここを修正
|
46
|
-
|
47
|
-
}
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
return NULL;
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
void *f2(void *arg) {
|
58
|
-
|
59
|
-
while (1) {
|
60
|
-
|
61
|
-
// デッドロックが発生する可能性がある m1,m2 の操作
|
62
|
-
|
63
|
-
// ここを修正
|
64
|
-
|
65
|
-
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
return NULL;
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
int main(int argc, char *argv[]) {
|
76
|
-
|
77
|
-
pthread_t t1, t2;
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
if (pthread_create(&t1, NULL, f1, NULL)) {
|
82
|
-
|
83
|
-
perror("pthread_create");
|
84
|
-
|
85
|
-
return 0;
|
86
|
-
|
87
|
-
}
|
88
|
-
|
89
|
-
if (pthread_create(&t2, NULL, f2, NULL)) {
|
90
|
-
|
91
|
-
perror("pthread_create");
|
92
|
-
|
93
|
-
return 0;
|
94
|
-
|
95
|
-
}
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
pthread_join(t1, NULL);
|
100
|
-
|
101
|
-
pthread_join(t2, NULL);
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
return 0;
|
106
|
-
|
107
|
-
}
|
108
20
|
|
109
21
|
#試したこと
|
110
22
|
|
1
文書の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
デッドロックを
|
1
|
+
デッドロックが発生するプログラムを作成したいと思います。
|
2
|
-
|
2
|
+
|
3
|
-
下記のテンプレート
|
3
|
+
下記のプログラムがテンプレートでそこから自分で試してみました。
|
4
|
+
|
5
|
+
このプログラムがデッドロックを発生しているか教えてください。
|
6
|
+
|
7
|
+
('#'を先頭に記載する方法も教えて下さい。)
|
8
|
+
|
9
|
+
|
4
10
|
|
5
11
|
|
6
12
|
|
@@ -16,9 +22,11 @@
|
|
16
22
|
|
17
23
|
*/
|
18
24
|
|
25
|
+
#プログラム
|
26
|
+
|
19
|
-
#include <pthread.h>
|
27
|
+
.#include <pthread.h>
|
20
|
-
|
28
|
+
|
21
|
-
#include <stdio.h>
|
29
|
+
.#include <stdio.h>
|
22
30
|
|
23
31
|
|
24
32
|
|
@@ -97,3 +105,99 @@
|
|
97
105
|
return 0;
|
98
106
|
|
99
107
|
}
|
108
|
+
|
109
|
+
#試したこと
|
110
|
+
|
111
|
+
.#include <pthread.h>
|
112
|
+
|
113
|
+
.#include <stdio.h>
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
|
118
|
+
|
119
|
+
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
void *f1(void *arg) {
|
124
|
+
|
125
|
+
while (1) {
|
126
|
+
|
127
|
+
pthread_mutex_lock(&m1);
|
128
|
+
|
129
|
+
printf("f1: running\n");
|
130
|
+
|
131
|
+
pthread_mutex_lock(&m2);
|
132
|
+
|
133
|
+
pthread_mutex_unlock(&m2);
|
134
|
+
|
135
|
+
pthread_mutex_unlock(&m1);
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
return NULL;
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
void *f2(void *arg) {
|
148
|
+
|
149
|
+
while (1) {
|
150
|
+
|
151
|
+
pthread_mutex_lock(&m2);
|
152
|
+
|
153
|
+
printf("f2: running\n");
|
154
|
+
|
155
|
+
pthread_mutex_lock(&m1);
|
156
|
+
|
157
|
+
pthread_mutex_unlock(&m1);
|
158
|
+
|
159
|
+
pthread_mutex_unlock(&m2);
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
return NULL;
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
int main(int argc, char *argv[]) {
|
172
|
+
|
173
|
+
pthread_t t1, t2;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
if (pthread_create(&t1, NULL, f1, NULL)) {
|
178
|
+
|
179
|
+
perror("pthread_create");
|
180
|
+
|
181
|
+
return 0;
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
if (pthread_create(&t2, NULL, f2, NULL)) {
|
186
|
+
|
187
|
+
perror("pthread_create");
|
188
|
+
|
189
|
+
return 0;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
pthread_join(t1, NULL);
|
196
|
+
|
197
|
+
pthread_join(t2, NULL);
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
return 0;
|
202
|
+
|
203
|
+
}
|