题外话:
这应该是最近有点难度的作业了,起码比之前的理发师,读写,哲学家问题要难。
但是做好程序的结构,自顶向下,就还是不难的。
银行家算法简介:
![](https://upload-images.jianshu.io/upload_images/16981913-bae01e974827b3ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-b4725c586f78cf11.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-19fa9d79fac1302f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-c73f9b149297a9b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-8e6aab644e2fd6ec.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-a63f7ee8b7e06111.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-ed1854fb5de0ce66.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-ff88aa1eaef8dc57.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
代码:
init() 初始化 reset() 资源回收 Banker() 银行家算法 safe() 安全性算法
![](https://upload-images.jianshu.io/upload_images/16981913-50fe8c3fa17517bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-d02b477fafc34141.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-3739fdbde56c419b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-5e4b402cf7bf42c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-d1a11a00b01e9962.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-d9683e4c4216f8da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-b83485b4bd1bd2fc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-887219ccaabf1036.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
效果图:
![](https://upload-images.jianshu.io/upload_images/16981913-800fb83aec079dff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-a73a0940e239ce84.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-eead37887682e49d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](https://upload-images.jianshu.io/upload_images/16981913-c74d638b37e44756.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
遇到的问题:
主要问题是一开始用sem_t声明mutex,结果不能pthread_cond_wait(&cond, &mutex);看来必须用pthread_mutex_t mutex;
vector数组clear时需要指定Allocation[i],而非Allocation.clear()
![](https://upload-images.jianshu.io/upload_images/16981913-3584cc2a94e306ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
错误原因:
其一:忘了加&
![](https://upload-images.jianshu.io/upload_images/16981913-0386dded26970c8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其二:i在不断++,虽然调用pthread_create在i++前,但是可能存在时间差导致传的是i增加后的数,导致越界。所以换为数组。
![](https://upload-images.jianshu.io/upload_images/16981913-a3c994064c8c1a50.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以及常见的忘记右括号,忘记分号....