Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 14180 - Luís Manuel Costa Pereira
- 14157 - Pedro Lopes Gonçalo
- */
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- //Exercicio 1
- /*
- void main()
- {
- int pidpai = getpid();
- if(fork())
- wait();
- else
- {
- if(fork())
- {
- wait();
- if(fork())
- wait();
- }
- }
- if(pidpai == getpid())
- {
- if(fork())
- wait();
- }
- printf("[Pai, Filho] = [%d, %d]\n", getppid(), getpid());
- }*/
- //Exercicio 2
- int main (int argc, char *argv[])
- {
- int pidpai=getpid(), i, status, erros=0, fe;
- for(i=1; i<argc; i++)
- {
- if(fork())
- {
- wait(&status);
- erros+=status>>8;
- }
- else
- {
- printf("PID=(%d) ficheiro %s\n", getpid(), argv[i]);
- close(0);
- fe=open(argv[i],O_RDONLY);
- if(fe < 0)
- exit(1);
- //dup(fe);
- //close(fe);
- printf("Nº de linhas do ficheiro %s: ", argv[i]);
- fflush(stdout);
- execlp("wc","wc","-l",NULL);
- printf("\n");
- exit(0);
- }
- }
- printf("Ficheiros: %d Erros: %d\n", argc-1, erros);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement