Process Forking

Started by
6 comments, last by stonemetal 13 years, 4 months ago
im curious as to when "process Forking" would be usefull/used?

The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process.

In computing, when a process forks, it creates a copy of itself. More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread


-thx
Advertisement
When you fork() a process, it creates a process identical to the previous one, except for the process ID. Then you can use a function like exec(), and it's related functions, to actually load and start an executable running, in that new process space.

link: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html

Basically whenever an executable in your system starts running, the OS does a fork() and then exec(). This happens for every program that is run on your computer.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
fork() is cheap on *nix systems. Processes use copy-on-write memory to avoid needing to copy large amounts of data into the child during the fork(). Processes are isolated from one another in the event of a crash, unlike a thread which usually brings the entire process down.

It is usually easier to debug and maintain fork() programs rather than multi-threaded programs - such programs generally have either one or no communication channels and the channel is inherently asynchronous. There is no chance of the two processes stomping on each others memory or reading data which is inconsistent and out of date.
Quote:
Basically whenever an executable in your system starts running, the OS does a fork() and then exec(). This happens for every program that is run on your computer.

On *nix systems. On Windows, there is no fork(), only CreateProcess().
Quote:It is usually easier to debug and maintain fork() programs rather than multi-threaded programs - such programs generally have either one or no communication channels and the channel is inherently asynchronous. There is no chance of the two processes stomping on each others memory or reading data which is inconsistent and out of date.


hmm interesting! in windows does each program process have it's own data section in main memory? and they both share the same code section in main memory?
In Windows, every process has its own isolated address space. Neither the data nor the code is shared.

The difference is that Windows processes start with a clean address space, unix ones inherit a copy of their parent processes address space.
if the code section is not shared in main memory then your saying the program would be loaded 2 seperate times in memory :/?? i dont think that's correct!
Read only pages can be shared in Windows, I believe DLL and executable code and read-only data will be shared.
It is useful when you want to write a server that does parallel processing but you don't want to deal with threads and locking. It is used quite often in server side software in Unix. An example would be the Postgresql database it loads up a master process that then forks of a gang of worker processes.

I believe rip-off is correct about windows sharing data, however it is important to note that starting a new process in windows is much slower than starting a new process on most unix systems (Mac OS X being the only unix OS I know of that is as slow or slower than windows at process creation). So cross platform servers tend to support both or do threads only. As an example Apache started as a fork only server(the 1.x branch) but as windows support became more important threading support was added(the 2.x branch).

This topic is closed to new replies.

Advertisement