Digression:
This should be a bit difficult homework recently, at least, than the previous barber, read and write, philosophers problem more difficult.
But do the structure of the program, top-down, it was not difficult.
About banker's algorithm:
![](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)
Code:
init () initialization reset () recycling Banker () banker's algorithm safe () security algorithm
![](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)
Renderings:
![](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)
Problems encountered:
The main problem is to start with a statement sem_t mutex, the results can not pthread_cond_wait (& cond, & mutex); it seems to be with pthread_mutex_t mutex;
vector is specified when the array clear Allocation [i], instead Allocation.clear ()
![](https://upload-images.jianshu.io/upload_images/16981913-3584cc2a94e306ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
wrong reason:
First: I forgot to add &
![](https://upload-images.jianshu.io/upload_images/16981913-0386dded26970c8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Second: i ++ continues, although calling pthread_create before i ++, but there might be led to pass the time difference is the increased number i, leading to cross-border. So for an array.
![](https://upload-images.jianshu.io/upload_images/16981913-a3c994064c8c1a50.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
As well as common closing parenthesis forget, forget the semicolon ....