Chinaunix首页 | 论坛 | 博客
  • 博客访问: 51768
  • 博文数量: 9
  • 博客积分: 1607
  • 博客等级: 一等列兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-05 22:18
文章分类

全部博文(9)

文章存档

2011年(9)

分类: C/C++

2011-08-13 11:39:57

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<time.h>
  4. #define randomize() srand((unsigned)time(NULL)) //定义一个宏
  5. #define uint unsigned int
  6. #define uchar unsigned char
  7. #define RES 3 //系统资源数
  8. #define MAX_P 5 //进程数
  9.   
  10. //PCB结构体定义部分
  11. typedef struct PCB
  12. {
  13.     /* 进程标识符 */
  14.     uint id;
  15.     /* 每一个进程需求M类资源的最大数目 */
  16.     uint max[RES];
  17.     /* 已分配的资源数 */
  18.     uint allocation[RES];
  19.     /* 当前进程还需要的资源个数 */
  20.     uint need[RES];
  21.     /* 当前进程请求资源 */
  22.     uint request[RES];
  23.     /* 标志进程是否可执行完成 */
  24.     uint finish;
  25. };
  26.   
  27. /* 系统中R资源的个数 */
  28. uint available[RES];
  29. uint work[RES];
  30. uchar finish[RES],flag;
  31. struct PCB process[MAX_P];
  32.   
  33. void init(void);
  34. bool check(void);
  35. void out();
  36. void main(void) ///////////主函数/////////////
  37. {
  38.     int i,j;
  39.     init();
  40.     printf("===============banker===============\n");
  41.     out();
  42.     while(!flag)
  43.     {
  44.         randomize(); //随机函数
  45.         for(i=0;i<MAX_P;i++)
  46.         {
  47.             process[i].finish = 0;
  48.             for(j=0;j<RES;j++)
  49.             {
  50.                 process[i].request[j] = rand()%5;//i进程申请j资源随机个
  51.                 if(process[i].request[j] < process[i].need[j])
  52.                     if(process[i].request[j] < available[j])
  53.                     {
  54.                         work[j] = available[j];
  55.                         //finish = 0;
  56.                         if(check()) //安全性检查
  57.                         {
  58.                             available[j] = available[j] - process[i].request[j];
  59.                             process[i].allocation[j] = process[i].allocation[j] + process[i].request[j];
  60.                             process[i].need[j] = process[i].need[j] - process[i].request[j];
  61.                             printf("distribute....\n");
  62.                             out();
  63.                             flag = 1;
  64.                         }
  65.                         else
  66.                             printf("wait...\n");
  67.                     }
  68.                     else
  69.                         printf("wait...\n");
  70.                 else
  71.                     printf(".....error...\n");
  72.             }
  73.         }
  74.         for(i=0;i<MAX_P;i++)
  75.             for(j=0;j<RES;j++)
  76.             {
  77.                    
  78.             }
  79.     }
  80. }
  81. void out(void)
  82. {
  83.     //printf("##############资源情况###################\n");
  84.     printf("\n进程\tMax\tAllocation\tNeed\tAvailable\tFinish\n");
  85.     printf("----------------------------------------------------------------|\n");
  86.     int i;
  87.     for(i=0;i<MAX_P;i++)
  88.     {
  89.         printf("%d\t%d,%d,%d\t%d,%d,%d\t\t%d,%d,%d\t%d,%d,%d\t\t%d |\n",process[i].id,process[i].max[0],process[i].max[1],process[i].max[2],process[i].allocation[0],process[i].allocation[1],process[i].allocation[2],process[i].need[0],process[i].need[1],process[i].need[2],available[0],available[1],available[2],process[i].finish);
  90.         if(i==4)
  91.             printf("----------------------------------------------------------------|\n");
  92.     }
  93. }
  94. /* 安全性检查 */
  95. bool check(void)
  96. {
  97.     int i,j;
  98.     for(i=0;i<MAX_P;i++)
  99.         for(j=0;j<RES;j++)
  100.         {
  101.             if(!process[i].finish && process[i].need[j]<=work[j])
  102.             {
  103.                 work[j] += process[i].allocation[j];
  104.                 process[i].finish = 1;
  105.                 printf("...not safe....\n");
  106.             }
  107.             else
  108.                 printf("...not safe....\n");
  109.         }
  110.     out();
  111.     return true;
  112. }
  113. /** 初始化 **/
  114. void init(void)
  115. {
  116.     int i = 0;
  117.     int j = 0;
  118.     flag = 0;
  119.     randomize();
  120.     //随机产生各资源的个数
  121.     for(i=0;i<RES;i++)
  122.         available[i] = rand()%5+6;
  123.     //初始化各进程
  124.     for(i=0;i<MAX_P;i++)
  125.     {
  126.         //初始化进程ID
  127.         process[i].id = i+1;
  128.         //各进程对资源的最大需求,随机产生:0~10
  129.         //已分配0个资源
  130.         //请求资源0个
  131.         //尚需 max - allocation
  132.         for(j=0;j<RES;j++)
  133.         {
  134.             process[i].max[j] = rand()%10;
  135.             process[i].allocation[j] = 0;
  136.             process[i].request[j] = 0;
  137.             process[i].need[j] = process[i].max[j]-process[i].allocation[j];
  138.         }
  139.     }
  140. }
阅读(1760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~