Search code examples
clinuxsignalstty

how does a process know when it's been backgrounded?


When I scp a file, I can stop it with ^Z and put it in the background. When it's in the background it stops printing its progress but the copying continues. If I foreground it again, it resumes printing. How does it know? SIGTTOU? does that happen on a standard ptty?


Solution

  • A coworker of mine and I actually looked through the source and found the answer.

    Whenever scp is about to print output it runs tcgetpgrp on stdout. This will return the controlling process group of the terminal (assuming it is a terminal). It will only print out if process group controlling the terminal is the same as the process group of scp. Turns out no signalling required! (Though it does handle SIGWINCH to calculate the size of the progress line).