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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <signal.h> #include <string.h> #include <pthread.h>
#define MAX_TASKS 10 #define HEARTBEAT_INTERVAL 5
typedef struct { int id; char name[50]; int interval_seconds; time_t last_run; int run_count; } task_t;
task_t tasks[MAX_TASKS]; int task_count = 0; timer_t heartbeat_timer; timer_t task_timers[MAX_TASKS]; pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
int add_task(int id, const char* name, int interval) { if (task_count >= MAX_TASKS) { return -1; } tasks[task_count].id = id; strncpy(tasks[task_count].name, name, sizeof(tasks[task_count].name) - 1); tasks[task_count].interval_seconds = interval; tasks[task_count].last_run = 0; tasks[task_count].run_count = 0; return task_count++; }
void execute_task(int task_index) { pthread_mutex_lock(&task_mutex); time_t current_time = time(NULL); tasks[task_index].last_run = current_time; tasks[task_index].run_count++; printf("[%ld] 执行任务 %d (%s): 第 %d 次执行\n", current_time, tasks[task_index].id, tasks[task_index].name, tasks[task_index].run_count); pthread_mutex_unlock(&task_mutex); }
void task_timer_handler(int sig, siginfo_t *si, void *uc) { int task_index = si->si_value.sival_int; execute_task(task_index); }
void heartbeat_handler(int sig, siginfo_t *si, void *uc) { static int heartbeat_count = 0; heartbeat_count++; printf("[%ld] 系统心跳 #%d\n", time(NULL), heartbeat_count); pthread_mutex_lock(&task_mutex); printf(" 任务状态:\n"); for (int i = 0; i < task_count; i++) { printf(" %s: 执行%d次, 最后执行: %s", tasks[i].name, tasks[i].run_count, tasks[i].last_run ? ctime(&tasks[i].last_run) : "从未执行\n"); if (tasks[i].last_run) { char* newline = strchr(ctime(&tasks[i].last_run), '\n'); if (newline) *newline = '\0'; printf("%s\n", ctime(&tasks[i].last_run)); } } pthread_mutex_unlock(&task_mutex); }
int init_timer_system() { struct sigaction sa; sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = task_timer_handler; sigemptyset(&sa.sa_mask); if (sigaction(SIGRTMIN, &sa, NULL) == -1) { perror("sigaction task"); return -1; } sa.sa_sigaction = heartbeat_handler; if (sigaction(SIGRTMIN + 1, &sa, NULL) == -1) { perror("sigaction heartbeat"); return -1; } return 0; }
int start_task_timer(int task_index) { struct sigevent sev; struct itimerspec its; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN; sev.sigev_value.sival_int = task_index; if (timer_create(CLOCK_REALTIME, &sev, &task_timers[task_index]) == -1) { perror("timer_create task"); return -1; } its.it_value.tv_sec = tasks[task_index].interval_seconds; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = tasks[task_index].interval_seconds; its.it_interval.tv_nsec = 0; if (timer_settime(task_timers[task_index], 0, &its, NULL) == -1) { perror("timer_settime task"); return -1; } printf("任务定时器 %s 已启动,间隔 %d 秒\n", tasks[task_index].name, tasks[task_index].interval_seconds); return 0; }
int start_heartbeat_timer() { struct sigevent sev; struct itimerspec its; sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN + 1; sev.sigev_value.sival_int = 0; if (timer_create(CLOCK_REALTIME, &sev, &heartbeat_timer) == -1) { perror("timer_create heartbeat"); return -1; } its.it_value.tv_sec = HEARTBEAT_INTERVAL; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = HEARTBEAT_INTERVAL; its.it_interval.tv_nsec = 0; if (timer_settime(heartbeat_timer, 0, &its, NULL) == -1) { perror("timer_settime heartbeat"); return -1; } printf("心跳定时器已启动,间隔 %d 秒\n", HEARTBEAT_INTERVAL); return 0; }
int main() { printf("=== 实际应用中的定时器系统 ===\n"); printf("启动时间: %s", ctime(&(time_t){time(NULL)})); if (init_timer_system() == -1) { fprintf(stderr, "初始化定时器系统失败\n"); exit(EXIT_FAILURE); } add_task(1, "数据备份", 10); add_task(2, "日志清理", 15); add_task(3, "状态检查", 5); add_task(4, "性能监控", 3); printf("已添加 %d 个任务\n", task_count); for (int i = 0; i < task_count; i++) { if (start_task_timer(i) == -1) { fprintf(stderr, "启动任务定时器 %d 失败\n", i); } } if (start_heartbeat_timer() == -1) { fprintf(stderr, "启动心跳定时器失败\n"); } printf("\n定时器系统运行中...\n"); printf("程序将运行60秒,按Ctrl+C退出\n\n"); for (int i = 0; i < 60; i++) { sleep(1); if ((i + 1) % 10 == 0) { printf("[%ld] === 运行统计 ===\n", time(NULL)); pthread_mutex_lock(&task_mutex); for (int j = 0; j < task_count; j++) { printf(" %s: 执行 %d 次\n", tasks[j].name, tasks[j].run_count); } pthread_mutex_unlock(&task_mutex); printf("==================\n\n"); } } printf("清理定时器...\n"); struct itimerspec stop_its = {{0, 0}, {0, 0}}; timer_settime(heartbeat_timer, 0, &stop_its, NULL); timer_delete(heartbeat_timer); for (int i = 0; i < task_count; i++) { timer_settime(task_timers[i], 0, &stop_its, NULL); timer_delete(task_timers[i]); } printf("定时器系统已停止\n"); printf("\n=== 最终统计 ===\n"); pthread_mutex_lock(&task_mutex); for (int i = 0; i < task_count; i++) { printf("%s: 执行 %d 次\n", tasks[i].name, tasks[i].run_count); } pthread_mutex_unlock(&task_mutex); return 0; }
|