linux 线程存活状态监控,不存在就重启

2024-05-30 15:08

本文主要是介绍linux 线程存活状态监控,不存在就重启,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

直接上代码

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include<unistd.h>
#include <signal.h>
#include <errno.h>/*线程变量*/
pthread_t thread[3];
pthread_mutex_t mut;void *thread1()
{/*线程互斥锁*///pthread_mutex_lock(&mut);//pthread_mutex_unlock(&mut);int i = 0;while(i++ < 10) {printf("111\n");sleep(1);}pthread_exit(NULL);
}/*定时函数*/
void sigalrm_func(int sig)
{alarm(2); /*改为定时2s*/printf("222\n");
}void *thread2()
{/*定时1s,处理函数为*/signal(SIGALRM, sigalrm_func);alarm(1);while(1);/*线程互斥锁*///pthread_mutex_lock(&mut);//pthread_mutex_unlock(&mut);pthread_exit(NULL);
}void thread_wait(int i)
{/*等待线程结束*/if(thread[i] != 0) {pthread_join(thread[i],NULL);}
}/*pthread_kill的返回值:成功(0) 线程不存在(ESRCH) 信号不合法(EINVAL)*/
void test_pthread(int i) 
{int pthread_kill_err;pthread_kill_err = pthread_kill(thread[i],0);if(pthread_kill_err == ESRCH) {printf("pthread[%d] no existence or withdrawal.\n", i);pthread_join(thread[i],NULL);sleep(10);if((pthread_create(&thread[i], NULL, switch_control_thread, NULL)) != 0) {printf("[sqlite] -- switch_control_thread create error!\n");}}else if(pthread_kill_err == EINVAL)printf("unlawful transmission of signals.\n");elseprintf("pthread[%d] still alive.\n", i);
}/*检测所有线程的存活状态*/
void *thread3()
{while(1) {sleep(3);test_pthread(0);test_pthread(1);test_pthread(2);}pthread_exit(NULL);
}void thread_create(void)
{int temp;memset(&thread, 0, sizeof(thread));/*创建线程*/if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) {printf("[sqlite] -- switch_control_thread create error!\n");}if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) {printf("[sqlite] -- data_control_thread create error!\n");}if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0) {printf("[sqlite] -- data_control_thread create error!\n");}
}int main()
{/*用默认属性初始化互斥锁*/pthread_mutex_init(&mut,NULL);thread_create();while(1);return 0;
}

编译:gcc main.c -o main -lpthread

运行:./main

运行结果:


这篇关于linux 线程存活状态监控,不存在就重启的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1016423

相关文章

linux生产者,消费者问题

pthread_cond_wait() :用于阻塞当前线程,等待别的线程使用pthread_cond_signal()或pthread_cond_broadcast来唤醒它。 pthread_cond_wait() 必须与pthread_mutex 配套使用。pthread_cond_wait()函数一进入wait状态就会自动release mutex。当其他线程通过pthread

随想录 Day 69 并查集 107. 寻找存在的路径

随想录 Day 69 并查集 107. 寻找存在的路径 理论基础 int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好vector<int> father = vector<int> (n, 0); // C++里的一种数组结构// 并查集初始化void init() {for (int i = 0; i < n; ++i) {father[i] = i;}

Linux 安装、配置Tomcat 的HTTPS

Linux 安装 、配置Tomcat的HTTPS 安装Tomcat 这里选择的是 tomcat 10.X ,需要Java 11及更高版本 Binary Distributions ->Core->选择 tar.gz包 下载、上传到内网服务器 /opt 目录tar -xzf 解压将解压的根目录改名为 tomat-10 并移动到 /opt 下, 形成个人习惯的路径 /opt/tomcat-10

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

【Linux进阶】UNIX体系结构分解——操作系统,内核,shell

1.什么是操作系统? 从严格意义上说,可将操作系统定义为一种软件,它控制计算机硬件资源,提供程序运行环境。我们通常将这种软件称为内核(kerel),因为它相对较小,而且位于环境的核心。  从广义上说,操作系统包括了内核和一些其他软件,这些软件使得计算机能够发挥作用,并使计算机具有自己的特生。这里所说的其他软件包括系统实用程序(system utility)、应用程序、shell以及公用函数库等

Windows/macOS/Linux 安装 Redis 和 Redis Desktop Manager 可视化工具

本文所有安装都在macOS High Sierra 10.13.4进行,Windows安装相对容易些,Linux安装与macOS类似,文中会做区分讲解 1. Redis安装 1.下载Redis https://redis.io/download 把下载的源码更名为redis-4.0.9-source,我喜欢跟maven、Tomcat放在一起,就放到/Users/zhan/Documents

vue同页面多路由懒加载-及可能存在问题的解决方式

先上图,再解释 图一是多路由页面,图二是路由文件。从图一可以看出每个router-view对应的name都不一样。从图二可以看出层路由对应的组件加载方式要跟图一中的name相对应,并且图二的路由层在跟图一对应的页面中要加上components层,多一个s结尾,里面的的方法名就是图一路由的name值,里面还可以照样用懒加载的方式。 页面上其他的路由在路由文件中也跟图二是一样的写法。 附送可能存在

Linux系统稳定性的奥秘:探究其背后的机制与哲学

在计算机操作系统的世界里,Linux以其卓越的稳定性和可靠性著称,成为服务器、嵌入式系统乃至个人电脑用户的首选。那么,是什么造就了Linux如此之高的稳定性呢?本文将深入解析Linux系统稳定性的几个关键因素,揭示其背后的技术哲学与实践。 1. 开源协作的力量Linux是一个开源项目,意味着任何人都可以查看、修改和贡献其源代码。这种开放性吸引了全球成千上万的开发者参与到内核的维护与优化中,形成了

Linux 下的Vim命令宝贝

vim 命令详解(转自:https://www.cnblogs.com/usergaojie/p/4583796.html) vi: Visual Interface 可视化接口 vim: VI iMproved VI增强版 全屏编辑器,模式化编辑器 vim模式: 编辑模式(命令模式)输入模式末行模式 模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式

Linux和Mac分卷压缩

使用 zip 命令压缩文件 使用 zip 命令压缩文件,并结合 split 命令来分卷: zip - largefile | split -b 500k 举例: zip - ./tomcat.dmg |split -b 500k 上述命令将文件 largefile 压缩成 zip 包并分卷成不超过 500k 的文件,分解后文件名默认是 x* ,后缀为 2 位a-z 字母,如 aa、ab。