问题

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 failed: Broken 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. -bash: fork: retry: Resource temporarily unavailable错误

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

  4. [转] - linux下使用write\send发送数据报 EAGAIN : Resource temporarily unavailable 错

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

  5. TNS-12518,TNS-12536,TNS-00506,Linux Error: 11: 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: Error initializing authority: Could not connect: Resource temporarily unavailable (g-io-error-quark, 27)

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

  7. FTP主动模式上传文件时返回"ftp: accept: Resource temporarily unavailable"

    FTP主动模式上传文件时返回 Passive mode off ftp: accept: Resource temporarily unavailable 这个问题要从ftp的2种模式说起 PORT ...

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

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

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

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

随机推荐

  1. expect-调试模式的使用

    1.expect简介 Expect是一种TCL扩展性的语言,主要用于完成系统交互方面的功能,比如SSH.FTP等,这些程序都需要手工与它们进行互动,而使用Expect就可以模拟人手工互动的过程,是一种 ...

  2. hbase 快速开发

    hbase是一个分布式的NoSQL,部署起来配置很多东西,开发起来太慢,可以使用docker快速搭建环境 gs@gs-virtual-machine:~$ sudo docker run -ti ha ...

  3. MTSC2019第五届中国移动互联网测试开发大会北京站震撼来袭!

    MTSC2019 暨第五届中国移动互联网测试开发大会(Mobile Testing Summit China)是由国内最大的测试开发技术社区之一 TesterHome 发起的行业会议,聚焦于软件测试及 ...

  4. 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  5. 【Spark-core学习之一】 Spark初识

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  6. python locust 性能测试:locsut参数化-保证并发测试数据唯一性,不循环取数据

    from locust import TaskSet, task, HttpLocustimport queue class UserBehavior(TaskSet): @task def test ...

  7. PHP函数------parse_ini_file()

    1.parse_ini_file()函数用于解析一个配置文件,并以数组的形式返回其中的设置. 举例说明:group.ini文件,文件内容如下: 0 = "Hleducation" ...

  8. Vue将px转化为rem适配移动端

    Vue将px转化为rem适配移动端 1.下载lib-flexible我使用的是vue-cli+webpack,所以是通过npm来安装的npm i lib-flexible --save 2.引入lib ...

  9. nginx 模块及运行机制 第三章

    概述:nginx服务器模块.web请求处理机制及事件驱动模型.进程功能和进程间通信 一:Nginx的模块化结构设计: 1.核心模块:指的是nginx服务器运行当中必不可少的模块,这些模块提供了最基本最 ...

  10. 【题解】JSOIWC2019 Round3

    题面 题解: T1: 先对图进行染色,重新对联通快重新建图 根据四色定理,珂以得出这实际是一颗树 因为树的中心肯定是最佳的决策,所以答案就是树的直径/2(上取整) #include <bits/ ...