This commit is contained in:
xixu-me committed 2025-04-28 12:15:00 +08:00
1 parent b792642ee2
commit 66d309b937
4 files changed
+47 -47

No files matched your search

+7 -7
View File
@@ -12,25 +12,25 @@ int main() {
int num; int num;
int sum = 0; int sum = 0;
printf("Calculator started. Waiting for collector...\n"); printf("Calculator 已启动,等待 Collector...\n");
// Open FIFO for reading // 打开 FIFO 以读取
fd = open(FIFO_PATH, O_RDONLY); fd = open(FIFO_PATH, O_RDONLY);
if (fd == -1) { if (fd == -1) {
perror("Failed to open FIFO"); perror("打开 FIFO 失败");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Connected! Ready to receive integers.\n"); printf("已连接!准备接收整数。\n");
// Read integers from the FIFO and calculate the sum // 从 FIFO 读取整数并计算总和
while (read(fd, &num, sizeof(int)) > 0) { while (read(fd, &num, sizeof(int)) > 0) {
sum += num; sum += num;
printf("Received: %d, Current Sum: %d\n", num, sum); printf("接收到:%d,当前总和:%d\n", num, sum);
} }
close(fd); close(fd);
printf("Calculator exiting.\n"); printf("Calculator 退出。\n");
return 0; return 0;
} }
+19 -19
View File
@@ -10,56 +10,56 @@ int main() {
pid_t pid; pid_t pid;
int num, sum = 0; int num, sum = 0;
// Create pipe // 创建管道
if (pipe(pipe_fd) == -1) { if (pipe(pipe_fd) == -1) {
perror("Pipe creation failed"); perror("管道创建失败");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Create child process // 创建子进程
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
// Fork failed // fork失败
perror("Fork failed"); perror("fork 失败");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
else if (pid == 0) { else if (pid == 0) {
// Child process - calculator // 子进程 - Calculator
close(pipe_fd[1]); // Close write end of pipe close(pipe_fd[1]); // 关闭管道写端
printf("[Calculator] Started. Waiting for integers...\n"); printf("[Calculator] 启动。等待整数...\n");
while (read(pipe_fd[0], &num, sizeof(int)) > 0) { while (read(pipe_fd[0], &num, sizeof(int)) > 0) {
sum += num; sum += num;
printf("[Calculator] Received: %d, Current Sum: %d\n", num, sum); printf("[Calculator] 收到:%d,当前总和:%d\n", num, sum);
} }
close(pipe_fd[0]); close(pipe_fd[0]);
printf("[Calculator] Exiting.\n"); printf("[Calculator] 退出。\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else { else {
// Parent process - collector // 父进程 - Collector
close(pipe_fd[0]); // Close read end of pipe close(pipe_fd[0]); // 关闭管道读端
printf("[Collector] Started. Enter integers (Ctrl+D to exit):\n"); printf("[Collector] 启动。输入整数(Ctrl+D 退出):\n");
while (scanf("%d", &num) == 1) { while (scanf("%d", &num) == 1) {
if (write(pipe_fd[1], &num, sizeof(int)) == -1) { if (write(pipe_fd[1], &num, sizeof(int)) == -1) {
perror("Write error"); perror("写入错误");
break; break;
} }
printf("[Collector] Sent: %d\n", num); printf("[Collector] 已发送:%d\n", num);
} }
close(pipe_fd[1]); // Close write end to signal EOF to child close(pipe_fd[1]); // 关闭管道写端以向子进程发送EOF信号
printf("[Collector] Waiting for calculator to finish...\n"); printf("[Collector] 等待 Calculator 完成...\n");
// Wait for child to finish // 等待子进程完成
wait(NULL); wait(NULL);
printf("[Collector] Exiting.\n"); printf("[Collector] 退出。\n");
} }
return 0; return 0;
+9 -9
View File
@@ -13,33 +13,33 @@ int main() {
int fd; int fd;
int num; int num;
// Create the FIFO if it doesn't exist // 创建 FIFO 如果不存在
if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) { if (mkfifo(FIFO_PATH, 0666) == -1 && errno != EEXIST) {
perror("mkfifo error"); perror("创建 FIFO 失败");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Collector started. Connecting to calculator...\n"); printf("Collector 已启动,正在连接 Calculator...\n");
// Open FIFO for writing // 打开 FIFO 以写入
fd = open(FIFO_PATH, O_WRONLY); fd = open(FIFO_PATH, O_WRONLY);
if (fd == -1) { if (fd == -1) {
perror("Failed to open FIFO"); perror("打开 FIFO 失败");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
printf("Connected! Enter integers (Ctrl+D to exit):\n"); printf("已连接!输入整数(Ctrl+D 退出):\n");
while (scanf("%d", &num) == 1) { while (scanf("%d", &num) == 1) {
if (write(fd, &num, sizeof(int)) == -1) { if (write(fd, &num, sizeof(int)) == -1) {
perror("Write error"); perror("写入错误");
break; break;
} }
printf("Sent: %d\n", num); printf("已发送:%d\n", num);
} }
close(fd); close(fd);
printf("Collector exiting.\n"); printf("Collector 退出。\n");
return 0; return 0;
} }
+12 -12
View File
@@ -3,30 +3,30 @@
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
volatile int number = 0; // Current even number volatile int number = 0; // 当前偶数
volatile int direction = 1; // 1 for ascending, -1 for descending volatile int direction = 1; // 1 表示递增,-1 表示递减
void handle_sigint(int sig) { void handle_sigint(int sig) {
// Change direction when CTRL+C is pressed // 按下 Ctrl+C 时改变方向
direction = -direction; direction = -direction;
printf("\nChanging direction to %s\n", printf("\n方向已切换为 %s\n",
(direction > 0) ? "ascending" : "descending"); (direction > 0) ? "递增" : "递减");
// Re-register the signal handler // 重新注册信号处理函数
signal(SIGINT, handle_sigint); signal(SIGINT, handle_sigint);
} }
int main() { int main() {
// Register SIGINT handler // 注册 SIGINT 信号处理函数
signal(SIGINT, handle_sigint); signal(SIGINT, handle_sigint);
printf("Starting program: outputting even numbers\n"); printf("程序启动:输出偶数\n");
printf("Press Ctrl+C to change direction\n"); printf("按下 Ctrl+C 可切换方向\n");
while (1) { while (1) {
printf("%d\n", number); printf("%d\n", number);
number += 2 * direction; // Add or subtract 2 based on direction number += 2 * direction; // 根据方向加或减 2
fflush(stdout); // Force output to be displayed immediately fflush(stdout); // 强制立即显示输出
sleep(1); // Wait for 1 second sleep(1); // 等待 1 秒
} }
return 0; return 0;