fork() 到底发生了什么?

发布于 2025-01-08 05:19:13 字数 259 浏览 1 评论 0原文

int main(){

    char ch;

    fork();

    cin >> c;
}

调用 fork() 后,我应该有 2 个完全相同的进程运行相同的代码。为什么运行这个简单的示例后,我要么只被要求输入一个字符一次,要么被要求输入两次?每次我运行这个程序时,系统不应该期望有 2 个输入吗?

>./a.out 
a
>./a.out
a
b
>
int main(){

    char ch;

    fork();

    cin >> c;
}

After calling fork() I should have 2 exact processes running the same code. Why after running this simple example, I am either asked to enter a character only once, either twice? Shouldn't the system expect 2 inputs every single time I run this program?

>./a.out 
a
>./a.out
a
b
>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箜明 2025-01-15 05:19:13

您有两个进程同时从终端读取数据。任何人都可以猜测哪个进程获取输入。

  • 如果父进程首先获得输入,它将退出并将控制权返回给 shell。 (请注意,这实际上会导致相同情况的重复,即 shell 和子进程争夺输入。)
  • 如果子进程首先获得输入,则它会退出,但控制权不会返回到 shell,直到父进程退出。

如果有两个进程从同一终端读取数据,则不应期望一致的行为。

You have two processes reading from the terminal at the same time. It is anybody's guess which process gets the input.

  • If the parent process gets the input first, it exits and returns control to the shell. (Note that this actually causes a repeat of the same situation, with shell and the child process fighting for input.)
  • If the child process gets the input first, it exits but control does not return to the shell untill the parent process exits.

You should not expect consistent behavior if you have two processes reading from the same terminal.

沩ん囻菔务 2025-01-15 05:19:13

当调用 fork() 时,操作系统通常会复制(某种程度上)正在执行的程序的整个内存空间。然后两个程序都会运行。唯一的区别是,在“新”进程中,fork() 返回 0,而在“旧”进程中,它返回新进程的进程 ID。

只要求您提供一项输入的原因是其中一个程序正在后台运行。命令行 shell 一次仅对一个进程执行 I/O。

When fork() is called, the operating system typically copies the entire memory space of the executing program (sort of). Both programs then run. The only differences is that in the "new" process, fork() returns 0, and in the "old" process it returns the process ID of the new process.

The reason that you're only asked for one input is that one of the programs is running in the background. The command-line shell only does I/O for one process at a time.

私野 2025-01-15 05:19:13

fork() 创建一个子进程。

但哪个进程(父进程和新出生的子进程)获得 CPU 切片尚未确定。当两个进程都被阻止键盘输入时,子进程或父进程都可以获得输入。
如果父进程获得令牌,它将输入读取到其地址空间中定义的变量中并退出。孩子永远没有机会阅读输入内容。然后这个孤立的子进程将被“root”进程(pid=1)采用。请参阅 ps 输出。

在另一种情况下,当子进程获取令牌并读取数据并退出时,父进程仍然处于活动状态,因此会再次阻止输入。

fork() creates a child process.

But which process (among the parent and the newly born child) gets the CPU slice is undetermined. When both the processes are blocked for keyboard input, either the child or the parent can get the input.
If the parent gets the token, it reads the input into its variable defined in its address space and exits. And the child never gets a chance to read from the input. And this orphaned child process will then be adopted by the 'root' process (pid=1). See ps output.

And in the other case, where the child gets the token and reads the data and exits, the parent is still alive and hence blocks for input again.

紫轩蝶泪 2025-01-15 05:19:13

在 fork() 之后添加 wait() 并尝试。

Include a wait() after fork() and try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文