У меня есть вопрос о следующем коде. Это пример, найденный на this page, а не мой код.вызовы fork() и wait()
Родительский процесс выдает 2 дочерних процесса, каждый из которых составляет 200, а затем выходит. Я не понимаю, почему дети не печатают свои сообщения сразу после того, как они разветвлены, и они позволяют своему отцу войти в состояние ожидания? Также, как вызов системы ожидания возвращает pid ребенка, который заканчивается первым?
pid = wait (& status);
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#define MAX_COUNT 200
#define BUF_SIZE 100
void ChildProcess(char [], char []); /* child process prototype */
void main(void)
{
pid_t pid1, pid2, pid;
int status;
int i;
char buf[BUF_SIZE];
printf("*** Parent is about to fork process 1 ***\n");
if ((pid1 = fork()) < 0) {
printf("Failed to fork process 1\n");
exit(1);
}
else if (pid1 == 0)
ChildProcess("First", " ");
printf("*** Parent is about to fork process 2 ***\n");
if ((pid2 = fork()) < 0) {
printf("Failed to fork process 2\n");
exit(1);
}
else if (pid2 == 0)
ChildProcess("Second", " ");
sprintf(buf, "*** Parent enters waiting status .....\n");
write(1, buf, strlen(buf));
pid = wait(&status);
sprintf(buf, "*** Parent detects process %d was done ***\n", pid);
write(1, buf, strlen(buf));
pid = wait(&status);
printf("*** Parent detects process %d is done ***\n", pid);
printf("*** Parent exits ***\n");
exit(0);
}
void ChildProcess(char *number, char *space)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
pid = getpid();
sprintf(buf, "%s%s child process starts (pid = %d)\n",
space, number, pid);
write(1, buf, strlen(buf));
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, "%s%s child's output, value = %d\n", space, number, i);
write(1, buf, strlen(buf));
}
sprintf(buf, "%s%s child (pid = %d) is about to exit\n",
space, number, pid);
write(1, buf, strlen(buf));
exit(0);
}
Выход для MAX_COUNT 40.
*** Parent is about to fork process 1 ***
*** Parent is about to fork process 2 ***
*** Parent enters waiting status .....
First child process starts (pid = 3300)
First child's output, value = 1
Second child process starts (pid = 3301)
Second child's output, value = 1
First child's output, value = 2
Second child's output, value = 2
Second child's output, value = 3
First child's output, value = 3
Second child's output, value = 4
First child's output, value = 4
Second child's output, value = 5
First child's output, value = 5
Second child's output, value = 6
First child's output, value = 6
Second child's output, value = 7
First child's output, value = 7
Second child's output, value = 8
First child's output, value = 8
Second child's output, value = 9
First child's output, value = 9
Second child's output, value = 10
First child's output, value = 10
Second child's output, value = 11
First child's output, value = 11
Second child's output, value = 12
First child's output, value = 12
Second child's output, value = 13
First child's output, value = 13
Second child's output, value = 14
First child's output, value = 14
Second child's output, value = 15
First child's output, value = 15
Second child's output, value = 16
First child's output, value = 16
Second child's output, value = 17
First child's output, value = 17
Second child's output, value = 18
First child's output, value = 18
Second child's output, value = 19
First child's output, value = 19
Second child's output, value = 20
First child's output, value = 20
Second child's output, value = 21
First child's output, value = 21
Second child's output, value = 22
First child's output, value = 22
Second child's output, value = 23
First child's output, value = 23
Second child's output, value = 24
First child's output, value = 24
Second child's output, value = 25
First child's output, value = 25
Second child's output, value = 26
First child's output, value = 26
Second child's output, value = 27
First child's output, value = 27
Second child's output, value = 28
First child's output, value = 28
Second child's output, value = 29
First child's output, value = 29
Second child's output, value = 30
First child's output, value = 30
Second child's output, value = 31
First child's output, value = 31
Second child's output, value = 32
First child's output, value = 32
Second child's output, value = 33
First child's output, value = 33
Second child's output, value = 34
First child's output, value = 34
Second child's output, value = 35
First child's output, value = 35
Second child's output, value = 36
First child's output, value = 36
Second child's output, value = 37
First child's output, value = 37
Second child's output, value = 38
First child's output, value = 38
Second child's output, value = 39
First child's output, value = 39
Second child's output, value = 40
First child's output, value = 40
Second child (pid = 3301) is about to exit
First child (pid = 3300) is about to exit
*** Parent detects process 3300 was done ***
*** Parent detects process 3301 is done ***
*** Parent exits ***
Почему *** Parent enters waiting status .....
линия отображается в начале, а не где-то после того, как Чайлдс начать печать?
Что вы подразумеваете под «почему дети не печатают свои сообщения сразу после того, как они разветвлены, и они позволяют своему отцу войти в состояние ожидания?» в точку. Не совсем ясно, каков ваш вопрос сейчас. –
Знаете ли вы, что 'fork()' создает новые процессы (без разрушения первого) и что несколько процессов могут выполняться одновременно? Если так, я не уверен, что вы просите. – Dmitri
@ ian-sellar @dmitri 'fork()' создает другой процесс, который в нашем случае, потому что он является дочерним, вводит 'ChildProcess (« First »,« »);'. Прямо сейчас, почему он не печатает от 1 до 200? Почему сообщение «*** Родитель входит в состояние ожидания .....» не отображается где-то между 1-200 или в конце? – Shury