linux 程序无缘无故推出 没有core文件 broken pipe Resource temporarily unavailable

时间:2023-02-09 13:33:59

问题

1. linux socket 服务端程序 无缘无故退出 。

2. 客户端大量访问服务端后,出现  Resource temporarily unavailable错误

问题分析:

1.

是否有代码问题出现段错误

发现没有任何错误输出,查看(ulimit -a )并打开 (ulimit -c unlimited) core输出   也没有core 文件产生。

后面发现,控制台 后台启动程序( nohup  ./xxx  &  )在程序退出的时候能看到退出原因

此处我的server是http服务 我的问题 客户端访问服务端  不等待服务端能响应完整, 直接断开。  此处浏览器f5刷新程序实现。

2. 网上查找 Resource temporarily unavailable 相关问题

ps -T -p pid  查看进程pid 有多少子进程 发现子进程都退出了。

最后,查找网络 发现应该是子线程回收问题,没有释放资源。

解决:

1.  SIGPIPE 信号的产生使进程退出了,所以在程序开始增加下面语句就可以

忽略管道类写错误, 因为我此处 tcp客户端关闭  服务端没有对所有send做判断是  会出现写错误 触发SIGPIPE,信号。

signal(SIGPIPE,SIG_IGN)

备注: 最好的解决方法还是 在每一个调用系统 读写的时候 都有有效的判断和处理。

2.  线程资源没有合理释放。  下面代码在创建线程的时候设置线程为detached 脱离的,退出自动清理资源。

pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&pid, &attr, PrintHello, NULL);
pthread_attr_destroy (&attr);

程序SIGPIPE退出
https://www.cnblogs.com/fgokey/p/5949004.html

线程创建

http://www.cppblog.com/prayer/archive/2012/04/23/172427.html

转:

http://www.cppblog.com/prayer/archive/2012/04/23/172427.html

这两天在看Pthread 资料的时候,无意中看到这样一句话(man pthread_detach):

Either pthread_join(3) or pthread_detach() should be called for each thread
that an application creates, so that system resources for the thread can be
released. (But note that the resources of all threads are freed when the
process terminates.)
也就是说:每个进程创建以后都应该调用pthread_join 或 pthread_detach 函数,只有这样在线程结束的时候资源(线程的描述信息和stack)才能被释放.

之后又查了pthread_join 但是没有明确说明必须调用pthread_join 或 pthread_detach.

但是再查了 Pthread for win32 pthread_join

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks.

才知道如果在新线程里面没有调用pthread_join 或 pthread_detach会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。

解决方法有三个:
1. 线程里面调用 pthread_detach(pthread_self()) 这个方法最简单
2. 在创建线程的设置PTHREAD_CREATE_DETACHED属性
3. 创建线程后用 pthread_join() 一直等待子线程结束。

下面是几个简单的例子
1. 调用 pthread_detach(pthread_self())
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
pthread_detach(pthread_self());
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//printf(“Hello World! It’s me, thread #%ld!\n”, tid);
//pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
2. 在创建线程的设置PTHREAD_CREATE_DETACHED属性
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
pthread_attr_t attr;
pthread_t thread;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&pid, &attr, PrintHello, NULL);
pthread_attr_destroy (&attr);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}
3. 创建线程后用 pthread_join() 一直等待子线程结束。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *PrintHello(void)
{
int stack[1024 * 20] = {0,};
//sleep(1);
long tid = 0;
//pthread_exit(NULL);
//pthread_detach(pthread_self());
}
int main (int argc, char *argv[])
{
pthread_t pid;
int rc;
long t;
while (1) {
printf(“In main: creating thread %ld\n”, t);
rc = pthread_create(&pid, NULL, PrintHello, NULL);
if (rc){
printf(“ERROR; return code from pthread_create() is %d\n”, rc);
//exit(-1);
}
pthread_join(pid, NULL);
sleep(1);
}
printf(” \n— main End —- \n”);
pthread_exit(NULL);
}

