I got next question about select: How to make select in loop ?
I try to do like that:
struct timeval timeout; int sel; size_t rozmiar = sizeof(pid_t); char buf[rozmiar]; int i; FD_ZERO(&set); for(i = 0; i< val; i++) { FD_SET(fd[i][0], &set); // val -> N pipe2 } timeout.tv_sec = 2; timeout.tv_usec = 0; while(1) { sel = select(val+1,&set,NULL,NULL,&timeout); if(sel < 0) perror("select"); else if(sel == 0) printf("No communicate n"); else{ for(i = 0; i < val; i++) { if(FD_ISSET(fd[i][0],&set)) { while(read(fd[i][0],&buf,rozmiar) > 0) write(1,&buf,rozmiar); } // check if exist and write to stdout } } // end SELECT timeout.tv_sec = 2; timeout.tv_usec = 0; }
But there all the time show: ,, no communicate”. Is it the correct way to create select which work all the time? I am not sure so I prefer to ask. I try to find information in books but with no lucky.
Advertisement
Answer
The set is changed by select
, you need to refill it each time