Are forking() and CreateProcess(with all required arguments), the same thing for Linux and WinXP, respectively?
If they are different, then could someone explain the difference in terms of what happens in each of the two cases?
Thanks
Are forking() and CreateProcess(with all required arguments), the same thing for Linux and WinXP, respectively?
If they are different, then could someone explain the difference in terms of what happens in each of the two cases?
Thanks
I will give two examples to show the difference:
fork()
:In this program, fork will do a copy and return two values. We called the copied process parent process and the other one child process.If we call the exec() function, the whole process will be replaced by a new program except the PID.
CreateProcess()
:This is an example from MSDN. What we call to create a new process must be a separate *.exe program in Windows system. The new process is a whole new one which just has the only connection of return value with the older one.
In conclusion, we often see
fork()+exec()
asCreateProcess()
. In fact,fork()
is more similar withCreateThread()
in Windows.CreateProcess takes the following steps:
Unix's fork takes the following steps:
It creates a complete copy of the parent process, and the parent doesn't set up the runtime environment for the child, because the parent process trusts its own set up. The child is a complete copy of the parent except for its process id (what fork returns). A forked process continues to run the same program as its parent until it performs an explicit exec. When the child calls exec which, the new executable image into memory and runs.
How is it efficient to make a complete copy? copy-on-write. It really only copies the virtual memory map. All of the segments in the segment table are read only. If the parent or child edits the data in a segment, an exception is thrown and the kernel creates a full memory copy of that. This is explained nicely in this answer
There are several benefits to shared resources between parent and child: - intuitively, resource management: less memory is needed to maintain the states of the processes - Cache resources are shared means greater temporal locality of data when data is not over written, which improves performance because retrieving data from larger caches/disk is time consuming.
Disadvantages to shared resources: - when writes are common, it puts the data in an invalid state for the other process, and this leads to coherency misses which is costly if the child process is running on a separate core, because the changes will have to propagate up to the L3 cache.
In general though, programs read a heck of a lot more than writes, typically the child/parent will only need to make writes to its stack, and that's a small portion of the their program block.
Additionally Unix fork is different because it returns twice, once in the parent (the process id of its child), once in the child (0, congrats you're a new baby process), which is how we distinguish in our code if we are the child or parent.
Unix Exec does the following:
the parent has the option to wait for the child to finish. When the child finishes, when exit is called is when the parent's wait is notified.
They do different things, and on different systems.
CreateProcess
is a Windows-only function, whilefork
is only on POSIX (e.g. Linux and Mac OSX) systems.The
fork
system call creates a new process and continue execution in both the parent and the child from the point where thefork
function was called.CreateProcess
creates a new process and load a program from disk. The only similarity is that the end result is a new process is created.For more information, read the respective manual page on
CreateProcess
andfork
.