LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 12-16-2009, 02:28 AM   #1
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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 TheIndependentAquarius; 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,208

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

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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: Debian lenny, Slackware 12
Posts: 806

Rep: Reputation: 161Reputation: 161
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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
Senior Member
 
Registered: Dec 2007
Distribution: Centos
Posts: 4,968

Rep: Reputation: 1073Reputation: 1073Reputation: 1073Reputation: 1073Reputation: 1073Reputation: 1073Reputation: 1073Reputation: 1073
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
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,051

Rep: Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953Reputation: 953
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: 58
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
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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 TheIndependentAquarius; 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,725

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,598
Blog Entries: 29

Original Poster
Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
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,725

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
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: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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


All times are GMT -5. The time now is 02:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration