C语言 程序收到信号 SIGPIPE, Broken pipe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18935446/
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
Program received signal SIGPIPE, Broken pipe
提问by alk
I write a client program based on posix sockets. The program creates multiple threads and is going to lock the server. But during debug in gdb time the program gives an info (error)
我编写了一个基于 posix 套接字的客户端程序。该程序创建多个线程并将锁定服务器。但是在 gdb 调试期间,程序给出了一个信息(错误)
(gdb) n Program received signal SIGPIPE, Broken pipe. [Switching to Thread 0xb74c0b40 (LWP 4864)] 0xb7fdd424 in __kernel_vsyscall () (gdb)
(gdb) n Program received signal SIGPIPE, Broken pipe. [Switching to Thread 0xb74c0b40 (LWP 4864)] 0xb7fdd424 in __kernel_vsyscall () (gdb)
Here's the code:
这是代码:
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int get_hostname_by_ip(char* h , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ((he = gethostbyname(h)) == NULL)
{
perror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
return 1;
}
void client(char* h, int s)
{
int fd;
struct sockaddr_in addr;
char ch[]="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
fd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family=AF_INET;
char* ip = new char[20];
get_hostname_by_ip(h, ip);
addr.sin_addr.s_addr=inet_addr(ip);
int port = 80;
addr.sin_port=htons(port);
if(connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
perror("connect error");
return;
}
while(1)
{
if(send(fd, ch, sizeof(ch), 0) < 0)
{
perror("send");
}
}
//char buffer[1024];
//if(recv(fd, &buffer, sizeof(buffer), 0) < 0)
//{
// perror("recive");
//}
//printf("nReply from Server: %s\n", buffer);
close(fd);
}
struct info
{
char* h;
int c;
};
void* thread_entry_point(void* i)
{
info* in = (info*)i;
client(in->h, in->c);
}
int main(int argc, char** argv)
{
int s = atoi(argv[2]);
pthread_t t[s];
info in = {argv[1], s};
for(int i = 0; i < s; ++i)
{
pthread_create(&t[i], NULL, thread_entry_point, (void*)&in);
}
pthread_join(t[0], NULL);
return 0;
}
What it is and what to do?
它是什么以及该怎么做?
回答by alk
The process received a SIGPIPE. The default behaviour for this signal is to end the process.
该过程收到一个SIGPIPE. 此信号的默认行为是结束进程。
A SIGPIPEis sent to a process if it tried to write to a socket that had been shutdown for writing or isn't connected (anymore).
SIGPIPE如果进程尝试写入已关闭以进行写入或未连接(不再连接)的套接字,则将A发送到进程。
To avoid that the program ends in this case, you could either
为避免程序在这种情况下结束,您可以
make the process ignore
SIGPIPE#include <signal.h> int main(void) { sigaction(SIGPIPE, &(struct sigaction){SIG_IGN}, NULL); ...or
install an explicit handler for
SIGPIPE(typically doing nothing):#include <signal.h> void sigpipe_handler(int unused) { } int main(void) { sigaction(SIGPIPE, &(struct sigaction){sigpipe_handler}, NULL); ...
使过程忽略
SIGPIPE#include <signal.h> int main(void) { sigaction(SIGPIPE, &(struct sigaction){SIG_IGN}, NULL); ...或者
为
SIGPIPE(通常什么都不做)安装一个显式处理程序:#include <signal.h> void sigpipe_handler(int unused) { } int main(void) { sigaction(SIGPIPE, &(struct sigaction){sigpipe_handler}, NULL); ...
In both cases send*()/write()would return -1and set errnoto EPIPE.
在这两种情况下send*()/write()都会返回-1并设置errno为EPIPE.
回答by Jason Chagas
When debugging with 'gdb', it is possible to manually disable SIGPIPE as follows:
使用 'gdb' 进行调试时,可以手动禁用 SIGPIPE,如下所示:
(gdb) handle SIGPIPE nostop
(gdb) 处理 SIGPIPE nostop
回答by Hatem Mashaqi
A workaround for SIGPIPE, you can ignore this signal by this code:
SIGPIPE 的解决方法,您可以通过以下代码忽略此信号:
#include <signal.h>
/* Catch Signal Handler functio */
void signal_callback_handler(int signum){
printf("Caught signal SIGPIPE %d\n",signum);
}
in your code (main or globally)
在您的代码中(主要或全局)
/* Catch Signal Handler SIGPIPE */
signal(SIGPIPE, signal_callback_handler);
回答by user207421
You have written to a connection that has already been closed by the peer.
您已写入已被对等方关闭的连接。
回答by neoneye
I sort of experienced the same problem and it let me to this SO post. I got sporadic SIGPIPE signals causing crashes of my fastcgi C program run by nginx. I tried to signal(SIGPIPE, SIG_IGN);without luck, it kept crashing.
我遇到了同样的问题,它让我看到了这篇 SO 帖子。我收到了零星的 SIGPIPE 信号,导致由 nginx 运行的 fastcgi C 程序崩溃。我试着signal(SIGPIPE, SIG_IGN);不走运,它一直崩溃。
The reason was that nginx's temp dir had a permission problem. Fixing the permissions solved the SIGPIPE problem. Details here on how to fixand more here.
原因是 nginx 的临时目录有权限问题。修复权限解决了 SIGPIPE 问题。此处有关如何修复的详细信息以及此处的更多信息。

