Advertisement
Kimossab

SO TP1

Mar 26th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. /*
  2.  
  3.     14180 - Luís Manuel Costa Pereira
  4.     14157 - Pedro Lopes Gonçalo
  5.  
  6. */
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11.  
  12. //Exercicio 1
  13. /*
  14. void main()
  15. {
  16.     int pidpai = getpid();
  17.     if(fork())
  18.         wait();
  19.     else
  20.     {
  21.         if(fork())
  22.         {
  23.             wait();
  24.             if(fork())
  25.                 wait();
  26.         }
  27.     }
  28.     if(pidpai == getpid())
  29.     {
  30.         if(fork())
  31.         wait();
  32.     }
  33.     printf("[Pai, Filho] = [%d, %d]\n", getppid(), getpid());
  34. }*/
  35. //Exercicio 2
  36. int main (int argc, char *argv[])
  37. {
  38.     int pidpai=getpid(), i, status, erros=0, fe;
  39.     for(i=1; i<argc; i++)
  40.     {
  41.         if(fork())
  42.         {
  43.             wait(&status);
  44.             erros+=status>>8;
  45.         }
  46.         else
  47.         {
  48.             printf("PID=(%d) ficheiro %s\n", getpid(), argv[i]);
  49.             close(0);
  50.             fe=open(argv[i],O_RDONLY);
  51.             if(fe < 0)
  52.                 exit(1);
  53.            
  54.             //dup(fe);
  55.             //close(fe);
  56.             printf("Nº de linhas do ficheiro %s: ", argv[i]);
  57.             fflush(stdout);
  58.             execlp("wc","wc","-l",NULL);
  59.             printf("\n");
  60.             exit(0);
  61.         }
  62.     }
  63.     printf("Ficheiros: %d Erros: %d\n", argc-1, erros);
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement