Linux SIGPIPE, 断管

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6824265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:15:40  来源:igfitidea点击:

SIGPIPE, Broken pipe

c++clinuxnetworking

提问by codereviewanskquestions

I am working on a networking program using epoll on linux machine and I got the error message from gdb.

我正在 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 ?? ()

My server doing n^2 time calculation and I tried to run the server with 500 connected users. What might cause this error? and how do I fix this?

我的服务器进行 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;
        }

采纳答案by Greg Hewgill

When you write to a pipe that has been closed (by the remote end) , your program will receive this signal. For simple command-line filter programs, this is often an appropriate default action, since the default handler for SIGPIPE will terminate the program.

当您写入已关闭(由远程端)的管道时,您的程序将收到此信号。对于简单的命令行过滤程序,这通常是合适的默认操作,因为 SIGPIPE 的默认处理程序将终止程序。

For a multithreaded program, the correct action is usually to ignorethe SIGPIPE signal, so that writing to a closed socket will not terminate the program.

对于多线程程序,正确的操作通常是忽略SIGPIPE 信号,这样写入关闭的套接字不会终止程序。

Note that you cannotsuccessfully perform a check before writing, since the remote end may close the socket in between your check and your call to write().

请注意,您无法在写入之前成功执行检查,因为远程端可能会在您的检查和调用 之间关闭套接字write()

See this question for more information on ignoring SIGPIPE: How to prevent SIGPIPEs (or handle them properly)

有关忽略 SIGPIPE 的更多信息,请参阅此问题:如何防止 SIGPIPE(或正确处理它们)

回答by Lightness Races in Orbit

You're not catching SIGPIPEsignals, but you're trying to write to a pipe that's been broken/closed.

您不是在捕捉SIGPIPE信号,而是在尝试写入已损坏/关闭的管道。

Fairly self-explanatory.

不言自明。

It's usually sufficient to handle SIGPIPEsignals as a no-op, and handle the error case around your writecall in whatever application-specific manner you require... like this.

通常将SIGPIPE信号作为无操作处理就足够了,并write以您需要的任何特定于应用程序的方式处理您的调用周围的错误情况……就像这样