操作系统课程设计-银行家算法

选了这个题目的同学有福了,可以参考一下~~~

有一些注释……?

另外……如果执行结果是乱码,把#define N 40改为更大的数。

多说一句:如果真的有帮助,麻烦给个评论再走呗~~~

  1 #include 
  2 #include 
  3 #include 
  4 #include  
  5 #define N 40 
  6 #define MM "Max_Matrix.txt"
  7 #define AM "Allocation_Matrix.txt"
  8 using namespace std;
  9 typedef struct Banker{
 10     int Max[N][N];
 11     int Allocation[N][N];
 12     int Need[N][N];
 13     int Availiable[N][N];
 14     int Work[N][N];
 15     int All_Resource[N][N];
 16     int Safe_Sequel[N];
 17     bool Visited[N][N];
 18     bool Finish[N][N];
 19     int Request[N];
 20 }BK;
 21 void Create_Random_Progress(){
 22     FILE *fp1,*fp2;
 23     fp1=fopen(MM,"w+");
 24     fp2=fopen(AM,"w+");
 25     if(fp1&&fp2){
 26         system("cls");
 27         cout<<"开始初始化进程……"<<endl;    
 28         int number;
 29         srand((unsigned) time(NULL)); //用时间做种,每次产生随机数不一样
 30         char Space=' ';
 31         char Enter='\n';
 32         for (int i=1; i<28; i++){
 33             number = rand() % 7; //产生0-6随机数
 34             fprintf(fp1,"%d",number);
 35             if(i%3!=0&&i!=1)
 36                 fprintf(fp1,"%c",Space);
 37             if(i%3==0)
 38                 fprintf(fp1,"%c",Enter);
 39             if(i==1)
 40                 fprintf(fp1,"%c",Space);
 41         }
 42         for (int i=1; i<28; i++){
 43             number = rand() % 7; //产生0-6的随机数
 44             fprintf(fp2,"%d",number);
 45             if(i%3!=0&&i!=1)
 46                 fprintf(fp2,"%c",Space);
 47             if(i%3==0)
 48                 fprintf(fp2,"%c",Enter);
 49             if(i==1)
 50                 fprintf(fp2,"%c",Space);
 51         }
 52         cout<<"初始化完成。"<<endl;
 53     }
 54     else
 55         cout<<"出现错误,请检查磁盘剩余空间。"<<endl;
 56     fclose(fp1);
 57     fclose(fp2);
 58 }
 59 void Banker_Get_Matrix_Max(){
 60     Banker b;
 61     FILE *fp;
 62     fp=fopen(MM,"r");
 63     int a;
 64     int i=1;
 65     while(i<5){
 66         for(int j=1;j<4;j++){
 67             if(fscanf(fp,"%d",&a)!=EOF)
 68                 b.Max[i][j]=a;
 69             }
 70         i++;
 71     }
 72     fclose(fp);
 73 }
 74 void Banker_Get_Matrix_Allocation(){
 75     Banker b;
 76     FILE *fp;
 77     fp=fopen(AM,"r");
 78     int a;
 79     int i=1;
 80     while(i<5){
 81         for(int j=1;j<4;j++){
 82             if(fscanf(fp,"%d",&a)!=EOF)
 83                 b.Allocation[i][j]=a;
 84             }
 85         i++;
 86     }
 87     fclose(fp);
 88 }
 89 void Banker_Get_Matrix_Completed(){
 90     Banker b;
 91     for(int i=1;i<5;i++)
 92         for(int j=1;j<4;j++){
 93             while(b.Max[i][j]-b.Allocation[i][j]<0)
 94                 b.Allocation[i][j]--;
 95         }
 96 }
 97 void Banker_Get_Matrix_Need(){
 98     Banker b;
 99     for(int i=1;i<5;i++)
100         for(int j=1;j<4;j++)
101             b.Need[i][j]=b.Max[i][j]-b.Allocation[i][j];
102 }
103 void Banker_Print_InitList(){
104     Banker b; 
105     printf("进程号    最 大 需 求 量 Max    已分配资源Allocation\n");
106     for(int i=1;i<5;i++){
107         cout<<"  "<"\t";
108         for(int j=1;j<4;j++)
109             cout<"    "; 
110         for(int j=1;j<4;j++){
111             cout<"    ";
112             if(j%3==0)
113                 cout<<endl;    
114         }
115     }
116 }
117 void Banker_Rest_Resource(){
118     Banker b;
119     cout<<"请输入此时系统可用资源向量:";
120     for(int i=1;i<4;i++)
121         cin>>b.Availiable[1][i];
122 }
123 void Banker_Get_All_Resource(){
124     Banker b;
125     int t=0;
126     for(int i=1;i<4;i++)
127         for(int j=1;j<5;j++){
128             t+=b.Allocation[j][i];
129             if(j%4==0){
130                 b.All_Resource[1][i]=t;
131                 t=0;    
132             }
133         }
134     for(int i=1;i<4;i++)
135         b.All_Resource[1][i]+=b.Availiable[1][i];
136     cout<<"系统资源向量总数:("; 
137     for(int i=1;i<4;i++){
138         cout<1][i];
139         if(i<3)
140             cout<<",";
141     }
142     cout<<")"<<endl;
143 }
144 void Banker_Print_Need(){
145     Banker b;
146     cout<<"进程对资源的需求矩阵:"<<endl;
147     for(int i=1;i<5;i++)
148         for(int j=1;j<4;j++){
149             cout<"    ";
150             if(j%3==0) 
151                 cout<<endl;
152         }
153 }
154 /*int Banker_Safety_Algorithm_Test(){
155     Banker b;
156     for(int i=1;i<4;i++)
157         b.Work[1][i]=b.Availiable[1][i];
158     for(int i=1;i<5;i++){
159         int counter=0;
160         for(int j=1;j<4;j++){
161             if(b.Work[1][j]-b.Need[i][j]>=0)
162                 counter++;
163             if(counter==3)
164                 b.Finish[i][j]=true;
165         }
166     }    
167     int t=0;
168     for(int i=1;i<5;i++)
169         if(b.Finish[i][j]==true)
170             t++;
171     if(t==0){
172         cout<<"没有任何资源能让这些任务执行完成。"<173         return 0;    
174     }else
175         return 1;
176 }*/
177 int Banker_Safety_Algorithm(){//int test
178     Banker b;
179     memset(b.Finish,false,sizeof(b.Finish)); 
180     memset(b.Visited,false,sizeof(b.Visited));
181     memset(b.Safe_Sequel,0,sizeof(b.Safe_Sequel));
182 //    if
183 //    if(test==0)
184 //        return 0;
185     int res=1;
186     for(int i=1;i<4;i++)
187         b.Work[1][i]=b.Availiable[1][i];
188     for(int r=1;r<5;r++){
189         for(int i=1;i<5;i++){
190             int counter=0;
191             for(int j=1;j<4;j++){
192                 if(b.Work[1][j]-b.Need[i][j]>=0)
193                     counter++;
194                 if(counter==3)
195                     b.Finish[i][j]=true;
196                 if(b.Finish[i][3]==true&&b.Visited[i][3]==false){
197                     for(int k=1;k<4;k++){
198                         b.Work[1][k]+=b.Allocation[i][k];
199                         b.Visited[i][3]=true;
200                         
201                     }
202                 b.Safe_Sequel[res]=i;
203                 res++;    
204                 }
205             }
206         }
207     }
208     int safe=1;
209     for(int i=1;i<5;i++)
210         if(b.Finish[i][3])
211             safe++;
212     if(safe==5){
213         cout<<"至少存在安全序列:";
214         for(int i=1;i)
215             cout<"\t";
216         cout<<"可使进程安全执行完毕。"<<endl;
217         return 1; 
218     }else
219         cout<<"系统资源不足。"<<endl;
220     return 0;    
221 }
222 /*    for(int i=1;i<5;i++){
223             for(int j=1;j<4;j++)
224                 
225                 }*/
226 //    }
227 void Banker_Get_Request(){
228     Banker b;
229     int n;
230     cout<<"请输入要申请资源的进程号和申请的各项资源数量:";
231     cin>>n;
232     for(int i=1;i<4;i++){
233         cin>>b.Request[i];
234         b.Need[n][i]+=b.Request[i];
235     }
236 }
237 int main()
238 {
239     int n;
240     char ch='y';
241     while(ch=='y'){
242         Create_Random_Progress();
243         Banker_Get_Matrix_Max();
244         Banker_Get_Matrix_Allocation();
245         Banker_Get_Matrix_Completed();
246         Banker_Get_Matrix_Need();
247         Banker_Print_InitList();
248         Banker_Rest_Resource();
249         Banker_Get_All_Resource();
250         Banker_Print_Need();
251         if(Banker_Safety_Algorithm()){
252             Banker_Get_Request();
253             Banker_Print_Need();
254             Banker_Safety_Algorithm();
255         }else
256             cout<<"系统资源不足,禁止申请。"<<endl;
257         cout<<"您还要做什么事情吗?"<<endl;
258         ch=getchar();
259         ch=getchar();
260         system("cls");
261     }
262     cout<<"再见。"<<endl;
263     return 0;
264 }

 

转载于:https://www.cnblogs.com/ArtistofCodeMartial/p/8490974.html

你可能感兴趣的:(操作系统课程设计-银行家算法)