【问题标题】:Why am I getting large reaped ints?为什么我得到大收获?
【发布时间】:2026-02-21 17:05:02
【问题描述】:

我正在开发一个类项目,该项目应该从命令行获取数字,fork,然后将它们传递给子进程添加,然后父进程应该获得数字的总和。它似乎工作得很好,除非当我收获子进程时,它给了我很大的数字,而不是它最初加在一起的数字的总和。如果我只输入 0,我得到 0,如果我输入 1,我得到 256,依此类推。我错过了什么,我只是没有正确收获吗?谢谢。

enter code // Numbers from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process adds up numbers sent through pipe.
//
// Child process returns sum of numbers to parent process.
//
// Parent process prints sum of numbers.


static int  com[2];

int main(int argc, char **argv)
{
    pid_t   pid;

    // set up pipe

    if (pipe(com))
    {
       printf("pipe error\n");
       return -1;
     }

    //call fork()

    pid = fork();

    if (pid == 0)
    {
        // -- running in child process --

        int             sum = 0;
        int             input = 0;

        //close output end of pipe

        close(com[1]);

        // Receive characters from parent process via pipe
        // one at a time, and count them.

        for (int idx = 1; idx < argc; idx++) //stared idx at 1 instead of 0
        {
                read(com[0], &input, 4); //changed from 4
                sum = sum + input;
        }
                printf("child sum: %i \n", sum); // error checking

        // Return sum of numbers.

        return sum;
    }
    else {
            // -- running in parent process --

            int sum;

            //close output end of pipe

            close(com[0]);

            // Send numbers (datatype: int, 4 bytes) from command line arguments
            // starting with argv[1] one at a time through pipe to child process.

            for (int idx = 1; idx < argc; idx++)
            {
                    int output = 0;

                    output = atoi(argv[idx]);
                    write(com[1], &output, 4);
                    printf("output: %i \n", output);// error checking
            }

            close(com[1]);

            // Wait for child process to return. Reap child process.
            // Receive sum of numbers via the value returned when
            // the child process is reaped.

            waitpid(pid, &sum, 0);

            printf("sum = %d\n", sum);

            return 0;
       }
 }

这里

【问题讨论】:

  • 似乎它正在转移我的位置。
  • 退出状态存储在退出状态的高 8 位中,有效地将您的总和乘以 256。您的总和也限制为 255。
  • 你最好阅读子进程的标准输出。首先是更容易测试,它允许您接受大于 256 的总和。命令返回的 status 仅用作状态:将其用于错误条件,但将 stdout 用于有意义的数据.

标签: c fork posix


【解决方案1】:

来自man page

如果 status 不为 NULL,wait() 和 waitpid() 将状态信息存储在它指向的 int 中。可以使用以下宏检查此整数

其中两个是

WIFEXITED(status)
    returns true if the child terminated normally, that is, by
    calling exit(3) or _exit(2), or by returning from main().

WEXITSTATUS(status)
    returns the exit status of the child. This consists of the
    least significant 8 bits of the status argument that the child
    specified in a call to exit(3) or _exit(2) or as the argument
    for a return statement in main(). This macro should only be
    employed if WIFEXITED returned true.

【讨论】: