我正在使用linux机器上的epoll开发网络程序,我收到了来自gdb的错误消息。
Program received signal SIGPIPE, Broken pipe.
[Switching to Thread 0x7ffff609a700 (LWP 19788)]
0x00007ffff7bcdb2d in write () from /lib/libpthread.so.0
(gdb)
(gdb) backtrace
#0 0x00007ffff7bcdb2d in write () from /lib/libpthread.so.0
#1 0x0000000000416bc8 in WorkHandler::workLoop() ()
#2 0x0000000000416920 in WorkHandler::runWorkThread(void*) ()
#3 0x00007ffff7bc6971 in start_thread () from /lib/libpthread.so.0
#4 0x00007ffff718392d in clone () from /lib/libc.so.6
#5 0x0000000000000000 in ?? ()
我的服务器正在进行n ^ 2次计算,我尝试使用500个已连接的用户运行服务器。什么可能导致此错误?以及如何解决此问题?
while(1){
if(remainLength >= MAX_LENGTH)
currentSentLength = write(client->getFd(), sBuffer, MAX_LENGTH);
else
currentSentLength = write(client->getFd(), sBuffer, remainLength);
if(currentSentLength == -1){
log("WorkHandler::workLoop, connection has been lost \n");
break;
}
sBuffer += currentSentLength;
remainLength -= currentSentLength;
if(remainLength == 0)
break;
}
答案 0 :(得分:28)
当您写入已关闭的管道(由远程端)时,您的程序将收到此信号。对于简单的命令行过滤程序,这通常是一个合适的默认操作,因为SIGPIPE的默认处理程序将终止该程序。
对于多线程程序,正确的操作通常是忽略 SIGPIPE信号,因此写入已关闭的套接字不会终止程序。
请注意,无法在写入前成功执行检查,因为远程端可能会在您的支票与您对write()
的通话之间关闭套接字。
有关忽略SIGPIPE的更多信息,请参阅此问题:How to prevent SIGPIPEs (or handle them properly)
答案 1 :(得分:2)