If we type a command in a shell it basically creates a child by calling fork and then the child exec's the command we type in, so the parent of it is the shell. If it is a foreground process the parent(in this case shell) has to wait for child to terminate and if we type & it executes in the background. So what exactly makes the parent to wait or not to wait for the child to terminate?

  • 3
    Well, you say in your question that if you run it in the foreground it waits, and if you use & it runs in the background and doesn't wait, so what's the point you're stuck on? Nov 10, 2016 at 17:27
  • That's the difference between a foreground and a background process lol. A handy util is screen. It lets you run a foreground process in the background.
    – trudgemank
    Nov 10, 2016 at 17:37
  • Or to be more specific its a "terminal multiplexer". so you can run a process in terminal and close the window as long as you've screened the process.
    – trudgemank
    Nov 10, 2016 at 17:40
  • @EricRenouf what goes into code to make it wait ? So you mean to say if we say & i.e background process the parent waits for the child to complete right? If yes what should i include in my code to make the parent to wait?
    – kkr
    Nov 10, 2016 at 17:46
  • & is shell syntax for running a process in the background, which means the shell will continue while the other process executes. In the foreground the shell will not do anything more (for our purposes at least) until the other process exits. If you're using the shell, just end a command with & and it will execute in the background, you needn't do anything special. If you're writing c code or something, if you want to block and wait for the child just call wait(3) for the child process Nov 10, 2016 at 17:49

1 Answer 1


As Eric Renouf commented, if you want to wait for a child process in your own code, you should use the wait function (typically implemented as both a system call and a C library function, so you'll find manpages in both section 2 and 3). The linked page has an example implementation.

If you want to do the same thing in shell code, you can use the wait command which waits for a backgrounded job to finish.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .