Guest User

Untitled

a guest
May 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/select.h>
  10.  
  11.  
  12. char buf[1024];
  13.  
  14.  
  15. int main(){
  16. int fd[2];
  17. int result;
  18. int acc = 0;
  19.  
  20. int the_end = 0;
  21. fd_set rd_set;
  22.  
  23. fd[0] = open("./in1", O_RDONLY | O_NONBLOCK);
  24. fd[1] = open("./in2", O_RDONLY | O_NONBLOCK);
  25. if(fd[0] == -1){
  26. printf("%sn", strerror(errno));
  27. exit(EXIT_FAILURE);
  28. }
  29. if(fd[1] == -1){
  30. printf("%sn",strerror(errno));
  31. exit(EXIT_FAILURE);
  32. }
  33.  
  34. while(1){
  35. FD_ZERO(&rd_set);
  36. FD_SET(fd[0], &rd_set);
  37. FD_SET(fd[1], &rd_set);
  38. result = select(fd[1] + 1, &rd_set, NULL, NULL, NULL);
  39. printf("result = %dn", result);
  40. if(result == -1){
  41. printf("%sn", strerror(errno));
  42. exit(EXIT_FAILURE);
  43. }
  44. if(result){
  45. if(FD_ISSET(fd[0], &rd_set)){
  46. result = read(fd[0], buf, 1024);
  47. if(result == -1){
  48. close(fd[0]);
  49. the_end++;
  50. } else if(result != 0) {
  51. buf[result] = 0;
  52. printf("from 1: %dn", atoi(buf));
  53. acc += atoi(buf);
  54. }
  55. FD_CLR(fd[0], &rd_set);
  56. }
  57. if(FD_ISSET(fd[1], &rd_set)){
  58. result = read(fd[1], buf, 1024);
  59. if(result == -1){
  60. close(fd[1]);
  61. the_end++;
  62. } else if(result != 0) {
  63. buf[result] = 0;
  64. printf("from 2: %dn", atoi(buf));
  65. acc += atoi(buf);
  66. }
  67. FD_CLR(fd[1], &rd_set);
  68. }
  69. }
  70. result = 0;
  71. if(the_end == 2){
  72. break;
  73. }
  74.  
  75. }
  76. printf("%dn", acc);
  77. return 0;
  78. }
  79.  
  80. fd[0] = open("./in1", O_RDONLY | O_NONBLOCK);
  81. fd[1] = open("./in2", O_RDONLY | O_NONBLOCK);
Add Comment
Please, Sign In to add comment