fork?

Not sure where exactly to ask this, but, can anyone tell me and lay down what a fork is and it’s purpose. I kinda know but not very clearly. This seems very very abstract to me, it would be nice if someone gives a nice explanation. I kinda guessing this is a really deep topic, maybe dealing with as deep as even CPU architecture(?) I’m learning about Linux and I’m finding it tough to understand this part.

PS: I’m talking about fork in context with process, PID, exec, etc;

On 2012-08-02 12:06, havock07 wrote:
>
> Not sure where exactly to ask this, but, can anyone tell me and lay down
> what a fork is and it’s purpose. I kinda know but not very clearly. This
> seems very very abstract to me, it would be nice if someone gives a nice
> explanation. I kinda guessing this is a really deep topic, maybe dealing
> with as deep as even CPU architecture(?) I’m learning about Linux and
> I’m finding it tough to understand this part.

In the context of opensource development, a fork occurs when a group of developpers from a
project detach from that project, and start a variant project developing a variant of the project.

For example, LibreOffice is a fork from OpenOffice.

> PS: I’m talking about fork in context with process, PID, exec, etc;

Ah! And you say that at the end. >:-)


Cheers / Saludos,

Carlos E. R.
(from 11.4 x86_64 “Celadon” at Telcontar)

Yes, first read until the end, not only of the post, but of the thread :wink:

I could try to type a lot here. but this tells it not to bad IMHO: Fork (operating system) - Wikipedia, the free encyclopedia

And of course read Linux everywhere where it says Unix in the text.

Yeah, this is not the fork I was talking about nevertheless it’s good to know, didn’t know much about this fork, but now that you laid out a easy-to-understand example its all good. :wink:

In that context would you say Waterfox is a fork of Firefox (Mozilla)?

Thanks for your respose.

Hey so, let me show you what I read the book I’m reading (I’m actually a college student, this is like the text book I use for class, fork isn’t like told in depth in my course, but I would like to know about this.)

And I quote…

“Like the file structure, the process structure is hierarchical, with parents, children,
and even a root. A parent process forks a child process, which in turn can fork other
processes. (The term fork indicates that, as with a fork in the road, one process
turns into two. Initially the two forks are identical except that one is identified as
the parent and one as the child. You can also use the term spawn; the words are
interchangeable.) The operating system routine, or system call, that creates a new
process is named fork().”

It says “A parent process forks a child process, which in turn can fork other processes.”, what does this mean?

For example:

If I type the command “ls” in the shell. Does this “ls” process fork a child “ls” process that does all the job and sends the results back to the main (parent) “ls” process? And then main (parent) “ls” passes the info to the shell and it wakes up and promts us? Is this what is happening? Would be nice if you explain in terms of the example :slight_smile:

Thanks in advance!

On 08/02/2012 08:46 PM, havock07 wrote:
>
> In that context would you say Waterfox is a fork of Firefox (Mozilla)?

welcome to the forum, but meet Google:

http://lmgtfy.com/?q=Is+Waterfox+a+fork+of+Firefox?


dd

Hi
The command pstree is your friend…


Cheers Malcolm °¿° (Linux Counter #276890)
SUSE Linux Enterprise Desktop 11 (x86_64) Kernel 3.0.34-0.7-default
up 4 days 15:35, 3 users, load average: 0.30, 0.38, 0.40
CPU Intel i5 CPU M520@2.40GHz | Intel Arrandale GPU

Nope. The shell process (the one you are typing in) forks the ls process. The output of the ls process (it’s standard out) is in this case the terminal and this is ingherited from it’s parent (the shell). That is why you see that output on the terminal. When the ls process is finished, the Kernel signals the parent (the shell) and it shows you the prompt.

It is not a must that the parent process waits until the child proccess is finished (as above). They can both run on independant. You can tell the shell to to this by ending the command with an &, like

henk@boven:~> ls -l &
[1] 13173
henk@boven:~> totaal 64
drwxr-xr-x  2 henk wij 4096 23 jul 18:43 bin
drwxr-xr-x  2 henk wij 4096 28 feb 11:43 cassette
drwx------  2 henk wij 4096 26 jan  2010 Desktop
drwxr-xr-x 32 henk wij 4096  8 jun 21:34 Documents
drwxr-xr-x  2 henk wij 4096  2 aug 15:34 Downloads
drwxr-xr-x  3 henk wij 4096  5 mei 13:49 Fotos
-rw-r--r--  1 henk wij 1540 30 mei  2011 kaddressbook
drwxr-xr-x  2 henk wij 4096  9 jul 14:40 logs
drwx-w---- 11 henk wij 4096  2 aug 20:41 Mail
drwxr-xr-x 10 henk wij 4096 31 jan  2012 milweb
drwx------  6 henk wij 4096 26 jan  2010 Muziek
drwx------  6 henk wij 4096 22 jun  2010 ripper
drwxr-xr-x  3 henk wij 4096 24 jul 19:48 Rum
drwxr-xr-x  4 henk wij 4096  9 jul  2010 sss
drwxr-xr-x  2 henk wij 4096 31 jul 11:25 ttt
drwxr-xr-x  2 henk wij 4096 13 jun  2010 uitzending

[1]+  Klaar                   ls $LS_OPTIONS -l
henk@boven:~> 

It is still possible for the child to signal the parent and vv. (through the kernel), but that depends on what was asked from the kernel at the fork call.
In the example above the ls is so quick that I can not do very much before it is finished. But when you have a lengthy process there, you can get a shell propmt by hitting return (I did this after ls was allready finished) and you can then carry on using the shell. If the child finishes, the shell will report that with the Klaar line you see (Ready in an English version). But in tthe meantime you can use the shell buildin fg command to call the child to the foregrounf again (e.g. to kill it with Ctrl-C as normal). See the paragraph JOB CONTROL in the bash man page.

And yes, all the processes in your system form a tree. The man page of ps has two examples to display this:

ps -axfj

and

ps -ejH

HTH

EDIT: yes, pstree is even more beautifull.

On Thu, 02 Aug 2012 18:56:02 +0000, havock07 wrote:

> It says “A parent process forks a child process, which in turn can fork
> other processes.”, what does this mean?

I this context, you can read it as “A parent process creates a child
process”. fork() is the system call that’s used to do that.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Thanks, yes I did look into this command, I feel enlightened :slight_smile:

Thanks, with the ampersand, I was better able to grasp what is happening, and how all processes are getting forked except the builtins. Also pstree helped much.