My Command Interpreter is bash shell.After opening terminal on my OS(cent OS), I have executed following commands:
scenario 1
$sh -c "ps -l"
UID PID PPID TIME CMD
3038 2577 2504 00:00:00 bash
3038 2992 2577 00:00:00 ps
scenario 2
$sh
$ps -l
UID PID PPID TIME CMD
3038 2577 2504 00:00:00 bash
3038 3005 2577 00:00:00 sh
3038 3006 3005 00:00:00 ps
Observe PID and PPID of ps
.
In scenario 1, I am executing ps -l
command on sh
shell. So it's parent should be sh
ie, it's PPID should be PID of sh
. But ps -l
command listing that it's parent is bash
. I am not understanding what is happening exactly. I am understanding the difference between scenario 1 and scenario 2 . But when I am executing the same commands on another OS(ubuntu), I am getting same listing under ps -l
in scenario 3 and scenario 4 , as below:
scenario 3
$sh
$ps -l
UID PID PPID TIME CMD
3038 2577 2504 00:00:00 bash
3038 2991 2577 00:00:00 sh
3038 2992 2991 00:00:00 ps
scenario 4
$sh
$ps -l
UID PID PPID TIME CMD
3038 2577 2504 00:00:00 bash
3038 3005 2577 00:00:00 sh
3038 3006 3005 00:00:00 ps
Here in both scenarios, I am getting PPID of ps
CMD as PID of sh
. What is happening exactly. Is my interpretation wrong?
When you type a command, bash just fork
then execve
the command. scenario 3 and scenario 4 are the case.
While sh -c 'ps -l'
depends on shells.
On my linux distribution, the result of ls -l `which sh`
is lrwxrwxrwx 1 root root 4 Oct 6 14:06 /usr/bin/sh -> bash
.
When -c
is present, bash execve
the following command directly. While another shell like fish
does not.
fish -c 'ps -l'
FS UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 0 S 1000 711 710 0 80 0 - 5707 wait pts/5 00:00:00 bash 0 S 1000 1519 711 0 80 0 - 7054 wait pts/5 00:00:00 fish 0 R 1000 1526 1519 0 80 0 - 9266 - pts/5 00:00:00 ps
What execve
does is replacing the current process image with a new process image. fork
creates a new process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.