in reply to Process Management

Do:

man 2 fork; man 2 execve; man 2 waitpid

and read a bit.

"A child is the exact copy of the parent, until it begins to develop own ideas."

So, if a progam is to execute another program, it does a fork system call. The clone then execs the program to be run.

Get a terminal and type in man perlsyn. Then get another terminal and type in

ps fu | less
and scroll about, look for perlsyn. You'll see something like
shmem 28521 0.0 ... 0:00 xterm shmem 28523 0.0 ... 0:00 \_ bash shmem 28726 0.0 ... 0:00 \_ man perlrun shmem 28752 0.0 ... 0:00 \_ sh -c (cd /usr/local/share +/man ... shmem 28753 0.0 ... 0:00 \_ sh -c (cd /usr/local/s +hare/... shmem 28757 0.0 ... 0:00 \_ less

Here you have a process chain, which has been setup as follows:

  1. xterm forked and its clone exec'ed bash
  2. bash forked and its clone exec'ed man. The bash is just sitting around in the waitpid(2) system call waiting for man to finish.
  3. man forked and its clone exec'ed a shell, which itself
  4. forked and exec'ed a subshell, which
  5. forked and exec'ed less, which is the pager I use to read manual pages with.

In the above table, for each process, the number after the user name is the process ID (or PID). bash is child of xterm, and is parent of man. A child can get at its parent PID via the getppid(2) system call; the result of a fork is the child PID in the parent, and 0 in the child. By those numbers they know of each other and can start killing each other (or process manage themselves, if they're behaved).

Perl's system is essentially a fork/exec and the parent hanging around in waitpid(2).

If a program (grandfather) does a fork, and that clone (clone_1) clones itself again into clone_2, and clone_1 exits, you have a double fork, and the last clone is detached from the grandfather and adopted by init(8). See man 8 init.

Read those manual pages and those provided under 'SEE ALSO'. Recurse.

You won't understand anything at all at first, but after you have finished the recursion, things will begin to fall into place... ;-)

Replies are listed 'Best First'.
Re^2: Process Management
by matze77 (Friar) on Jan 19, 2009 at 10:21 UTC

    Nice done. Good Explanation. pstree could also help visualize ...

    Thanks MH