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... ;-)


In reply to Re: Process Management by shmem
in thread Process Management by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.