Monday, December 12, 2011

Stopped Process

Terminal defined a SUSP character (Ctl-Z) to stop a process from running in the foreground. Pressing the keys resulted in Linux sending a SIGTSTP signal to the process. The process leaves the stop state when it receives a SIGCONT sigbnal. Otherwise, the only other way to get out of the stop state is to terminate.

Foreground process is the only process that receives terminal input. All other processes are considered to be background process. When background process tries to read from terminal, the terminal stops the process by sending it a SIGTTIN signal. The process stops until it is brought to foreground via the fg command. If the process receives a SIGNCONT, it resumes but will be blocked again when trying to read from terminal.

To stop background process writing to console and thus clobbering the display, use the tostop flag of the terminal. While this setting is disabled (default), background processes can write to terminal. The setting is enabled using this command

stty tostop

When a backgroud process tries to write to terminal, the terminal sends a SIGTTOU signal to the process and puts it to sleep. Sending SIGCONT to the stopped process will wake it up but it will be blocked again as soon as it tries the write. Again, the process will allow to write when it is brought to foreground using fg. To disable the setting, use this command

stty -tostop