0

我的任务是编写一个创建三个进程的 C 程序(“procs.c”):一个创建两个子进程的父进程。

第一个孩子应该做到以下几点:

  • 显示“第一个孩子出生,我的 pid 是……”

  • 显示十次消息“第一个孩子执行迭代 X”,其中 X 是迭代的次数

  • 显示“第一个孩子安静地死去。”

第二个孩子应该做到以下几点:

  • 显示“第二个孩子出生了,我的pid是……”

  • 显示十次消息“第二个孩子执行迭代 X”,其中 X 是迭代的次数

  • 显示“第二个孩子安静地死去。”

父进程应执行以下操作:

  • display "父进程诞生,我的pid是..."

  • 创建第一个孩子

  • 创建第二个孩子

  • 显示“父进程安静地死掉。”

使用 gcc 编译程序并将可执行文件命名为“procs”。多次执行程序,注意两个孩子的输出是如何交错的。属于项目这一部分的所有文件都应该放在你的 nova 主目录中,并在 H3 分配下发布在 Webtycho 中。

该程序的一个可能输出是:

nova> ./procs

Parent process is born, my pid is 7847
First child is born, my pid is 7848    
First child executes iteration: 1
First child executes iteration: 2
First child executes iteration: 3
First child executes iteration: 4
First child executes iteration: 5
Second child is born, my pid is 7849
Second child executes iteration 1
Second child executes iteration 2
Second child executes iteration 3
First child executes iteration: 6
Second child executes iteration 4
Second child executes iteration 5
Second child executes iteration 6
First child executes iteration: 7
Second child executes iteration 7
Second child executes iteration 8
Second child executes iteration 9
Second child executes iteration 10
Second child dies quietly.
First child executes iteration: 8
First child executes iteration: 9
First child executes iteration: 10
First child dies quietly.    
Parent process dies quietly.

但是我得到的当前输出是:

nova2> ./procs
Parent process is born, my pid is: 1977
First child is born, my pid is: 1978
First child executes iteration: 1
First child executes iteration: 2
First child executes iteration: 3
First child executes iteration: 4
First child executes iteration: 5
First child executes iteration: 6
First child executes iteration: 7
First child executes iteration: 8
First child executes iteration: 9
First child dies quietly
Second child is born, my pid is: 1979
Second child executes iteration: 1
Second child executes iteration: 2
Second child executes iteration: 3
Second child executes iteration: 4
Second child executes iteration: 5
Second child executes iteration: 6
Second child executes iteration: 7
Second child executes iteration: 8
Second child executes iteration: 9
Second child dies quietly
Bus Error (core dumped)

我现在的代码是:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main() {
    int pid, i;

    printf("Parent process is born, my pid is: %d\n", getpid());

    pid = fork();
    switch(pid) {
    case -1:
        /* an error occurred */
        printf("Fork error");
        break;

    case 0:
        /* this code is executed by child process #1*/
        printf("First child is born, my pid is: %d\n", getpid());

        for(i=1; i<10; i++)
            printf("First child executes iteration: %d\n", i);

        printf("First child dies quietly\n");

        exit(0);

    default: /* this code is executed by the parent process */

        if(fork()==0) {
            printf("Second child is born, my pid is: %d\n", getpid());

            for(i=1; i<10; i++)
                printf("Second child executes iteration: %d\n", i);

            printf("Second child dies quietly\n");
            exit(0);
        }

        wait();

        printf("Parent process dies quietly.\n");

    }
}

任何人都可以帮助我,我不确定出了什么问题,因为我一直在检查编码,这对我来说似乎是正确的。

4

1 回答 1

1

The wait function takes a parameter, a pointer to an int. Omitting the parameter means some random value may be passed, which means wait will try to write the exit status of the child process to some random memory location, crashing the program. A simple fix is passing NULL:

wait(NULL);

If you compiled your program with warnings enabled the compiler should warn you about this. For example gcc would tell you:

test.c: In function ‘main’:
test.c:40:9: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
test.c:45:1: warning: control reaches end of non-void function [-Wreturn-type]
于 2013-06-21T18:47:27.490 回答