Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/select.h>
- char buf[1024];
- int main(){
- int fd[2];
- int result;
- int acc = 0;
- int the_end = 0;
- fd_set rd_set;
- fd[0] = open("./in1", O_RDONLY | O_NONBLOCK);
- fd[1] = open("./in2", O_RDONLY | O_NONBLOCK);
- if(fd[0] == -1){
- printf("%sn", strerror(errno));
- exit(EXIT_FAILURE);
- }
- if(fd[1] == -1){
- printf("%sn",strerror(errno));
- exit(EXIT_FAILURE);
- }
- while(1){
- FD_ZERO(&rd_set);
- FD_SET(fd[0], &rd_set);
- FD_SET(fd[1], &rd_set);
- result = select(fd[1] + 1, &rd_set, NULL, NULL, NULL);
- printf("result = %dn", result);
- if(result == -1){
- printf("%sn", strerror(errno));
- exit(EXIT_FAILURE);
- }
- if(result){
- if(FD_ISSET(fd[0], &rd_set)){
- result = read(fd[0], buf, 1024);
- if(result == -1){
- close(fd[0]);
- the_end++;
- } else if(result != 0) {
- buf[result] = 0;
- printf("from 1: %dn", atoi(buf));
- acc += atoi(buf);
- }
- FD_CLR(fd[0], &rd_set);
- }
- if(FD_ISSET(fd[1], &rd_set)){
- result = read(fd[1], buf, 1024);
- if(result == -1){
- close(fd[1]);
- the_end++;
- } else if(result != 0) {
- buf[result] = 0;
- printf("from 2: %dn", atoi(buf));
- acc += atoi(buf);
- }
- FD_CLR(fd[1], &rd_set);
- }
- }
- result = 0;
- if(the_end == 2){
- break;
- }
- }
- printf("%dn", acc);
- return 0;
- }
- fd[0] = open("./in1", O_RDONLY | O_NONBLOCK);
- fd[1] = open("./in2", O_RDONLY | O_NONBLOCK);
Add Comment
Please, Sign In to add comment