我的任务是编写一个C程序(“procs.c”),它创建了三个进程:一个创建两个子进程的父进程。
第一个孩子应该做到以下几点:
显示“第一个孩子出生,我的pid是......”
显示十次“第一个孩子执行迭代X”的消息,其中X是迭代次数
显示“第一个孩子安静地死去。”
第二个孩子应该做到以下几点:
显示“第二个孩子出生,我的pid是......”
显示十次“第二个孩子执行迭代X”的消息,其中X是迭代次数
显示“第二个孩子安静地死去。”
父进程应执行以下操作:
显示“父进程诞生,我的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");
}
}
任何人都可以帮助我,我不确定是什么问题,因为我继续编码,对我来说似乎是对的。
答案 0 :(得分:1)
wait
函数接受一个参数,一个指向int
的指针。省略参数意味着可以传递一些随机值,这意味着wait
将尝试将子进程的退出状态写入某个随机内存位置,从而导致程序崩溃。一个简单的解决方法是传递NULL
:
wait(NULL);
如果您编译了启用了警告的程序,编译器应该警告您。例如,gcc
会告诉您:
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]