Wednesday, April 2, 2014

exec

With the exec command, you can execute a process without forking a new process.
In the following screenshot a Korn shell (ksh) is started and is being replaced with a
bash shell using the exec command. The pid of the bash shell is the same as the pid
of the Korn shell. Exiting the child bash shell will get me back to the parent bash,
not to the Korn shell (which does not exist anymore).

[root@localhost ~]$ echo $$
4224 # PID of bash
[root@localhost ~]$ ksh
$ echo $$ $PPID
5343 4224 # PID of ksh and bash
$ exec bash
[root@localhost ~]$ echo $$ $PPID
5343 4224 # PID of bash and bash
[root@localhost ~]$ exit
exit
[root@localhost ~]$ echo $$
4224

Terminology

Process
A process is compiled source code that is currently running on the system.

PID
All processes have a process id or PID.

PPID
Every process has a parent process (with a PPID). The child process is often started
by the parent process.

init
The init process always has process ID 1. The init process is started by the kernel
itself so technically it does not have a parent process. init serves as a foster parent
for orphaned processes.

kill
When a process stops running, the process dies, when you want a process to die, you
kill it.

daemon
Processes that start at system startup and keep running forever are called daemon
processes or daemons. These daemons never die.

zombie
When a process is killed, but it still shows up on the system, then the process is
referred to as zombie. You cannot kill zombies, because they are already dead.