1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/syscall.h> #include <signal.h> #include <pthread.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <time.h>
#define MAX_THREADS 5
typedef struct { pthread_t thread; pid_t tid; int id; int running; time_t start_time; } thread_info_t;
thread_info_t threads[MAX_THREADS]; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pid_t gettid(void) { return syscall(SYS_gettid); }
void signal_handler(int sig) { pid_t tid = gettid(); printf("[信号处理] 线程 %d 收到信号: %s\n", tid, strsignal(sig)); if(sig == SIGUSR1) { printf("[信号处理] 线程 %d 将暂停工作\n", tid); sleep(5); printf("[信号处理] 线程 %d 恢复工作\n", tid); } else if(sig == SIGTERM) { printf("[信号处理] 线程 %d 准备退出\n", tid); pthread_exit(NULL); } }
void* worker_thread(void* arg) { thread_info_t* info = (thread_info_t*)arg; info->tid = gettid(); info->running = 1; info->start_time = time(NULL); printf("工作线程 %d 启动,系统线程ID: %d\n", info->id, info->tid); signal(SIGUSR1, signal_handler); signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); while(info->running) { printf("线程 %d 正在处理任务...\n", info->tid); sleep(3); } printf("线程 %d 正常退出\n", info->tid); return NULL; }
int tgkill(int tgid, int tid, int sig) { return syscall(SYS_tgkill, tgid, tid, sig); }
void show_thread_status() { printf("\n=== 线程状态 ===\n"); pthread_mutex_lock(&mutex); for(int i = 0; i < MAX_THREADS; i++) { if(threads[i].tid != 0) { time_t uptime = time(NULL) - threads[i].start_time; printf("线程 %d: TID=%d, 状态=%s, 运行时间=%lds\n", threads[i].id, threads[i].tid, threads[i].running ? "运行中" : "已停止", uptime); } } pthread_mutex_unlock(&mutex); printf("================\n\n"); }
int main() { int i; printf("主线程监控程序启动,PID: %d\n", getpid()); memset(threads, 0, sizeof(threads)); for(i = 0; i < MAX_THREADS; i++) { threads[i].id = i + 1; if(pthread_create(&threads[i].thread, NULL, worker_thread, &threads[i]) != 0) { perror("创建线程失败"); exit(EXIT_FAILURE); } printf("创建线程 %d\n", i + 1); } sleep(2); show_thread_status(); printf("=== 演示tgkill操作 ===\n"); if(tgkill(getpid(), threads[0].tid, SIGUSR1) == 0) { printf("向线程 %d 发送暂停信号\n", threads[0].tid); } sleep(1); if(tgkill(getpid(), threads[2].tid, SIGTERM) == 0) { printf("向线程 %d 发送终止信号\n", threads[2].tid); pthread_mutex_lock(&mutex); threads[2].running = 0; pthread_mutex_unlock(&mutex); } sleep(2); show_thread_status(); printf("向所有线程发送中断信号...\n"); pthread_mutex_lock(&mutex); for(i = 0; i < MAX_THREADS; i++) { if(threads[i].tid != 0 && threads[i].running) { if(tgkill(getpid(), threads[i].tid, SIGINT) == 0) { printf("向线程 %d 发送中断信号\n", threads[i].tid); } } } pthread_mutex_unlock(&mutex); sleep(2); for(i = 0; i < MAX_THREADS; i++) { if(threads[i].tid != 0) { pthread_join(threads[i].thread, NULL); printf("线程 %d 已结束\n", threads[i].id); } } printf("所有线程已安全退出,程序结束\n"); return 0; }
|