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
Accepted Answers: fork() by default creates a child process with same file descriptors while CreateProcess() does not. fork() duplicates the program for different process. CreateProcess() creates different process with new program. CreateProcess() is more efficient than fork() then exec() without copy-on-write.
The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.
Microsoft Windows does not support the fork-exec model, as it does not have a system call analogous to fork() . The spawn() family of functions declared in process. h can replace it in cases where the call to fork() is followed directly by exec() .
Each invocation of fork() results in two processes, the child and the parent.
They do different things, and on different systems. CreateProcess
is a Windows-only function, while fork
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 the fork
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
and fork
.
To summarize: CreateProcess
is similar to fork()
followed by one of the exec()
functions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With