现在,我在理解fork()系统调用的工作方面遇到了问题。我编写了如下代码:
#include<stdio.h>
int main()
{
int a, b;
b=fork();
printf("\n the value of b = %d",b);
}
此代码的输出如下:
现在我不明白为什么输出是这样的?
之后,我只需在代码中添加一行代码,输出就完全不同了。我的代码如下:
int main()
{
int a, b;
b=fork();
当我运行代码时,输出如下2389,我的名字是manish
the value of b
我当时正在阅读Linux开发,并试图理解fork()情况下的进程地址空间语义。当我在Kernel v2.6的上下文中阅读时,以及在新版本中,任何子版本或父版本都可能先运行,但我对以下内容感到困惑:
Back in do_fork(), if copy_process() returns successfully, the new child is woken up
and run. Deliberately, the kernel runs the child process first. In the common case of the
child simply calling exec(
我正在尝试调试一个在处理请求时派生的进程。
我已经将follow-fork-mode设置为child,允许gdb在命中子对象中的断点时闯入。但是,当我继续从子进程中继续时,父进程就分离了。
很多文档都指向选项detach-on-fork,但是如果我尝试设置这个选项,我会得到:
(gdb) set detach-on-fork on
No symbol "detach" in current context.
该选项的链接清晰可见:
编辑:系统信息
>uname -a
Linux localhost 2.6.9-78.0.22.ELsmp #1 SMP Thu Apr 3
使用之前版本的octave,我运行的是
pid = fork();
问题不多。现在,使用octave 4.2.1时,我得到了以下错误:
error: fork: cannot be called from command line
系统信息: Ubuntu 16.10Linux 4.8.0-54-generic #57-Ubuntu SMP Wed May 24 10:21:44 UTC 2017 x86_64 GNU/Linux