select () 호출에 남아있는 시간
문제
사용 중입니다 select()
Linux/ARM 플랫폼에서 UDP 소켓이 패킷을 받았는지 확인하십시오. 타임 아웃 전에 돌아 오면 (패킷을 감지 한) 선택 통화에 얼마나 많은 시간이 남아 있는지 알고 싶습니다.
라인을 따라 무언가 :
int wait_fd(int fd, int msec)
{
struct timeval tv;
fd_set rws;
tv.tv_sec = msec / 1000ul;
tv.tv_usec = (msec % 1000ul) * 1000ul;
FD_ZERO( & rws);
FD_SET(fd, & rws);
(void)select(fd + 1, & rws, NULL, NULL, & tv);
if (FD_ISSET(fd, &rws)) { /* There is data */
msec = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
return(msec?msec:1);
} else { /* There is no data */
return(0);
}
}
해결책
가장 안전한 것은 모호한 정의를 무시하는 것입니다. select()
그리고 시간을 스스로하십시오.
선택 전후에 시간을 보내고 원하는 간격에서 그 시간을 빼십시오.
다른 팁
올바르게 기억하면 select () 함수는 타임 아웃과 I/O 매개 변수를 취급하고 select를 반환하면 나머지 시간이 시간 초과 변수에서 리턴됩니다.
그렇지 않으면, 당신은 호출하기 전에 현재 시간을 기록하고 다시 한 번 후에 두 가지 사이의 차이를 얻어야합니다.
OSX에서 "Man Select"에서 :
Timeout is not changed by select(), and may be reused on subsequent calls, however it
is good style to re-ini-tialize it before each invocation of select().
Select에게 전화하기 전에 gettimeofday에 전화 한 다음 출구에서 GetTimeofday를 방문해야합니다.
편집] Linux가 약간 다른 것 같습니다.
(ii) The select function may update the timeout parameter to indicate
how much time was left. The pselect function does not change
this parameter.
On Linux, the function select modifies timeout to reflect the amount of
time not slept; most other implementations do not do this. This causes
problems both when Linux code which reads timeout is ported to other
operating systems, and when code is ported to Linux that reuses a
struct timeval for multiple selects in a loop without reinitializing
it. Consider timeout to be undefined after select returns.
Linux Select ()는 시간 초과 인수를 업데이트하여 과거의 시간을 반영합니다.
이것은 다른 시스템에서 휴대 할 수 없지만 (위의 OS X 매뉴얼의 경고)는 Linux에서 작동합니다.
길라드
Select를 사용하지 말고 코드를 사용하여 1024보다 큰 FD로 시도하고 무엇을 얻을 수 있는지 확인하십시오.
제휴하지 않습니다 StackOverflow