Skip to content

Wanderson-Morais/DEADLOOCKS-DO-GARGAMEL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

#include <stdio.h>
int main()
{
    // P0, P1, P2, P3, P4 são os nomes dos processos aqui
 
    int n, m, i, j, k;
    n = 5; // Numero de processos
    m = 3; // Numero de recursos
    
  int alloc[5][3] = { { 0, 1, 0 },     // P0  alocação da Matrix
                        { 2, 0, 0 },   // P1
                        { 3, 0, 2 },   // P2
                        { 2, 1, 1 },   // P3
                        { 0, 0, 2 } }; // P4
 
    int max[5][3] = { { 7, 5, 3 },   // P0  // MAX Matrix
                      { 3, 2, 2 },   // P1
                      { 9, 0, 2 },   // P2
                      { 2, 2, 2 },   // P3
                      { 4, 3, 3 } }; // P4
 
  int avail[3] = { 3, 3, 2 }; // Recursos disponíveis
 
    int f[n], ans[n], ind = 0;
    for (k = 0; k < n; k++) {
        f[k] = 0;
    }
    int need[n][m]; // calculo da necessidade
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++)
            need[i][j] = max[i][j] - alloc[i][j];
    }
    int y = 0;
    for (k = 0; k < 5; k++) {     //Verificação do Sistema
        for (i = 0; i < n; i++) {
            if (f[i] == 0) {
 
                int flag = 0;
                for (j = 0; j < m; j++) {
                    if (need[i][j] > avail[j]){
                        flag = 1;
                         break;
                    }
                }
 
                if (flag == 0) { // Adição aos recursos
                    ans[ind++] = i;
                    for (y = 0; y < m; y++)
                        avail[y] += alloc[i][y];
                    f[i] = 1;
                }
            }
        }
    }
   
      int flag = 1;
       
      for(int i=0;i<n;i++)
    {
      if(f[i]==0)
      {
        flag=0;
         printf("O seguinte sistema não é seguro");
        break;
      }
    }
     
      if(flag==1)
    {
      printf("Sequência SEGURA\n");
      for (i = 0; i < n - 1; i++)
        printf(" P%d ->", ans[i]);
      printf(" P%d", ans[n - 1]);
    }
     
 
    return (0);
 
   
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published