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