document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ----------------------------------------------------------------------------
  2.  Title - Write a program to implement Banker’s Algorithm
  3. ----------------------------------------------------------------------------
  4.  
  5. ------------------------
  6. MAIN PROGRAM a9.c
  7. ------------------------
  8.  
  9. #include<stdio.h>
  10. #include<conio.h>
  11.  
  12. void main()
  13. {
  14.     int max[20][20],allo[20][20],need[20][20],i,j,k,np,ava[10],ava1[20],sum,nr;
  15.     int m,rem,flag,sequence[20],m1,flag1=1;
  16.  
  17.     clrscr();
  18.  
  19.     printf("\\n Enter the No. of Processor : ");
  20.     scanf("%d",&np);
  21.  
  22.     printf("\\n Enter the No. of Resoucres : ");
  23.     scanf("%d",&nr);
  24.  
  25.     /*printf("\\nEnter the process name: ");
  26.     for(i=0;i<np;i++)
  27.     {
  28.         scanf("%s",&p[i]);
  29.         }*/
  30.  
  31.     /* printf("\\nEnter the resources name: ");
  32.     for(j=0;j<nr;j++)
  33.     {
  34.         scanf("%s",&r[j]);
  35.         }*/
  36.  
  37.     printf("\\n Enter the No. of instance for each Resources : ");
  38.     for(i=0;i<nr;i++)
  39.     {
  40.         printf("\\n Resource r%d : ",i);
  41.         scanf("%d",&ava[i]);
  42.         }
  43.  
  44.     /*printf("\\nInstance for each resources: ");
  45.     for(j=0;j<nr;j++)
  46.     {
  47.         printf("\\nResource r%d: ",j);
  48.         printf("%d\\t",ava[j]);
  49.         }*/
  50.  
  51.         for(i=0;i<np;i++)
  52.         {
  53.             printf("\\n\\n Enter the Maximum Resources for Process p%d : ",i);
  54.             for(j=0;j<nr;j++)
  55.             {
  56.                 printf("\\n Resource r%d : ",j);
  57.                 scanf("%d",&max[i][j]);
  58.                 }
  59.             }
  60.  
  61.         printf("\\n\\n Max. Matrix : \\n");
  62.         for(i=0;i<np;i++)
  63.         {
  64.             for(j=0;j<nr;j++)
  65.             {
  66.                 printf(" %d \\t",max[i][j]);
  67.                 }
  68.             printf("\\n");
  69.             }
  70.  
  71.         for(i=0;i<np;i++)
  72.         {
  73.             printf("\\n\\n Enter the Allocation Resources for Process p%d : ",i);
  74.             for(j=0;j<nr;j++)
  75.             {
  76.                 flag1=1;
  77.                 while(flag1==1)
  78.                 {
  79.                     printf("\\nResource r%d: ",j);
  80.                     scanf("%d",&m1);
  81.                     if(m1<=max[i][j])
  82.                     {
  83.                         allo[i][j]=m1;
  84.                         flag1=0;
  85.                         }
  86.                     else
  87.                     {
  88.                         printf("\\n Enter Value less than the Max value..");
  89.                         flag1=1;
  90.                         }
  91.                     }
  92.                    //   allo[i][j]=m1;
  93.                 }
  94.             }
  95.  
  96.         printf("\\n\\n Allocation Matrix : \\n");
  97.         for(i=0;i<np;i++)
  98.         {
  99.             for(j=0;j<nr;j++)
  100.             {
  101.                 printf(" %d \\t",allo[i][j]);
  102.                 }
  103.             printf("\\n");
  104.             }
  105.  
  106.         for(i=0;i<np;i++)
  107.         {
  108.             for(j=0;j<nr;j++)
  109.             {
  110.                 need[i][j]=max[i][j]-allo[i][j];
  111.                 }
  112.             }
  113.  
  114.         printf("\\n\\n Need Matrix : \\n");
  115.         for(i=0;i<np;i++)
  116.         {
  117.             for(j=0;j<nr;j++)
  118.             {
  119.                 printf(" %d \\t",need[i][j]);
  120.                 }
  121.             printf("\\n");
  122.             }
  123.  
  124.         printf("\\n\\n Available Resources : ");
  125.         for(j=0;j<nr;j++)
  126.         {
  127.             sum=0;
  128.             for(i=0;i<np;i++)
  129.             {
  130.                 sum=sum+allo[i][j];
  131.                 }
  132.             ava1[j]=ava[j]-sum;
  133.             printf("\\t%d",ava1[j]);
  134.             }
  135.  
  136.         for(k=0;k<np;k++)
  137.         {
  138.             for(i=0;i<np;i++)
  139.             {
  140.                 m=0;
  141.                 for(j=0;j<nr;j++)
  142.                 {
  143.                     if(need[i][j]!=-1 && need[i][j]<=ava1[m])
  144.                     {
  145.                         flag=1;
  146.                         rem=i;
  147.                         }
  148.                     else
  149.                     {
  150.                         flag=0;
  151.                         break;
  152.                         }
  153.                     m++;
  154.                     }
  155.                 if(flag==1)
  156.                     break;
  157.                 }
  158.  
  159.              printf("\\n\\n p%d Executed ",rem);
  160.              sequence[k]=rem;
  161.  
  162.              if(flag==1)
  163.              {
  164.                  for(j=0;j<nr;j++)
  165.                  {
  166.                     need[rem][j]=-1;
  167.                     }
  168.  
  169.                  printf("\\n\\n Need matrix : \\n");
  170.                  for(i=0;i<np;i++)
  171.                  {
  172.                     for(j=0;j<nr;j++)
  173.                     {
  174.                         printf("%d\\t",need[i][j]);
  175.                         }
  176.                     printf("\\n");
  177.                     }
  178.  
  179.                  printf("\\n\\nAvailable resources: ");
  180.                  for(j=0;j<nr;j++)
  181.                  {
  182.                     ava1[j]=ava1[j]+allo[rem][j];
  183.                     printf("\\t %d ",ava1[j]);
  184.                     }
  185.                 }
  186.             }
  187.  
  188.         if(flag==1)
  189.         {
  190.             printf("\\n\\n\\nSafe sequence is:\\n");
  191.             for(j=0;j<np;j++)
  192.             printf("p%d\\t ",sequence[j]);
  193.             }
  194.         printf("\\n");
  195.  
  196.         if(flag==0)
  197.         printf("\\nresource not available unsafe state\\n");
  198.  
  199.     getch();
  200.     }
  201.  
  202.  
  203. ------------------------
  204. ENND OF THE PROGRAM
  205. ------------------------
  206. /*
  207. ------------------------
  208.     OUTPUT
  209. ------------------------
  210.  
  211.  
  212. mca@mca-VirtualBox:~/Desktop$ gcc a9a.c
  213. mca@mca-VirtualBox:~/Desktop$ ./a.out
  214.  
  215.  
  216.  Enter the No. of Processor : 4
  217.  
  218.  Enter the No. of Resoucres : 3
  219.  
  220.  Enter the No. of instance for each Resources :
  221.  Resource r0 : 9
  222.  
  223.  Resource r1 : 3
  224.  
  225.  Resource r2 : 6
  226.  
  227.  
  228.  Enter the Maximum Resources for Process p0 :
  229.  Resource r0 : 3
  230.  
  231.  Resource r1 : 2
  232.  
  233.  Resource r2 : 2
  234.  
  235.  Enter the Maximum Resources for Process p1 :
  236.  Resource r0 : 6
  237.  
  238.  Resource r1 : 1
  239.  
  240.  Resource r2 : 3
  241.  
  242.  Enter the Maximum Resources for Process p2 :
  243.  Resource r0 : 3
  244.  
  245.  Resource r1 : 1
  246.  
  247.  Resource r2 : 4
  248.  
  249.  Enter the Maximum Resources for Process p3 :
  250.  Resource r0 : 4
  251.  
  252.  Resource r1 : 2
  253.  
  254.  Resource r2 : 2
  255.  
  256.  Max. Matrix :
  257.  3       2       2
  258.  6       1       3
  259.  3       1       4
  260.  4       2       2
  261.  
  262.  Enter the Allocation Resources for Process p0 :
  263. Resource r0: 1
  264.  
  265. Resource r1: 0
  266.  
  267. Resource r2: 0
  268.  
  269.  Enter the Allocation Resources for Process p1 :
  270. Resource r0: 6
  271.  
  272. Resource r1: 1
  273.  
  274. Resource r2: 2
  275.  
  276.  Enter the Allocation Resources for Process p2 :
  277. Resource r0: 2
  278.  
  279. Resource r1: 1
  280.  
  281. Resource r2: 1
  282.  
  283.  Enter the Allocation Resources for Process p3 :
  284. Resource r0: 0
  285.  
  286. Resource r1: 0
  287.  
  288. Resource r2: 2
  289.  
  290. Allocation matrix :
  291. 1       0       0
  292. 6       1       2
  293. 2       1       1
  294. 0       0       2
  295.  
  296. Need matrix :
  297. 2       2       2
  298. 0       0       1
  299. 1       0       3
  300. 4       2       0
  301.  
  302.  Available Resources :  0       1       1
  303.  
  304.  p1 Executed
  305.  
  306.  Need matrix :
  307. 2       2       2
  308. -1      -1      -1
  309. 1       0       3
  310. 4       2       0
  311.  
  312. Available resources:     6       2       3
  313.  
  314.  p0 Executed
  315.  
  316.  Need matrix :
  317. -1      -1      -1
  318. -1      -1      -1
  319. 1       0       3
  320. 4       2       0
  321.  
  322. Available resources:     7       2       3
  323.  
  324.  p2 Executed
  325.  
  326.  Need matrix :
  327. -1      -1      -1
  328. -1      -1      -1
  329. -1      -1      -1
  330. 4       2       0
  331.  
  332. Available resources:     9       3       4
  333.  
  334.  p3 Executed
  335.  
  336.  Need matrix :
  337. -1      -1      -1
  338. -1      -1      -1
  339. -1      -1      -1
  340. -1      -1      -1
  341.  
  342. Available resources:     9       3       6
  343.  
  344. Safe sequence is:
  345. p1       p0      p2      p3
  346.  
  347. ------------------------
  348. EXIT
  349. ------------------------
  350. */
');