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
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <mqueue.h> #include <fcntl.h> #include <sys/stat.h> #include <string.h> #include <errno.h> #include <time.h> #include <signal.h>
#define MAX_MESSAGES 10 #define MESSAGE_SIZE 256
volatile sig_atomic_t stop_flag = 0;
void signal_handler(int sig) { printf("收到信号 %d,准备停止...\n", sig); stop_flag = 1; }
mqd_t create_realtime_queue(const char* name) { struct mq_attr attr = { .mq_flags = 0, .mq_maxmsg = 5, .mq_msgsize = MESSAGE_SIZE, .mq_curmsgs = 0 }; mqd_t mq = mq_open(name, O_CREAT | O_RDWR, 0644, &attr); if (mq == (mqd_t)-1) { perror("创建实时队列失败"); return -1; } printf("创建实时队列: %s\n", name); return mq; }
int calculate_relative_timeout(struct timespec* abs_timeout, int milliseconds) { if (clock_gettime(CLOCK_REALTIME, abs_timeout) == -1) { perror("获取当前时间失败"); return -1; } long seconds = milliseconds / 1000; long nanoseconds = (milliseconds % 1000) * 1000000; abs_timeout->tv_sec += seconds; abs_timeout->tv_nsec += nanoseconds; if (abs_timeout->tv_nsec >= 1000000000) { abs_timeout->tv_sec++; abs_timeout->tv_nsec -= 1000000000; } return 0; }
void realtime_message_sender(mqd_t mq, const char* sender_name) { printf("实时发送器 %s 启动\n", sender_name); srand(time(NULL)); int message_count = 0; while (!stop_flag && message_count < MAX_MESSAGES) { char message[MESSAGE_SIZE]; snprintf(message, sizeof(message), "%s: 实时消息 %d", sender_name, message_count + 1); int timeout_ms = 10 + rand() % 91; struct timespec abs_timeout; if (calculate_relative_timeout(&abs_timeout, timeout_ms) == -1) { continue; } unsigned int priority = rand() % 10; int result = mq_timedsend(mq, message, strlen(message), priority, &abs_timeout); if (result == 0) { printf("[%s] 发送成功: %s (优先级: %u, 超时: %dms)\n", sender_name, message, priority, timeout_ms); } else { if (errno == ETIMEDOUT) { printf("[%s] 发送超时: %s (超时: %dms)\n", sender_name, message, timeout_ms); } else if (errno == EAGAIN) { printf("[%s] 队列满,发送失败: %s\n", sender_name, message); } else { printf("[%s] 发送错误: %s (%s)\n", sender_name, message, strerror(errno)); } } message_count++; usleep(500000); } printf("实时发送器 %s 完成\n", sender_name); }
void message_receiver(mqd_t mq, const char* receiver_name) { printf("消息接收器 %s 启动\n", receiver_name); char buffer[MESSAGE_SIZE]; ssize_t bytes_received; unsigned int priority; int received_count = 0; while (!stop_flag && received_count < MAX_MESSAGES * 2) { struct timespec abs_timeout; if (calculate_relative_timeout(&abs_timeout, 2000) == -1) { continue; } bytes_received = mq_timedreceive(mq, buffer, sizeof(buffer), &priority, &abs_timeout); if (bytes_received > 0) { buffer[bytes_received] = '\0'; printf("[%s] 接收: %s (优先级: %u)\n", receiver_name, buffer, priority); received_count++; } else if (errno == ETIMEDOUT) { printf("[%s] 接收超时\n", receiver_name); } else if (errno == EAGAIN) { printf("[%s] 暂无消息\n", receiver_name); usleep(100000); } else { printf("[%s] 接收错误: %s\n", receiver_name, strerror(errno)); break; } } printf("消息接收器 %s 完成,接收 %d 条消息\n", receiver_name, received_count); }
int main() { printf("=== 实时系统超时消息发送示例 ===\n"); const char* queue_name = "/realtime_queue"; signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); mqd_t mq = create_realtime_queue(queue_name); if (mq == -1) { exit(EXIT_FAILURE); } pid_t sender1 = fork(); if (sender1 == 0) { realtime_message_sender(mq, "发送器1"); exit(EXIT_SUCCESS); } pid_t sender2 = fork(); if (sender2 == 0) { realtime_message_sender(mq, "发送器2"); exit(EXIT_SUCCESS); } pid_t receiver = fork(); if (receiver == 0) { message_receiver(mq, "接收器"); exit(EXIT_SUCCESS); } printf("系统运行中... 按Ctrl+C停止或等待30秒\n"); int elapsed = 0; while (elapsed < 30 && !stop_flag) { sleep(1); elapsed++; if (elapsed % 5 == 0) { struct mq_attr attr; if (mq_getattr(mq, &attr) == 0) { printf("队列状态: %ld/%ld 消息\n", attr.mq_curmsgs, attr.mq_maxmsg); } } } stop_flag = 1; printf("发送停止信号...\n"); waitpid(sender1, NULL, 0); waitpid(sender2, NULL, 0); waitpid(receiver, NULL, 0); mq_close(mq); mq_unlink(queue_name); printf("系统已停止,资源已清理\n"); printf("\n=== 实时系统演示完成 ===\n"); return 0; }
|