分叉实施

发布于 2024-12-27 02:18:27 字数 80 浏览 1 评论 0原文

fork系统调用代码是怎么写的。我想知道一个函数如何返回两个不同的值以及如何返回两个不同的进程的一些细节。总之想知道fork系统调用是如何实现的?

How is fork system call code written . I want to know some details how a function can return two different values and that to two different processes . In short want to know how fork system call is implemented?

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

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

发布评论

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

评论(4

绻影浮沉 2025-01-03 02:18:27

卡尔的回答很棒。我想补充一点,在许多操作系统中,返回值是在寄存器之一中传递的。在x86架构中,这个寄存器可能是eax,在ARM架构中,这个寄存器可能是R0等。

每个进程还有一个进程控制块(PCB),它存储在发生一些中断、系统调用或异常时的寄存器值并进行控制被传递给操作系统。下次调度进程时,将从 PCB 恢复寄存器的值。

现在,当 fork() 发生时,操作系统可以执行以下操作:

 child_process->PCB[return_value_register] = 0;
 parrent_process->PCB[return_value_register] = child_pid;

因此,当重新调度进程时,每个进程都会看到不同的返回值。

作为示例,您可以查看 xv6 的 fork 实现。在那里,父进程仍然处于运行状态,因此它使用简单的 return 语句返回父进程的返回值。但它将子进程的 EAX 寄存器的值设置为 0,因此当子进程被调度时,它会将 0 视为返回值:

// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;

请注意,return 0 也会编译为类似“mov eax, 0”的内容。

更新:我刚刚为我正在做的一个业余爱好操作系统实现了 fork() 。您可以在此处查看源代码。

Carl's answer was great. I'd like to add that in many operating systems return values are passed in one of the registers. In x86 architecture this register might be eax, In ARM architecture this register might be R0, etc.

Each process also have a Process Control Block (PCB), which store values of registers at the time some interrupt, syscall, or exception happened and control was passed to the OS. The next time the process scheduled, the values of the registers are restored from PCB.

Now, when fork() happens, OS can do:

 child_process->PCB[return_value_register] = 0;
 parrent_process->PCB[return_value_register] = child_pid;

So, when the processes are rescheduled, each of them see a different return value.

As an example, you can see xv6's implementation of fork. In there, the parent process is still in running state, so it returns parent's return value using simple return statement. But it sets value of EAX register for child process to 0, so when child process is scheduled it sees 0 as return value:

// Clear %eax so that fork returns 0 in the child.
np->tf->eax = 0;

Note that return 0 will also compile to something like "mov eax, 0".

Update: I just implemented fork() for a hobby OS I am doing. You can see the source code here.

把梦留给海 2025-01-03 02:18:27

你已经通过说这是一个系统调用来解释它了。操作系统的工作就是完成所有这些工作,并且操作系统几乎可以在程序上下文或您实现它所用的任何语言的规则之外做任何它想做的事情。这是一个简单的示例,说明它如何实现发生:

  1. 程序调用 fork() 系统调用
  2. 内核 fork 系统调用复制运行该程序的进程
  3. 内核为原始程序和重复程序设置系统调用的返回值(重复程序和重复程序的 PID)分别为 0)
  4. 内核将调度程序队列中的两个进程
  5. 当每个进程被调度时,内核“返回”到两个程序中的每一个。

You've pretty much explained it by saying that it's a system call. It's the operating system's job to do all that work, and the operating system can pretty much do whatever it wants outside of the context of your program or the rules of whatever language you're implementing it in. Here's a simple example of how it might happen:

  1. Program calls fork() system call
  2. Kernel fork system call duplicates the process running the program
  3. The kernel sets the return value for the system call for the original program and for the duplicate (PID of the duplicate and 0, respectively)
  4. The kernel puts both processes in the scheduler queue
  5. As each process is scheduled, the kernel 'returns' to each of the two programs.
分開簡單 2025-01-03 02:18:27

在大学的 Unix V6 源代码小册子中有一条评论,由 Ken Thompson 和 Dennis 注释里奇自己描述了双重回报实际上是如何运作的。评论以以下句子结尾:

您不应该理解这一点。

There is a comment in the Unix V6 source code booklet for universities which was annotated by Ken Thompson and Dennis Ritchie themselves describing how the double return actually works. The comment ends with following sentence:

You are not expected to understand this.

心安伴我暖 2025-01-03 02:18:27

例如,以简单的方式在 fork() 函数中克隆进程,并使用移动 IP/EIP/RIP 寄存器来跳过函数中的某些指令,如下所示:

return pid;
return 0;

第一个进程将执行第一条指令和从堆栈中弹出函数,第二个进程将启动,但从第二条指令返回 0。

In easy way for example process is cloned in fork() function with Moving IP/EIP/RIP register to skip some instruction in functions that can look like:

return pid;
return 0;

First process will execute first instruction and pop function from stack, second process will start but from second instruction returning 0.

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