我有一个与以下代码相关的问题。这是在这个页面上找到的一个示例,而不是我的代码。
父进程分叉出两个子进程,每个子进程计数到200然后退出。我不理解的是为什么孩子们不会在他们被分叉后立即打印他们的消息,而是允许他们的父亲进入等待状态?此外,等待系统调用如何返回先完成的子进程的pid? pid = wait(&status);
父进程分叉出两个子进程,每个子进程计数到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()
会创建新的进程(而不是销毁第一个进程),并且多个进程可以同时运行?如果是这样,我不确定你在问什么。 - Dmitrifork()
创建了另一个进程,在我们的情况下,因为它是子进程,所以进入了ChildProcess("First", " ");
。现在,为什么它没有从1打印到200呢?为什么消息*** Parent enters waiting status .....
没有在1-200之间或最后显示? - Shurysys/wait.h
,stdlib.h
和unistd.h
)并修复了main函数的返回类型(应该是int
)之后,它对我来说可以正常工作...所有三个进程都运行,它们的输出交错等。新进程需要一段时间才能启动(例如,第一个子进程在第二个开始计数之前计数到147)...但这是预期的,因为启动新进程涉及开销。 - Dmitri