3

Whenever we open a terminal, kernel creates a process(bash-terminal using fork+exec) and it's parent process is "init". But this "mate-terminal" becomes parent and creates another process "terminal" which is visualised by user.

Just wanted to know what sort of file descriptors,memory etc are shared between the "init"(pid=1,ppid=0), "mate-terminal"(pid=200 and ppid=1) and "terminal"(pid=201 and ppid=200).

I know about the process stack and which segments are shared but unable to clearly visualise in this practical scenario.

Please help me in understanding the memory sematics of the process..

Any help is welcome..

Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51

1 Answers1

2

Nothing is shared between init and its terminal children, nor between the first terminal and its terminal child.

This is because, although fork() will make both processes (father and child) to share some objects, exec() family functions completely replaces the current process image with a new process image.

This means that all references to previous objects, such as shared file descriptors from the father, are forgotten.

user2492779
  • 101
  • 2
  • 1
    what about the environment variables created for the first terminal process. Won't these be shared between the first terminal process and its child? – Santosh Sahu Sep 08 '13 at 15:33
  • waiting for your response – Santosh Sahu Sep 09 '13 at 05:36
  • That is a matter of which `exec` function was called: `execvpe` function has a third parameter which is in fact envp array, thus its environment will be created from scratch. Other `exec` functions just leave environment variables as they were. It really depends then on what function init or your shell used. The usual is for environment variables to be preserved, because you need that to allow applications to read user set variables. For example, a program may need environment variable FOO=bar. You export FOOR=bar in bash and call your program: it forks and execs mantaining FOO variable. – user2492779 Sep 09 '13 at 12:37