Advertisement
AJBF

Ej 7

Apr 6th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<unistd.h>
  3. #include<stdio.h>
  4. #include<sys/stat.h>
  5. #include<fcntl.h>
  6. #include <sys/types.h>
  7. #include <strings.h>
  8.  
  9. int main (void)
  10. {
  11. int pid1, pid2, nbytes, descriptor, flag;
  12. int d_pipe[2];
  13. char buffer[80];
  14. char readbuffer[80];
  15. pipe(d_pipe);
  16.  
  17.  
  18. pid1 = fork();
  19.  
  20. if(pid1 == -1)
  21. {
  22. printf("ERROR 404");
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. if(pid1 == 0)
  27. {
  28.  
  29. close(d_pipe[0]);
  30. if ((descriptor = open ("algunombre.text", O_CREAT, 0777)) < 0)
  31.  
  32. {
  33. printf("ERROR OPEN");
  34. exit(EXIT_FAILURE);
  35.  
  36. }
  37.  
  38. flag = read(descriptor, &buffer, 5);
  39. write(d_pipe[1], buffer, 6);
  40. close(d_pipe[1]);
  41. exit(EXIT_SUCCESS);
  42. }
  43. else
  44. {
  45. pid2 = fork();
  46. if(pid2 == -1)
  47. {
  48. printf("EEROROOROARFSADF");
  49. exit(EXIT_FAILURE);
  50. }
  51. if(pid2 == 0)
  52. {
  53. close(d_pipe[1]);
  54. nbytes = read(d_pipe[0], readbuffer, sizeof(readbuffer));
  55. printf("Mira recibiste algo: %s\n", readbuffer);
  56. exit(EXIT_SUCCESS);
  57. }
  58. else
  59. {
  60. waitpid(pid1);
  61. waitpid(pid2);
  62. exit(EXIT_SUCCESS);
  63. }
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement