我使用epoll和非阻塞管道将消息从父级传递到子级。当我的请求计数较低时,该代码有效。但是,当我在n = 100000和-c 10的linux中用ab命令对其进行测试时,我检测到使用strace对管道挂起的写入。如果n小于10000,它将起作用。
我不确定原因。管道过大是否需要做些什么?
while(1)
{
int status = -1;
int n = epoll_wait (efd, ttlevents, MAXEVENTS, -1);
for(int i=0;i<n;i++)
{
if(ttlevents[i].data.fd == *fdptr) // New Connection
{
while(1){
if(acceptNewConnection(fdptr,efd) == -1)
break;
}
}
else
{
char databuffer[1024];
memset(databuffer,0,sizeof(databuffer));
read(ttlevents[i].data.fd,databuffer,sizeof(databuffer));
write(temptr->pipeArr[1],databuffer,sizeof(databuffer)); //The program tends to hang in this place. Probably pipe is not being emptied fast enough so pipe is full
printf("Wrote to p2c : %d -> %d\n",temptr->pipeArr[0],temptr->pipeArr[1]);
count++;
close(ttlevents[i].data.fd); //if i want to close the connection but i haven't decided yet. Testing only remove it when done
if((count%u) == 0)
temptr = p2c;
else
temptr++;
}//else
}//for
} //while
while(1)
{
int status = -1;
int n = epoll_wait (ecfd, cttlevents, MAXEVENTS, -1);
for(int i=0;i<n;i++)
{
printf("%d Child -> Events Detected : %d\n",getpid(),n);
if(cttlevents[i].data.fd == p2c->pipeArr[0]) // New Connection
{
char buffer[1024];
memset(buffer,0,sizeof(buffer));
read(cttlevents[i].data.fd,buffer,sizeof(buffer));
printf("Parent Said : %s, Child Process Id : %d, Pipe Id : %d\n",buffer,getpid(),c2p->pipeArr[1]);
char strtemp[50]="WRITING TO PARENT PIPE";
write(c2p->pipeArr[1],strtemp,sizeof(strtemp));
}
else
printf("Event detected at childprocess but it is not p2c : %d",cttlevents[i].data.fd);
}//for
} //while
strace还表明了以下建议。
write(4, "HELLO\0", 6) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
我不确定如何设置SA_RESTART。