Advertisement
AJBF

ej7 while

Apr 13th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 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, cond;
  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 ("algunnombre.text", O_CREAT, 0777)) < 0)
  31.  
  32. {
  33. printf("ERROR OPEN");
  34. exit(EXIT_FAILURE);
  35.  
  36. }
  37.  
  38.  
  39. while ((flag = read(descriptor, &buffer, 6)) != 0)
  40. {
  41.  
  42. write(d_pipe[1], buffer, flag);
  43. //flag = read(descriptor, &buffer, 6);
  44. //printf("%d", flag);
  45.  
  46.  
  47. }
  48. sprintf(buffer,"%s\n","fin");
  49. write(d_pipe[1], buffer, flag);
  50. close(d_pipe[1]);
  51. close(descriptor);
  52. exit(EXIT_SUCCESS);
  53. }
  54. else
  55. {
  56. pid2 = fork();
  57.  
  58.  
  59. if(pid2 == -1)
  60. {
  61. printf("EEROROOROARFSADF");
  62. exit(EXIT_FAILURE);
  63. }
  64. if(pid2 == 0)
  65. {
  66. close(d_pipe[1]);
  67.  
  68. while ((nbytes = read(d_pipe[0], &readbuffer, 6)) == 6)
  69. {
  70.  
  71. printf("Mira recibiste algo: %s con %d bytes\n", readbuffer,nbytes);
  72. //nbytes = read(d_pipe[0], readbuffer, sizeof(readbuffer));
  73. //printf("%d", nbytes);
  74. }
  75. printf("Mira recibiste algo: %s con %d bytes\n", readbuffer,nbytes);
  76. printf("TTTEERRMMIINNEE!!\n");
  77. close(d_pipe[0]);
  78. //NO SALE DEL WHILE
  79.  
  80. exit(EXIT_SUCCESS);
  81. }
  82.  
  83. waitpid(pid1);
  84. waitpid(pid2);
  85.  
  86.  
  87.  
  88.  
  89. }
  90. exit(EXIT_SUCCESS);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement