socket select 超时问题 导致CPU高
2017-05-24 10:30
573 查看
以下代码问题在哪里,会不会select每次1秒超时呢?
void IPCServer::DoWork()
{
fd_set read_set;
struct timeval tv_select;
tv_select.tv_sec = 1;
tv_select.tv_usec = 0;
int max_fd = m_nListenSocket;
int r = 0;
struct sockaddr_in addr_client;
socklen_t addr_len = sizeof(addr_client);;
char buffer[IPC_MAX_BUF_LEN] = {0};
while(true)
{
FD_ZERO(&read_set);
FD_SET(m_nListenSocket, &read_set);
r = select(max_fd + 1, &read_set, NULL, NULL, &tv_select);
}
}
man select
DESCRIPTION
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of
I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without
blocking.
The operation of select() and pselect() is identical, with three differences:
(i) select() uses a timeout that is a struct timeval (with seconds and microseconds), while pselect() uses a struct timespec (with seconds and nanoseconds).
(ii) select() may update the timeout argument to indicate how much time was left. pselect() does not change this argument.
每调用一次select timeout是上一次的剩余时间,即第二次都是为0,会导致什么问题呢,频繁调用导致CPU高。
(iii) select() has no sigmask argument, and behaves as pselect() called with NULL sigmask.
正确应该每次都重新赋值(或采用pselect)
struct timeval tv_select;
tv_select.tv_sec = 1;
tv_select.tv_usec = 0;
r = select(max_fd + 1, &read_set, NULL, NULL, &tv_select);
void IPCServer::DoWork()
{
fd_set read_set;
struct timeval tv_select;
tv_select.tv_sec = 1;
tv_select.tv_usec = 0;
int max_fd = m_nListenSocket;
int r = 0;
struct sockaddr_in addr_client;
socklen_t addr_len = sizeof(addr_client);;
char buffer[IPC_MAX_BUF_LEN] = {0};
while(true)
{
FD_ZERO(&read_set);
FD_SET(m_nListenSocket, &read_set);
r = select(max_fd + 1, &read_set, NULL, NULL, &tv_select);
}
}
man select
DESCRIPTION
select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of
I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform the corresponding I/O operation (e.g., read(2)) without
blocking.
The operation of select() and pselect() is identical, with three differences:
(i) select() uses a timeout that is a struct timeval (with seconds and microseconds), while pselect() uses a struct timespec (with seconds and nanoseconds).
(ii) select() may update the timeout argument to indicate how much time was left. pselect() does not change this argument.
每调用一次select timeout是上一次的剩余时间,即第二次都是为0,会导致什么问题呢,频繁调用导致CPU高。
(iii) select() has no sigmask argument, and behaves as pselect() called with NULL sigmask.
正确应该每次都重新赋值(或采用pselect)
struct timeval tv_select;
tv_select.tv_sec = 1;
tv_select.tv_usec = 0;
r = select(max_fd + 1, &read_set, NULL, NULL, &tv_select);
相关文章推荐
- odbcasvc.exe导致CPU使用100%问题的解决办法
- 一个关于java.net.Socket的超时的问题
- LINUX C语言 SOCKET 服务器断开导致客户端SEND崩溃问题解决办法
- 解决PCoIP连接View 5.0虚拟桌面超时断开导致无法再次登录的问题
- 【WCF】 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“00:00:56.9810000”。
- WCF 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的
- 使用upd socket(SOCK_DGRAM)的时候,recvform超时的问题
- 解决Linux Socket select总是返回1的问题
- Socket超时设置---select()的妙用
- VMware CPU speed导致的时间同步问题的解决
- [原]分析Vista导致资源管理器占用CPU资源100%的问题的原因及解决办法
- 停电导致IIS问题,解决inetinfo的CPU占用很大
- 停电导致IIS问题,解决inetinfo的CPU占用很大
- socket连接超时问题
- weblogic 出现 java.net.SocketException 异常可能是一个页面的小问题导致的。
- Socket超时设置---select()的妙用
- 停电导致IIS问题,解决inetinfo的CPU占用很大
- 由于游标打开数过多导致的CPU占用率过高的问题
- 0.net-关于socket的connect超时的问题
- Forefront for Exchange更新超时所导致的Exchange问题