分叉实施
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
卡尔的回答很棒。我想补充一点,在许多操作系统中,返回值是在寄存器之一中传递的。在x86架构中,这个寄存器可能是eax,在ARM架构中,这个寄存器可能是R0等。
每个进程还有一个进程控制块(PCB),它存储在发生一些中断、系统调用或异常时的寄存器值并进行控制被传递给操作系统。下次调度进程时,将从 PCB 恢复寄存器的值。
现在,当 fork() 发生时,操作系统可以执行以下操作:
因此,当重新调度进程时,每个进程都会看到不同的返回值。
作为示例,您可以查看 xv6 的 fork 实现。在那里,父进程仍然处于运行状态,因此它使用简单的 return 语句返回父进程的返回值。但它将子进程的 EAX 寄存器的值设置为 0,因此当子进程被调度时,它会将 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:
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:
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.
你已经通过说这是一个系统调用来解释它了。操作系统的工作就是完成所有这些工作,并且操作系统几乎可以在程序上下文或您实现它所用的任何语言的规则之外做任何它想做的事情。这是一个简单的示例,说明它如何实现发生:
fork()
系统调用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:
fork()
system call在大学的 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:
例如,以简单的方式在
fork()
函数中克隆进程,并使用移动 IP/EIP/RIP 寄存器来跳过函数中的某些指令,如下所示:第一个进程将执行第一条指令和从堆栈中弹出函数,第二个进程将启动,但从第二条指令返回 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:First process will execute first instruction and pop function from stack, second process will start but from second instruction returning 0.