What exactly does it mean to run a process in the background?
The difference between $program
and $program&
: Is it just that the input file descriptor is not inherited from the shell or is there something else / something more?
What exactly does it mean to run a process in the background?
The difference between $program
and $program&
: Is it just that the input file descriptor is not inherited from the shell or is there something else / something more?
program &
will still have the same stdin
and stdout
as program
, but program &
usually (depending on your terminal settings) won't be able to read from stdin
without getting stopped by the system via the SIGTTIN
signal (see https://en.wikipedia.org/wiki/Job_control_(Unix)#Implementation ).
Background processes won't receive signals from keyboard shortcuts (Ctrl-C, Ctrl-\, Ctrl-Z
) and of course, running processes in the background will let you keep using the shell (e.g., to start additional processes).
It means a number of things.
When you start a process in the foreground, the shell will wait for it to complete (with one of the wait()
family of system calls) before reading the next command. When you start a process in the background, the shell does not wait for it to complete or change state; it immediately reads and executes the next command.
Note that whether or not you start a process in the background, you can move it between background and foreground at will (with ^Z
and bg
, or with fg
). All jobs are equal in that respect. The only restriction is that there can be at most one foreground job at any given time, and backgrounded jobs get stopped if they try to use the shell's tty.
Whenever there is no foreground job, the shell will read its next command (from the script file or from stdin) and execute it.