LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2009, 02:28 AM   #1
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
How does '&' make a process run in background ?


Hi,

This is no homework question ! It is just out of curiosity !

emacs abc.c &

Executing the above code runs abc.c in background. I was wondering is & a command/process or what ?

Is & internally related to bg command !

I have read many tutorials regarding pipes '|' and redirection operators '>' but none regarding '&'

Kindly explain !

Last edited by Aquarius_Girl; 12-16-2009 at 02:31 AM.
 
Old 12-16-2009, 02:44 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Maybe it uses a fork() / exec() (Ugh!) without waiting for the child (waitpid()?).
 
Old 12-16-2009, 02:55 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by smeezekitty
Maybe it uses a fork() / exec() (Ugh!) without waiting for the child (waitpid()?).
Thanks for bothering !

What made u think that it doesn't wait for the child process ?
 
Old 12-16-2009, 04:53 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by anishakaul View Post
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.
 
1 members found this post helpful.
Old 12-16-2009, 08:05 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by anishakaul View Post
I was wondering is & a command/process or what ?
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
 
1 members found this post helpful.
Old 12-16-2009, 08:19 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
This thread is a spin off from
http://www.linuxquestions.org/questi...82#post3793682
So if you want to know the context of the question.
 
Old 12-16-2009, 08:21 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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."

Also see the nohup command.
 
1 members found this post helpful.
Old 12-16-2009, 10:32 AM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by anishakaul View Post
Thanks for bothering !

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).
 
1 members found this post helpful.
Old 12-19-2009, 02:39 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by sundialsvcs
Also see the nohup command.
Thanks for pointing out the nohup command. I unaware of its existence.

Quote:
Originally Posted by catkin
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
Thanks for the info regarding the operators. I didnt read much details yet, but definitely I will !

Thanks to all others for making the explanation of shell behavior more clear !!
 
Old 12-19-2009, 02:44 AM   #10
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Meanwhile I was wondering is there a command to know the system calls which these commands call internally ??

e.g. printf calls read()
e.g. cd calls chdir()

I tried strace nohup ls & to know the system call nohup must be calling, but couldn't figure out much !

thanks !

Last edited by Aquarius_Girl; 12-19-2009 at 02:48 AM.
 
Old 12-19-2009, 09:07 AM   #11
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
It may be easier to read the source code for the utility than to figure it out from strace...
 
1 members found this post helpful.
Old 12-20-2009, 10:43 PM   #12
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by gnashley
It may be easier to read the source code for the utility than to figure it out from strace...
Thanks for the reply,

But i think there should be some other smarter way to figure that out rather than reading the source code !
 
Old 12-20-2009, 11:32 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by sundialsvcs View Post
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

Last edited by ta0kira; 12-20-2009 at 11:40 PM.
 
Old 12-21-2009, 12:51 AM   #14
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
This is kind of related. I have wondered -is there a way to retrieve the exit status of a process which was backgrounded?
 
Old 12-21-2009, 02:47 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by gnashley View Post
This is kind of related. I have wondered -is there a way to retrieve the exit status of a process which was backgrounded?
Dunno. Maybe jobs -n?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to run a process in Background? anandv_1234 Linux - Newbie 3 02-22-2008 11:47 AM
How can I run updatedb @reboot as a background process? randomsel Slackware 4 02-02-2007 03:36 PM
how to run a process in the X background sh4d0w13 Linux - General 6 09-12-2005 02:54 PM
how to run any binary in background - background process tej Linux - Newbie 1 08-16-2004 12:27 AM
run process in background using system calls indian Programming 2 08-10-2004 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration