You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

父子进程管道通信循环输入问题求助:正整数处理负整数退出

问题修复方案

原代码核心问题

  • 输入逻辑仅执行一次,未做循环处理,无法持续接收用户输入
  • 循环条件逻辑错误,输入负数直接退出,但正整数场景未实现循环流程
  • 进程通信逻辑完全颠倒:需求是父进程传数给子进程求平方根,原代码却是子进程传数、父进程计算平方
  • 父进程处理逻辑中存在return 0,执行一次就直接终止程序

修正后的代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <math.h>

int main(int argc, char *argv[]) {
    int x;
    // 主循环:持续请求用户输入
    while (1) {
        printf("Enter an Integer (-ve to quit): ");
        scanf("%d", &x);

        // 输入负数则退出循环
        if (x < 0) {
            break;
        }

        // 创建两个单向管道:父→子 传整数,子→父 传平方根结果
        int fd_parent_to_child[2];
        int fd_child_to_parent[2];
        if (pipe(fd_parent_to_child) == -1 || pipe(fd_child_to_parent) == -1) {
            printf("Failed to create pipes\n");
            return 1;
        }

        // 创建子进程
        int pid = fork();
        if (pid == -1) {
            printf("Failed to fork process\n");
            return 2;
        }

        if (pid == 0) {
            // 子进程逻辑:读取整数→计算平方根→返回结果
            close(fd_parent_to_child[1]); // 关闭父→子管道的写端
            close(fd_child_to_parent[0]); // 关闭子→父管道的读端

            int input_num;
            // 读取父进程传来的整数
            if (read(fd_parent_to_child[0], &input_num, sizeof(int)) == -1) {
                printf("Child process read error\n");
                return 3;
            }
            close(fd_parent_to_child[0]);

            // 计算平方根(用double保证精度)
            double sqrt_result = sqrt(input_num);

            // 将结果写回父进程
            if (write(fd_child_to_parent[1], &sqrt_result, sizeof(double)) == -1) {
                printf("Child process write error\n");
                return 4;
            }
            close(fd_child_to_parent[1]);
            exit(0);
        } else {
            // 父进程逻辑:发送整数→读取结果→展示输出
            close(fd_parent_to_child[0]); // 关闭父→子管道的读端
            close(fd_child_to_parent[1]); // 关闭子→父管道的写端

            // 发送输入的整数给子进程
            if (write(fd_parent_to_child[1], &x, sizeof(int)) == -1) {
                printf("Parent process write error\n");
                return 5;
            }
            close(fd_parent_to_child[1]);

            // 读取子进程返回的平方根结果
            double final_result;
            if (read(fd_child_to_parent[0], &final_result, sizeof(double)) == -1) {
                printf("Parent process read error\n");
                return 6;
            }
            close(fd_child_to_parent[0]);

            // 打印结果
            printf("Square root of %d is %.2f\n", x, final_result);

            // 等待子进程结束,避免僵尸进程
            wait(NULL);
        }
    }

    printf("Program exited\n");
    return 0;
}

关键修改说明

  1. 循环输入逻辑:将请求输入的代码放入while(1)循环,输入负数时通过break退出循环,实现持续交互
  2. 双向管道通信:使用两个单向管道分别处理父→子的整数传递和子→父的结果返回,解决单向管道无法双向通信的问题
  3. 修正计算逻辑:由子进程负责计算平方根(使用double类型保留小数精度),符合需求设定
  4. 资源清理:每次处理完成后关闭管道的闲置端,调用wait(NULL)等待子进程结束,避免资源泄漏和僵尸进程

内容的提问来源于stack exchange,提问作者BearB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:45:36