[SOLVED] How does '&' make a process run in background ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What made u think that it doesn't wait for the child process ?
"It" is, of course, the shell.
The reason we know it doesn't wait for the child process to complete is that if it did wait, you wouldn't get your shell prompt back until the waiting was complete.
The GNU Bash Reference's section on Lists of commands says "the control operator ‘&’" so I guess that makes it a control operator!
Even bash doesn't report that. It reports external commands, built-ins and keywords but not operators
Code:
c:~$ type ls
ls is /usr/bin/ls
c:~$ type [
[ is a shell builtin
c:~$ type then
then is a shell keyword
c:~$ type '>'
bash: type: >: not found
c:~$ type '&'
bash: type: &: not found
The shell works by spawning child processes. Normally, it does so and then waits for the processes to complete.
When you use the "&" operator, the shell launches the child process but does not wait for it to complete. It calls the launched process a "job" and immediately presents you with the command-prompt again.
You can, with the bg and fg commands, switch your attention between any of these processes.
All of these processes are, and remain, "children of the shell."
What made u think that it doesn't wait for the child process ?
Well of course it waits for the process to end in the background. If it didnt wait for it at all then the prompt would be blocked until the child is done, which is the same as just running any command normally (the shell is blocked). If it didnt wait to accept the childs return code (in the background), then the child would be an orphan process (or zombie, I forget the terminology).
The shell works by spawning child processes. Normally, it does so and then waits for the processes to complete.
When you use the "&" operator, the shell launches the child process but does not wait for it to complete. It calls the launched process a "job" and immediately presents you with the command-prompt again.
Partially correct. The shell will still keep track of it unless you disown it; that's how you can put it in the foreground later. The shell is the session leader, which means it controls the terminal and the process groups tied to that terminal. A command line generally constitutes a process group, which has separate processes for each stage of I/O redirection. Normally a new process group is put in the foreground, which means it controls the terminal. When using &, the shell takes terminal control from the process group so that a program blocks (with SIGTTOU) if it requires access to it (other than writing to it like a pipe.) See man tcsetpgrp for more info.
Kevin Barry
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.