Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. /*
  2. ** UC: 21111 -Sistemas Operativos
  3. ** e-fólio A 2019-20 (ab.c)
  4. **
  5. ** Aluno: 1802614 - Pedro N. Silva
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <ctype.h>
  14.  
  15. void arvore (int n, int i);
  16.  
  17. int main (int argc, char *argv[])
  18. {
  19. /*
  20.   Ponto de entrada do processo ab, responsavel por validar os parametros de entrada e devolver erro caso não sejam válidos.
  21.   Se tudo estiver correcto invoca a função principal ab(n)
  22. */
  23.     if ( argc!= 2 )
  24.     {
  25.         printf("Deve invocar o processo da seguinte forma: ./abs <n>\n");
  26.         printf(" n - nº de processos filhos das cadeias A e B , com 0 ≤ n ≤ 32\n");
  27.        
  28.         exit(1);
  29.        
  30.         }
  31.  
  32. // sscanf para solicitar o argumento de entrada de string para inteiro
  33. // argv[0] - Nome do programa
  34. // argv[1] - Argumento n
  35.  
  36.     // Validacao do n
  37.     int n;
  38.     if ( sscanf( argv[1], "%d", &n ) != 1 )
  39.     {
  40.         printf("Erro escrever um numero inteiro\n");
  41.        
  42.         exit(1);
  43.        
  44.         }
  45.        
  46.     if ( n < 0 || n > 32 )
  47.     {
  48.         printf("Valor incorrecto de n (0<=n<=32)\n");
  49.        
  50.         exit(1);
  51.        
  52.         }
  53.        
  54.         return ab (n);
  55. }
  56.  
  57.  
  58. void arvore(int n, int i)
  59.     {
  60.         pid_t pid;
  61.        
  62.         if (0)
  63.         {
  64. //imprime ID de pai        
  65.             printf("Cadeia de processos em V invertido com n=\n");
  66.             printf("Processo AB tem PID=%5d\n", (int) getpid());
  67.            
  68.             sleep(3);
  69.            
  70.         for(i=1; i<=n; i++)
  71.         {
  72.             pid = fork();
  73.            
  74.             if(pid==-1)
  75.             {
  76.                 perror("Erro na funcao fork()");
  77.                
  78.                 exit(1);
  79.                
  80.                 }
  81.                 else if(pid>=0)
  82.                 {
  83.                     pid_t filho_pid = fork();
  84.                    
  85.                     sleep(1);
  86.                    
  87.                     if(filho_pid==-1)
  88.                     {
  89.                         perror("Erro na funcao fork()");
  90.                         exit(1);
  91. //cria programa neto
  92.   } else if(filho_pid>=0) {
  93. //imprime programa neto
  94.          printf("Processo B%d :  PID=%5d e PPID=%5d\n",\
  95.             i, (int) getpid(), (int) getppid());
  96.          //wait(NULL);
  97.         exit(0);
  98.     }
  99. //imprime programa filho                      
  100.          printf("Processo A%d :  PID=%5d e PPID=%5d\n",\
  101.             i, (int) getpid(), (int) getppid());
  102.            sleep(1);  
  103.     exit(1);
  104.      }
  105.    sleep(4);
  106.    }
  107.    return;
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement