我是靠谱客的博主 从容星星,这篇文章主要介绍操作系统实验3(银行家算法),现在分享给大家,希望可以做个参考。

本次实验由本菜鸟一步一步做出来。最大的感想就是,果然语言本身才是浪费时间最多的地方,比如说那个pthread_create()的最后的参数,不知如何传,上网查看,都是一大坨一大坨的,贴一大段复杂的不得了的代码,随便说两句,谁看得懂?只好瞎试。最后明白了
int i = 99;
pthread_create(&tid[i], NULL, runner, (void*)i);

而传到了runner函数中,在void *runner( void * param)中,如果需要提取,则int x = (int)param;

而关于银行家算法本身,遵循算法本身的规则,然后注意一下细节,就比较容易做出来了。具体细节不再赘述,先写报告。

复制代码
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <semaphore.h> #define NUMBER_OF_CUSTOMERS 5 #define NUMBER_OF_RESOURCES 3 int request_resource(int customer_num, int request[]); int release_resource(int customer_num, int request[]); void *runner(void *param); sem_t s, st; int real_finish[NUMBER_OF_CUSTOMERS]={0}; int real_count = 0; int available[NUMBER_OF_RESOURCES]; int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {7, 5, 3, 3, 2, 2, 9, 0, 2, 2, 2, 2, 4, 3, 3}; int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {0, 1, 0, 2, 0, 0, 3, 0, 2, 2, 1, 1, 0, 0, 2}; int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES] = {7, 4, 3, 1, 2, 2, 6, 0, 0, 0, 1, 1, 4, 3, 1}; int main(int argc, char *argv[]) { pthread_t tid[5]; sem_init(&s, 0, 1); sem_init(&st, 0, 1); int i; for(i = 0; i <= 2; ++i) available[i] = atoi(argv[i+1]); //printf("%s, %s, %s, %sn", argv[0], argv[1], argv[2], argv[3]); //the first command decides the value of available //start the main code //in circle, request input values for(; ; ) { //the end condition if(NUMBER_OF_CUSTOMERS == real_count) break; //create five threads for(i = 0; i <= 4; ++i) { //judge whether request can be accepted pthread_create(&tid[i], NULL, runner, (void*)i); //test //printf("test: here is %dn", i); } //wait for the threads end int j; for(j = 0; j <= 4; ++j) pthread_join(tid[j], NULL); } printf("task completen"); return 0; } void *runner(void *param) { int x = (int)param; sem_wait(&st); int request[3]; printf("to pthread[%d] please input the request you wantn", x); scanf("%d %d %d", &request[0], &request[1], &request[2]); sem_post(&st); //request the resource and may release it sem_wait(&s);//--------------------------critical area s int judge = request_resource(x, request); //when the request is successful if(0 == judge) { //get into a new status release_resource(x, request); } sem_post(&s);//--------------------------critical area e //when failing, it needs to be hung if(-1 == judge) { //with the releasing, the bigger the available is, //the eassilier meet the request //no need to put the request fucton into the mutex area while(-1 == request_resource(x, request)); sem_wait(&s);//------------------critical area s release_resource(x, request); sem_post(&s);//------------------critical area e } //test the pthread_join /* if(0 == x) { while(99 != y); sem_wait(&s); printf("%dn", x); sem_post(&s); } else { sem_wait(&s); printf("%dn", x); if(4 == x) y = 99; sem_post(&s); } */ pthread_exit(0); } int request_resource(int customer_num, int request[]) { //return (1 == customer_num?0:-1); int judge = 0; int i, j; int finish_flag = 0;//is there a p finished? int finish_num;//what's the number int t_allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES]; int t_available[NUMBER_OF_RESOURCES]; int t_need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES]; //judge the first step and save the status //printf("here is the request of the customer%dn", customer_num); if(request[0] <= available[0] && request[1] <= available[1] && request[2] <= available[2] && request[0] <= need[customer_num][0] && request[1] <= need[customer_num][1] && request[2] <= need[customer_num][2]) { //initialize the new status 1 //initialize the t_allocation, t_need for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i) for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) { t_allocation[i][j] = allocation[i][j]; t_need[i][j] = need[i][j]; if(customer_num == i) { t_allocation[i][j] = allocation[i][j] + request[j]; t_need[i][j] = need[i][j]- request[j]; } } //judge to release to initialize the new status 2 //in the mean time, initialize the t_available int judge_release = 1; for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) if(request[j] != need[customer_num][j]) judge_release = 0; if(1 == judge_release) { printf("get into the new status 2n"); finish_flag = 1; finish_num = customer_num; for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) t_available[j] = available[j] + allocation[customer_num][j]; } else for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) t_available[j] = available[j] - request[j]; } else return -1; //judge whether it is the safet state //critical point: how to jump out of the dangerous status? int count = 0;//count the number of the finishing int count_d = -1;//count the time of undone to judge whether to jump int finish[NUMBER_OF_CUSTOMERS]= {0}; //init the real finish if(1 == finish_flag) {real_finish[finish_num] = 1; ++real_count;} //initialize the finish for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i) {finish[i] = real_finish[i]; count = real_count;} //start the check of the safety while(NUMBER_OF_CUSTOMERS != count) { //in one round, if there's no change, we consider it is unsafe if(0 == count_d) return -1; count_d = 0; for(i = 0; i <= NUMBER_OF_CUSTOMERS-1; ++i) { if(1 == finish[i]) continue; int j_release = 1; for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) if(t_need[i][j] > t_available[j]) {j_release = 0; break;} if(1 == j_release) { finish[i] = 1; count_d = 1; ++count; for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) t_available[j] += t_allocation[i][j]; } } } //printf("testn"); return 0; } int release_resource(int customer_num, int request[]) { int j; printf("accept the request of the customer%dn", customer_num); for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) { available[j] = available[j] - request[j]; allocation[customer_num][j] += request[j]; need[customer_num][j] -= request[j]; printf("dec available[%d] is %dn", j, available[j]); } int j_release = 1; for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) if(need[customer_num][j] > 0) { j_release = 0; break; } if(1 == j_release) { for(j = 0; j <= NUMBER_OF_RESOURCES-1; ++j) { allocation[customer_num][j] -= request[j]; available[j] += request[j] + allocation[customer_num][j]; printf("inc available[%d] is %dn", j, available[j]); } } return 0; }

最后

以上就是从容星星最近收集整理的关于操作系统实验3(银行家算法)的全部内容,更多相关操作系统实验3(银行家算法)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部