What is effect of CTRL + Z on a unix\Linux application

88

29

I was curious and confused that what exactly is the behaviour of CTRl+Z.

I know, If a process in running in foreground, and we press ctrl+z, it goes to background.

But what exactly happens.

Does it keep doing it's job, or does it get suspended, and stopped at the point where it was.

And if it gets stopped at that point, and what is the meaning of background job.

Kumar Alok

Posted 2012-09-13T12:51:29.600

Reputation: 1 153

7Ctrl-Z doesn't send a process to the background, it suspends it. – Wooble – 2012-09-13T12:53:40.983

Answers

74

A "background job" is just one that is not interacting with the user -- it doesn't control the tty and it just does its thing (generally silently). A foreground job is the reverse, it holds control of the tty to interact with the user.

Control-Z suspends the most recent foreground process (the last process to interact with the tty) (unless that process takes steps to ignore suspension, like shells normally do). This will generally bring you back to your shell, from which you can generally enter the command bg to move the just-suspended process to the background (letting it continue to run) or fg to bring it back to the foreground.

mah

Posted 2012-09-13T12:51:29.600

Reputation: 864

1to undo a backgrounded process with fg first identify which job by issuing jobs then say if its %1 then issue fb %1 and process will get resumed – Scott Stensland – 2017-03-31T23:25:51.887

fg %1 not fb %1 – ahonnecke – 2019-06-20T19:09:31.413

58

Pressing Ctrl+Z sends the TSTP signal to your process. This halts execution (the kernel won't schedule any more CPU time to the process) and the process is awaiting a CONT to continue processing.

You can emulate/replicate this via kill -TSTP and kill -CONT (since kill will send a nominated signal to your process, despite the name!)

The shell has the functionality to 'background' the process, but this is a relationship between the shell and the process. The process itself doesn't really have the concept of 'background' or 'foreground'.

See here for more info.

Brian Agnew

Posted 2012-09-13T12:51:29.600

Reputation: 795

3^Z sends SIGTSTP, not SIGSTOP – William Pursell – 2012-09-13T17:50:05.123

@WilliamPursell - ah! Noted. Now changed. Thx – Brian Agnew – 2012-09-19T08:19:31.450

15

Consider this command, which takes approx. 4.5 seconds on my laptop:

echo 2^10000000 | bc -lq | wc -c

When you press Ctrl+Z, the calculation will be suspended. You have an option to resume calculation in foreground using fg, or resume it in background using bg. The latter is more or less equivalent to executing:

echo 2^10000000 | bc -lq | wc -c &

Bolo

Posted 2012-09-13T12:51:29.600

Reputation: 280

9

A process is suspended and stopped when pressing ctrl+z. With fg you can move the suspended job to the foreground, with bg you can run it in the background.

See http://linuxreviews.org/beginner/jobs/ for more information.

alexclooze

Posted 2012-09-13T12:51:29.600

Reputation: 191