创建了多少进程?
How many processes are created?
我认为这些创建了 5 个进程,但我需要您的验证。
我对这类问题感到困惑。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
if (fork()){
printf("A\n");
exec("execjune");
}
else{
printf("B\n");
fork();
fork();
exec("execjune");
}
printf("D\n");
return 0;
}
正在将评论转为答案。
当你 运行 时会发生什么? execjune
程序是什么?没有标准的 C 或 POSIX 函数称为 exec()
(标准名称都有 1-3 个后缀字母,例如 execve()
); exec()
做什么?
If the following program is executed, and if execjune
executes printf("C\n")
. I got this question from my professor, so I need to ask how many processes are created with these fork()
statements.
在研究这样的代码行为时,我发现添加 printf()
打印 PID 和显着信息的调用很有帮助。并调用 fflush()
以确保按预期生成输出。创建一个函数来封装工作。如果没有 运行 代码,并且考虑到 exec()
是 execve()
的变体并且 execjune
打印“C”,我希望在中看到 A、B 和五个 C输出,没有 D。
Why D is 0? I think that D is printed by main()
method or …?
如果 exec()
函数是 execve()
的变体(而不是 system()
的变体),那么 if
和 else
块将显示的代码替换为execjune
,并没有打印D.
的代码路径
这是一些代码。即使在 Linux(RHEL 7.4 测试)上,在 GCC 10.2.0 的默认编译器选项下也没有函数 exec()
。所以,我创建了一个函数 exec()
,还有一个函数 dbg_print()
,并将它们与代码一起使用(源文件 fork97.c
编译为可执行文件 fork97
):
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void dbg_print(const char *fmt, ...)
{
printf("%d: ", (int)getpid());
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
static inline void exec(const char *cmd)
{
execl(cmd, (char *)0);
}
int main(void)
{
dbg_print("Initial process\n");
if (fork()){
dbg_print("Parent process\n");
printf("A\n");
exec("execjune");
}
else{
dbg_print("Child process\n");
printf("B\n");
fork();
dbg_print("Between consecutive fork() calls\n");
fork();
dbg_print("After consecutive fork() calls\n");
exec("execjune");
}
printf("D\n");
return 0;
}
我还使用了从源代码 execjune.c
创建的程序 execjune
(打印 PID 和字母 C):
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("%d: C\n", (int)getpid());
return 0;
}
当我 运行 主程序时,我得到如下输出:
$ fork97
28876: Initial process
28876: Parent process
A
28877: Child process
B
28877: Between consecutive fork() calls
28877: After consecutive fork() calls
28879: After consecutive fork() calls
28878: Between consecutive fork() calls
28878: After consecutive fork() calls
28880: After consecutive fork() calls
28876: C
28879: C
$ 28880: C
28878: C
28877: C
$
注意28880
打印C
之前的提示$
是因为没有有意义的方式控制所有child进程在[=68]之前退出=] 进程执行(部分原因是 children 全部执行 execjune
)。空白行和最后的 $
提示来自我在最后一个 C 行之后点击 return。
我认为这些创建了 5 个进程,但我需要您的验证。 我对这类问题感到困惑。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
if (fork()){
printf("A\n");
exec("execjune");
}
else{
printf("B\n");
fork();
fork();
exec("execjune");
}
printf("D\n");
return 0;
}
正在将评论转为答案。
当你 运行 时会发生什么? execjune
程序是什么?没有标准的 C 或 POSIX 函数称为 exec()
(标准名称都有 1-3 个后缀字母,例如 execve()
); exec()
做什么?
If the following program is executed, and if
execjune
executesprintf("C\n")
. I got this question from my professor, so I need to ask how many processes are created with thesefork()
statements.
在研究这样的代码行为时,我发现添加 printf()
打印 PID 和显着信息的调用很有帮助。并调用 fflush()
以确保按预期生成输出。创建一个函数来封装工作。如果没有 运行 代码,并且考虑到 exec()
是 execve()
的变体并且 execjune
打印“C”,我希望在中看到 A、B 和五个 C输出,没有 D。
Why D is 0? I think that D is printed by
main()
method or …?
如果 exec()
函数是 execve()
的变体(而不是 system()
的变体),那么 if
和 else
块将显示的代码替换为execjune
,并没有打印D.
这是一些代码。即使在 Linux(RHEL 7.4 测试)上,在 GCC 10.2.0 的默认编译器选项下也没有函数 exec()
。所以,我创建了一个函数 exec()
,还有一个函数 dbg_print()
,并将它们与代码一起使用(源文件 fork97.c
编译为可执行文件 fork97
):
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static void dbg_print(const char *fmt, ...)
{
printf("%d: ", (int)getpid());
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
static inline void exec(const char *cmd)
{
execl(cmd, (char *)0);
}
int main(void)
{
dbg_print("Initial process\n");
if (fork()){
dbg_print("Parent process\n");
printf("A\n");
exec("execjune");
}
else{
dbg_print("Child process\n");
printf("B\n");
fork();
dbg_print("Between consecutive fork() calls\n");
fork();
dbg_print("After consecutive fork() calls\n");
exec("execjune");
}
printf("D\n");
return 0;
}
我还使用了从源代码 execjune.c
创建的程序 execjune
(打印 PID 和字母 C):
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("%d: C\n", (int)getpid());
return 0;
}
当我 运行 主程序时,我得到如下输出:
$ fork97
28876: Initial process
28876: Parent process
A
28877: Child process
B
28877: Between consecutive fork() calls
28877: After consecutive fork() calls
28879: After consecutive fork() calls
28878: Between consecutive fork() calls
28878: After consecutive fork() calls
28880: After consecutive fork() calls
28876: C
28879: C
$ 28880: C
28878: C
28877: C
$
注意28880
打印C
之前的提示$
是因为没有有意义的方式控制所有child进程在[=68]之前退出=] 进程执行(部分原因是 children 全部执行 execjune
)。空白行和最后的 $
提示来自我在最后一个 C 行之后点击 return。