Why doesn't (ps -f) create a subshell, but a sepaete process?

I need help because I am not getting anything. From what I read from the internet, a subshell is created when the shell script is executed, or if we run the command in parentheses:( )

I tried to test this with a script that only contains the following command:

ps -f

      

When I run it, I see the following output:

ID      PID  PPID  C STIME TTY          TIME CMD 
me     2213  2160  0 08:53 pts/14   00:00:00 bash 
me     3832  2213  0 18:41 pts/14   00:00:00 bash 
me     3833  3832  0 18:41 pts/14   00:00:00 ps -f

      

This is good because I can see that my bash process spawned another bash process for my script.

But when I do this:

( ps -f )

      

it produces:

UID     PID  PPID  C STIME TTY          TIME CMD 
me     2213  2160  0 08:53 pts/14   00:00:00 bash 
me     3840  2213  0 18:46 pts/14   00:00:00 ps -f

      

So, if the parentheses spawn a subshell, why isn't it shown in processes? And why ps -f

is it considered a different process? Does each command run as a separate process?

+3


source to share


2 answers


It looks like you got caught bash

in a bit of optimization. if a subshell only contains one command, why really make it a subshell?

$ ( ps -f )
UID        PID  PPID  C STIME TTY          TIME CMD
jovalko  29393 24133  0 12:05 pts/10   00:00:00 bash
jovalko  29555 29393  0 12:07 pts/10   00:00:00 ps -f

      

However add a second command, say :

(bash null command which does nothing) and this is the output:

$ ( ps -f ; : )
UID        PID  PPID  C STIME TTY          TIME CMD
jovalko  29393 24133  0 12:05 pts/10   00:00:00 bash
jovalko  29565 29393  0 12:08 pts/10   00:00:00 bash
jovalko  29566 29565  0 12:08 pts/10   00:00:00 ps -f

      



One of the main reasons for using a subshell is that you can perform operations such as I / O redirection in a group of commands instead of a single command, but if your subshell contains only one command, there is no reason to overclock a new bash process.

When it comes to ps

counting as a process, it changes. Many of the commands that you use, such as ls

, grep

, awk

, are external programs. But there are built-in functions, such as cd

, kill

.

You can determine which command is in bash

using the command type

:

$ type ps
ps is hashed (/bin/ps)
$ type cd
cd is a shell builtin

      

+4


source


The main part of the question:

Does each command run as a separate process?

YES!. Each command not built into bash (eg, declare

etc.) runs as a separate process. How it works?

When you type ps

and hit enter, bash parses the input you entered, doing the usual things like globes, extension variables, etc. and finally when it is an external command



  • bash forks

    .

With forking markup than just after the fork, you will have two identical bash

processes (each with a different process ID (PID)) called "parent" and "child", and the only difference between the two running programs bash

is than the "parent" gets ( return value from fork

) PID of the child, but the child does not know the PID of the parent. (fork for child returns 0).

  • after fork, (bash is written this way) - the child replaces itself with a new program image (for example:) ps

    - with a call exec

    .
  • after that, the child bash of course no longer exists and the command just executed is executed - eg. ps

    ...

Of course, when bash will fork itself, do a lot of other things, like redirecting I / O, opening and closing file descriptors, changing signal handling for the child, and much, much more.

+1


source







All Articles