tags:

views:

379

answers:

4

Which signal should I send to a background process to move it foreground? SIGTTIN, SIGTOU or...?

+2  A: 

There is no way (In any OS I know of yet) to use a signal to bring a process to the foreground.

I believe you can only bring a process to the foreground using fg

Foreground is only relevant in the context of a console, and a signal cannot tell the process what console to foreground to...

John Weldon
I need a signal name :)
gurudoglu
there is no signal name :)
John Weldon
there is no spoon :-)
paxdiablo
rofl - that's hilarious
John Weldon
+10  A: 

It's not signals which directly control whether jobs are foreground or background. The jobs are under the control of a shell (usually).

For example, under bash, if you execute:

pax> sleep 3600 &
pax> jobs

you will see output like:

[1]+  Running                 sleep 3600 &

Then, you can bring that job back into the foreground by using:

pax> fg %1
sleep 3600

(and the terminal waits).

Using CTRLZ does send a signal to the process (SIGSTOP) as well as putting it into the background but the only signal that can change that is SIGCONT (to continue):

pax> fg %1
sleep 3600
^Z
[1]+  Stopped                 sleep 3600
pax> jobs
[1]+  Stopped                 sleep 3600
pax> kill -CONT %1
pax> jobs
[1]+  Running                 sleep 3600 &

That will instruct the process to start running again but it doesn't bring it into the foreground. For that, you need the fg command.

It's probably best to think of signals (which affect the process) and foreground/background (which affect the shell that started the process by determining whether it waits for it, amongst other things) separately.

paxdiablo
you can continue a suspended process send a signal in C like this :kill(1453, SIGCONT);I need the name of the signal which can move a background process to foreground like SIGCONT. :)
gurudoglu
There isn't one. Plain and simple. Foreground/background is not an attribute of the process, it's an attribute of the *shell* that started the process. The shell controls that aspect. You can send the SIGCONT to start a process running (if it was stopped with SIGSTOP) but there is no signal that will allow a process to force itself into the foreground because it simply doesn't have that power.
paxdiablo
@gurudoglu - It's like trying to phone up a TV broadcaster and ask them to switch on your TV. There's no number for that. Fortunately...
martin clayton
+1  A: 

Assuming you are on Unix and started the process from a shell you could type the following

  • Stop Process :: ^Z
  • Move process into background :: bg
  • Move back into foreground :: fg
Tom
+4  A: 

gurudoglu. I think your request's response is here :

FTI
thanks FTIK. I found the answer of my question at the first link :D
gurudoglu
good answer - but the question is still fundamentally wrong :)
John Weldon
None of these signals will move a process into the foreground. SIGCONT will cause a process to resume after it has been SIGSTOP'ped. The SIGTT ones are simply delivered to the process when they attempt to read or write while backgrounded. They do *not* foreground the process, in fact their default action is to simply stop the process as if you had sent a SIGSTOP. You still have to either SIGCONT to have them keep running in the background or fg them from the shell to bring them into the foreground.
paxdiablo