linux 程序无缘无故推出 没有core文件 broken pipe Resource temporarily unavailable的更多相关文章

  1. ssh连接报错Write&&num;160&semi;failed&colon;&&num;160&semi;Broken&&num;160&semi;pipe Resource temporarily unavailable

    问题描述 使用root连接服务器正常,切换普通用户连接报错 具体报错如下:Write failed: Broken pipe 或者:failed to execute /bin/bash: Resou ...

  2. Linux C程序异常退出怎么办——core文件帮你忙

    Linux C程序异常退出怎么办——core文件帮你忙 http://blog.csdn.net/zhu2695/article/details/51512138

  3. linux&period; -bash&colon; fork&colon; retry&colon; Resource temporarily unavailable错误

    切换用户或登陆服务器后执行ls命令报错: -bash: fork: retry: Resource temporarily unavailable 上面这段错误提示的本质是Linux操作系统无法创建更 ...

  4. &lbrack;转&rsqb; - linux下使用write&bsol;send发送数据报 EAGAIN &colon; Resource temporarily unavailable 错

    linux下使用write\send发送数据报 EAGAIN : Resource temporarily unavailable 错 首先是我把套接字设置为异步的了,然后在使用write发送数据时采 ...

  5. TNS-12518&comma;TNS-12536&comma;TNS-00506&comma;Linux Error&colon; 11&colon; Resource temporarily unavailable

    TNS-12518: TNS:listener could not hand off client connection TNS-12536: TNS:operation would block  T ...

  6. Linux重启mysql Error getting authority&colon; Error initializing authority&colon; Could not connect&colon; Resource temporarily unavailable &lpar;g-io-error-quark&comma; 27&rpar;

    问题: Linux下重启mysql: systemctl restart mysqld 出现以下错误: Error getting authority: Error initializing auth ...

  7. 让linux中的程序崩溃时生成core文件

    当我们的linux程序崩溃的时候,常常会有这样的提示:    Segmentation fault (core dumped)    段错误 (核心已转储)    提示说生成了core文件,但是此功能 ...

  8. 结合程序崩溃后的core文件分析bug

    引言     在<I/O的效率比较>中,我们在修改图1程序的BUF_SIZE为8388608时,运行程序出现崩溃,如下图1:          图1. 段错误     一般而言,导致程序段 ...

  9. &lbrack;转载&rsqb; Linux 下产生和调试core文件

    原地址:http://blog.csdn.net/shaovey/article/details/2744487 linux下如何产生core,调试core 在程序不寻常退出时,内核会在当前工作目录下 ...

随机推荐

  1. derby支持的数据类型

    Data types This section describes the data types used in Derby. Built-In type overview Numeric types ...

  2. DOM&lowbar;节点层次&lowbar;Element类型

    一.Element类型: nodeType: 1; nodeName: 元素名; nodeValue: null; parentValue: Document 或者 Element; var oDiv ...

  3. JAVA LinkedList和ArrayList的使用及性能分析

    第1部分 List概括List的框架图List 是一个接口,它继承于Collection的接口.它代表着有序的队列.AbstractList 是一个抽象类,它继承于AbstractCollection ...

  4. 国籍控件(js源码)

    国籍控件(js源码) 一直苦于没有好的国籍控件可以用,于是抽空写了一个国籍控件,现分享给大家. 主要功能和界面介绍 国籍控件主要支持中文.英文过滤以及键盘上下事件. 源码介绍 国籍控件核心是两个文件, ...

  5. AOP in dotnet :AspectCore的参数拦截支持

    距离上一篇AspectCore的介绍发布已经很长一段时间了,这篇文章也早该和大家见面,最近一直忙于适应新工作,并在业余时间有幸向何镇汐,Savorboard,农夫,AlexLEWIS等几位大牛请教学习 ...

  6. java常量池詳解

    一.相关概念 什么是常量用final修饰的成员变量表示常量,值一旦给定就无法改变!final修饰的变量有三种:静态变量.实例变量和局部变量,分别表示三种类型的常量. Class文件中的常量池在Clas ...

  7. P4178 Tree(点分治)

    题面要求小于等于K的路径数目,我么很自然的想到点分治(不会的就戳我) 这道题的统计答案与模板题不一样的地方是由等于K到小于等于K 那么我们可以把每一个子节点到当前根(重心)的距离排序,然后用类似双指针 ...

  8. You&&num;39&semi;ve implemented -&lbrack;&lt&semi;UIApplicationDelegate&gt&semi; application&colon;didReceiveRemoteNotification&colon;fetchCompletionHandler&colon;&rsqb;&comma; but you still need to add &quot&semi;remote-notification&quot&semi; to the list of your supported UIBackgrou

    最近有个同事问我,他工程运行时就会有如下提示,但是不影响功能:You've implemented -[<UIApplicationDelegate> application:didRec ...

  9. docker stack 部署 redis

    =============================================== 2019/4/16_第2次修改                       ccb_warlock 更新 ...

  10. DevExpress学习02——DevExpress 14&period;1的汉化

    汉化资源: 汉化补丁:dxKB_A421_DXperience_v14.1_(2014-06-09):http://www.t00y.com/file/86576990 汉化工具:DXperience ...