190 lines
5.5 KiB
C++
190 lines
5.5 KiB
C++
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include <time.h>
|
|
#include "PQSMsg.h"
|
|
#include "client2.h"
|
|
|
|
/* 常量定义 */
|
|
#define THREAD_CONNECTIONS 10 // 最大线程数
|
|
#define MONITOR_INTERVAL 1 // 监控间隔(秒)
|
|
|
|
/* 线程状态枚举 */
|
|
typedef enum {
|
|
THREAD_RUNNING, // 0:运行中
|
|
THREAD_STOPPED, // 1:正常停止
|
|
THREAD_RESTARTING, // 2:重启中
|
|
THREAD_CRASHED // 3:异常崩溃
|
|
} thread_state_t;
|
|
|
|
/* 线程控制结构体 */
|
|
typedef struct {
|
|
pthread_t tid; // 线程ID
|
|
int index; // 线程编号(0~CONNECTIONS-1)
|
|
thread_state_t state; // 当前状态
|
|
pthread_mutex_t lock; // 线程专用互斥锁
|
|
} thread_info_t;
|
|
|
|
/* 全局变量 */
|
|
thread_info_t thread_info[THREAD_CONNECTIONS]; // 线程信息数组
|
|
pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER; // 全局互斥锁
|
|
|
|
/* 线程工作函数 待分配线程池*/
|
|
void* work_thread(void* arg) {
|
|
int index = *(int*)arg; // 获取线程索引
|
|
free(arg); // 释放动态分配的索引内存
|
|
|
|
// 更新线程状态为运行中
|
|
pthread_mutex_lock(&thread_info[index].lock);
|
|
printf("Thread %d started\n", index);
|
|
thread_info[index].state = THREAD_RUNNING;
|
|
pthread_mutex_unlock(&thread_info[index].lock);
|
|
|
|
// 模拟工作循环(5秒间隔)
|
|
while (1) {
|
|
sleep(5);
|
|
|
|
// 10%概率模拟线程故障
|
|
if (rand() % 10 == 0) {
|
|
pthread_mutex_lock(&thread_info[index].lock);
|
|
printf("Thread %d simulated failure\n", index);
|
|
pthread_mutex_unlock(&thread_info[index].lock);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 线程终止处理
|
|
pthread_mutex_lock(&thread_info[index].lock);
|
|
thread_info[index].state = THREAD_STOPPED;
|
|
printf("Thread %d stopped\n", index);
|
|
pthread_mutex_unlock(&thread_info[index].lock);
|
|
return NULL;
|
|
}
|
|
/* 线程工作函数 1号子线程*/
|
|
/* 客户端连接管理线程函数*/
|
|
void* client_manager_thread(void* arg) {
|
|
int index = *(int*)arg;
|
|
free(arg);
|
|
|
|
// 更新线程状态为运行中
|
|
pthread_mutex_lock(&thread_info[index].lock);
|
|
printf("Client Manager Thread %d started\n", index);
|
|
thread_info[index].state = THREAD_RUNNING;
|
|
pthread_mutex_unlock(&thread_info[index].lock);
|
|
|
|
printf("Started client connections\n");
|
|
|
|
start_client_connect();
|
|
|
|
printf("Stopped all client connections\n");
|
|
|
|
// 线程终止处理
|
|
pthread_mutex_lock(&thread_info[index].lock);
|
|
thread_info[index].state = THREAD_STOPPED;
|
|
printf("Client Manager Thread %d stopped\n", index);
|
|
pthread_mutex_unlock(&thread_info[index].lock);
|
|
return NULL;
|
|
}
|
|
/* 线程重启函数 */
|
|
void restart_thread(int index) {
|
|
pthread_mutex_lock(&global_lock);
|
|
if (thread_info[index].state == THREAD_RESTARTING) {
|
|
pthread_mutex_unlock(&global_lock);
|
|
return; // 避免重复重启
|
|
}
|
|
|
|
thread_info[index].state = THREAD_RESTARTING;
|
|
printf("Restarting thread %d\n", index);
|
|
pthread_mutex_unlock(&global_lock);
|
|
|
|
// 创建新线程
|
|
int* new_index = (int*)malloc(sizeof(int));
|
|
*new_index = index;
|
|
|
|
if (index == 0) {
|
|
// 客户端管理线程
|
|
if (pthread_create(&thread_info[index].tid, NULL, client_manager_thread, new_index) != 0) {
|
|
pthread_mutex_lock(&global_lock);
|
|
printf("Failed to restart client manager thread %d\n", index);
|
|
thread_info[index].state = THREAD_CRASHED;
|
|
pthread_mutex_unlock(&global_lock);
|
|
free(new_index);
|
|
}
|
|
}
|
|
else {
|
|
// 其他工作线程
|
|
// 这里简化为空,实际应用中可添加其他线程
|
|
}
|
|
}
|
|
|
|
/* 线程存活检测 */
|
|
int is_thread_alive(pthread_t tid) {
|
|
return pthread_tryjoin_np(tid, NULL) == EBUSY; // EBUSY表示线程仍在运行
|
|
}
|
|
|
|
/* 主函数 */
|
|
int main() {
|
|
srand(time(NULL)); // 初始化随机数种子
|
|
|
|
// 初始化线程数组
|
|
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
|
|
thread_info[i].index = i;
|
|
thread_info[i].state = THREAD_STOPPED;
|
|
pthread_mutex_init(&thread_info[i].lock, NULL); // 初始化每个线程的锁
|
|
}
|
|
|
|
// 创建初始线程组
|
|
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
|
|
int* index = (int*)malloc(sizeof(int));
|
|
*index = i;
|
|
|
|
if (i == 0) {
|
|
// 客户端管理线程
|
|
if (pthread_create(&thread_info[i].tid, NULL, client_manager_thread, index) != 0) {
|
|
printf("Failed to create client manager thread %d\n", i);
|
|
free(index);
|
|
}
|
|
}
|
|
else {
|
|
// 其他工作线程
|
|
// 这里简化为空,实际应用中可添加其他线程
|
|
free(index);
|
|
}
|
|
}
|
|
|
|
printf("Thread monitoring system started with %d workers\n", THREAD_CONNECTIONS);
|
|
|
|
// 主监控循环
|
|
while (1) {
|
|
sleep(MONITOR_INTERVAL);
|
|
|
|
// 检查所有线程状态
|
|
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
|
|
pthread_mutex_lock(&thread_info[i].lock);
|
|
|
|
// 检测运行中线程是否崩溃
|
|
if (thread_info[i].state == THREAD_RUNNING && !is_thread_alive(thread_info[i].tid)) {
|
|
printf("Thread %d crashed unexpectedly\n", i);
|
|
thread_info[i].state = THREAD_CRASHED;
|
|
}
|
|
|
|
// 处理需要重启的线程
|
|
if (thread_info[i].state == THREAD_STOPPED || thread_info[i].state == THREAD_CRASHED) {
|
|
pthread_mutex_unlock(&thread_info[i].lock);
|
|
restart_thread(i); // 异步重启
|
|
}
|
|
else {
|
|
pthread_mutex_unlock(&thread_info[i].lock);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 清理资源(理论上不会执行到这里)
|
|
for (int i = 0; i < THREAD_CONNECTIONS; i++) {
|
|
pthread_mutex_destroy(&thread_info[i].lock);
|
|
}
|
|
return 0;
|
|
}
|