Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <signal.h>
  6. #include <fcntl.h>
  7. #include <sys/wait.h>
  8. #include <errno.h>
  9.  
  10. int main(int argc, char *argv[]){
  11.  
  12. int pid, fd[2], estado;
  13. int buf[256];
  14. creat(argv[2], 7777);
  15. //printf("%dn", f);
  16.  
  17. pipe(fd);
  18.  
  19. int nhijos = *argv[1] -'0';
  20. for(int i=0; i<nhijos; i++){
  21. switch(fork()){
  22. case 0: //Hijo
  23. close (fd[0]);
  24. close (1);
  25. dup(fd[1]);
  26. close(fd[1]);
  27. pid = getpid();
  28. buf[1]= pid;
  29. write(1, buf, 2);
  30. write(1, "n", 1);
  31. exit(0);
  32. case -1:
  33. printf("fork");
  34. default: //Padre
  35. close (fd[1]);
  36. close (0);
  37. dup(fd[0]);
  38. close(fd[0]);
  39. read(0, buf, 3);
  40. write(3, buf, 3);
  41. int err;
  42. err = wait(&estado);
  43. }
  44. }
  45. printf("Termino la ejecucion n");
  46. exit(0);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement