Sign in to follow this  
nuclear123

Process Forking

Recommended Posts

nuclear123    119
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

Share this post


Link to post
Share on other sites
Endar    668
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.

Share this post


Link to post
Share on other sites
rip-off    10976
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().

Share this post


Link to post
Share on other sites
nuclear123    119
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?

Share this post


Link to post
Share on other sites
Nitage    1107
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.

Share this post


Link to post
Share on other sites
stonemetal    288
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this