gpt4 book ai didi

c - epoll_wait 在有信号的套接字上阻塞

转载 作者:太空宇宙 更新时间:2023-11-04 08:55:42 26 4
gpt4 key购买 nike

请原谅我的长篇文章。我会发布代码,以便更容易理解我面临的问题。似乎如果将信号套接字添加到 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 调用 block

#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 );

}

最佳答案

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

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

关于c - epoll_wait 在有信号的套接字上阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17237704/

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