Fork implementation
Asked Answered
L

4

13

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?

Loveinamist answered 13/1, 2012 at 22:2 Comment(2)
You can try and look at the Linux kernel source code...Gabbard
Are you comfortable with the idea that the OS can create processes, and can choose what areas of memory to map to each process's address space?Dashboard
E
15

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.

Electrolyze answered 21/8, 2014 at 15:7 Comment(0)
R
12

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.
Ridicule answered 13/1, 2012 at 22:5 Comment(0)
G
4

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.

Galahad answered 13/1, 2012 at 22:25 Comment(1)
IIRC that referred to the assembly used for that, not to the idea of returning twice itself.Panda
I
-3

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.

Immensurable answered 13/1, 2012 at 22:5 Comment(2)
I'd think it's just the kernel returning different values from the system call for each process.Dissertation
Both process fork from the same point when you call fork(). I don't know what you mean by the "second" process(parent or child) but it's incorrectMilstone

© 2022 - 2024 — McMap. All rights reserved.