gpt4 book ai didi

c - epoll(EPOLLET)取决于读取字节数?

转载 作者:行者123 更新时间:2023-11-30 14:20:19 26 4
gpt4 key购买 nike

我在边缘触发模式下使用epoll。为了避免饥饿,代码从一个套接字一次读取 MAX_FREAD_LENGTH 字节。随后组装片段直至 EOL 发生。我注意到当 MAX_FREAD_LENGTH 很小时,epoll 会卡住。我认为它应该适用于任何大小的读取 block 。它在 512 字节下运行良好,但有时会挂起(意味着没有 EPOLLIN 事件)。如果我增加 MAX_FREAD_LENGTH 它会变得更加稳定。我该如何解决这个问题?

非常感谢您考虑我的问题!

EPOLL initialize

int res;
epFd = epoll_create(EPOLL_SIZE);
event.data.fd = serverFd;
event.events = EPOLLIN|EPOLLET;
res=epoll_ctl(epFd, EPOLL_CTL_ADD, serverFd, &event);
if (res == -1){
perror ("epoll_ctl error: ");
return EH_ERROR;
}
events = calloc (MAX_EVENTS, sizeof event);

register net event:

while (TRUE){
int nfds;
do{
nfds = epoll_wait(epFd, events, MAX_EVENTS, -1);
} while (nfds < 0 && errno == EINTR);

int i = 0;
for (;i<nfds;i++){
if ( (events[i].data.fd == serverFd) && (events[i].events & EPOLLIN)){
if ((clientFd = accept(serverFd,(struct sockaddr *) &clientAddr, &clientLen)) < 0){
char log[255];
sprintf(log,"dispatch_net_event: Socket accept failed: %s",strerror(errno));
logger->log(LOGG_ERROR,log);
}
if(newclient(clientFd)!=EH_ERROR){
/* client created */
setnonblocking(fd,NONBLOCKING);
event.data.fd = clientFd;
event.events = EPOLLIN |EPOLLET;
if(epoll_ctl(epFd, EPOLL_CTL_ADD, fd, &event)<0){
fprintf(stderr,"Epoll insertion error (fd=%d): ",clientFd);
return EH_ERROR;
}
continue;
}
else{
logger->log(LOGG_ERROR,"Client creation error");
continue;
}
}
else{
dispatch_event(events[i].data.fd,NET_EVENT);
}
}
}

handle a net event

#define SMTP_MAX_LINE_LENGTH MAX_FREAD_LENGTH
ssize_t count;
char buf[SMTP_MAX_LINE_LENGTH];

memset(buf,'\0', SMTP_MAX_LINE_LENGTH);
count = read (fd, buf,MAX_FREAD_LENGTH );

if (count==-1){
if (errno == EAGAIN)
return KEEP_IT;
else if (errno == EWOULDBLOCK)
return KEEP_IT;
else{
char log[255];
sprintf(log,"handle_net_event: Read error: %s",strerror(errno));
logger->log(LOGG_ERROR,log);
}
}
else{ /* count > 0 there are data in the buffer */
/* assemble possible partial lines, TRUE if line is whole (end with \r\n)*/
whole=assemble_line(count,client,&buf[0]);
}
/* process the line */

EDIT:

我忘了提及,epoll 与其他部分相比在单独的线程中运行

最佳答案

我认为您错误地使用了 EPOLLET。尝试不使用 EPOLLET(即使用电平触发模式),看看是否有效。如果是这样,则意味着您的根本问题是您没有执行边沿触发模式所要求的操作,即继续从任何就绪描述符中读取,直到获得 EAGAIN。

关于c - epoll(EPOLLET)取决于读取字节数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15569845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com