0

原谅我的长篇。我会发布代码,以便更容易理解我面临的问题。似乎如果在 epoll 实例中添加了有信号的套接字,则 epoll 实例上的 epoll_wait 不会阻塞。下面的例子让我相信了这一点:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <sys/epoll.h>

int MAX_EVENT_COUNT = 10;

int main(int argc, char *argv[])
{

int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}

epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;

int pipefd[2];
if (pipe(pipefd) == -1) 
{
perror("pipe");
exit(EXIT_FAILURE);
}

char ch = 'a';
write(pipefd[1], &ch, 1);

if (epoll_ctl( epollfd , EPOLL_CTL_ADD, pipefd[0], &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}

epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );

if(res!=1)
{
printf("\n Epoll problem \n");
}
else
{
printf("\n OK \n");
}

}

然而下面的例子几乎相同,但这次我向 epoll 添加了一个信号套接字,这一次 epoll_wait 调用阻塞

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h> 
#include <sys/epoll.h>

int MAX_EVENT_COUNT = 10;

int main(int argc, char *argv[])
{
int listenfd = 0;
{
struct sockaddr_in serv_addr; 

char sendBuff[1025];

listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff)); 

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000); 

bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 

listen(listenfd, 10); 
}


int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr; 

memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
} 

memset(&serv_addr, '0', sizeof(serv_addr)); 

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000); 

if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
    } 

if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
} 

int connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); 
int epollfd = epoll_create( MAX_EVENT_COUNT );
if ( epollfd == -1 )
{
printf("\n Error : epoll_create \n");
return 1;
}

epoll_event ev;
memset(&ev,0,sizeof(ev));
ev.events = EPOLLIN | EPOLLET;

if (epoll_ctl( epollfd , EPOLL_CTL_ADD, sockfd, &ev ) == -1 )
{
printf("\n Error : epoll add result \n");
return 1;
}

epoll_event rawResult [MAX_EVENT_COUNT];
int32_t res = epoll_wait( epollfd, rawResult, MAX_EVENT_COUNT, -1 );

}
4

1 回答 1

2

epoll_wait()如果它正在等待的文件描述符没有要报告的事件,将阻塞。在你的情况下,我不相信你已经达到epoll_wait()了。假设你的程序已经通过connect()accept(),那么你还没有在连接上写入任何数据(在 上sockfd),所以epoll_wait()不会检测到 上的任何事件connfd

这与您的第一个程序不同,在第一个程序中,您pipefd[1]在调用之前写入一个字节epoll_wait()的数据pipefd[0]

于 2013-06-21T15:06:21.310 回答