Мое назначение - написать программу на языке C («procs.c»), которая создает три процесса: родительский процесс, который создает два дочерних процесса.Процессы работают неправильно
Первый ребенок должен сделать следующее:
дисплея «Первый ребенок родился, мой ПИД- ...»
дисплей десяти раз сообщения «Первый ребенок выполняет итерацию X» , где X - номер итерации
дисплей «Первый ребенок умирает тихо».
Второй ребенок должен сделать следующее:
дисплея «Второй ребенок родился, мой ПИД- ...»
дисплея десяти раз сообщения «Второй ребенок Выполняет итерация X ", где X - номер итерации
дисплей« Второй ребенок умирает тихо ».
Родительский процесс должен выполнить следующие действия:
дисплей "процесс Родитель родился, мой ПИД- ..."
создать первый ребенок
создать второго ребенка
дисплей "Parent процесс умирает спокойно ».
Скомпилировать программу, используя gcc и назовите исполняемый файл "procs". Выполните программу несколько раз и обратите внимание на то, как чередуются выходные данные двух детей. Все файлы, принадлежащие этой части проекта, должны быть помещены в ваш домашний каталог nova, а также размещены в Webtycho под назначением H3.
Возможный выход этой программы:
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");
}
}
Может кто-нибудь помочь я не уверен, что не так, потому что я продолжаю кодирование, и мне кажется правильным.
Этот код выглядит корректно и работает правильно. Похоже, что у вас проблемы со средой. Единственная возможная проблема с кодом, которую я вижу, - это: Вы уверены, что '% d' - код правильного формата для' pid_t'? Он может быть больше или меньше, чем 'int'. –
Я использую NOVA, и Windows 8 может быть проблемой? –
Я ничего не знаю о NOVA. –