What hanppened when I send a SIGCONT signal to a background job?

1.3k views Asked by At

I'm writing a simple Linux shell and I'm wondering if I can put a background job to the foreground by sending it a SIGCONT signal.

2

There are 2 answers

0
ua2b On

You can freeze a process by sending it a SIGSTOP and let it continue to run by sending SIGCONT.

If you are writing your own shell, sending the process to the foreground or in the background is up to you.

In bash sending SIGCONT lets the process continue in the background and you need to issue fg to put to the foreground.

0
Anand7253 On

Quoting the GLibC manual https://www.gnu.org/software/libc/manual/html_node/Continuing-Stopped-Jobs.html

The shell can continue a stopped job by sending a SIGCONT signal to its process group. If the job is being continued in the foreground, the shell should first invoke tcsetpgrp to give the job access to the terminal, and restore the saved terminal settings.

And the tcsetpgrp(3) manual:

#include <unistd.h>

pid_t tcgetpgrp(int fd);
int tcsetpgrp(int fd, pid_t pgrp);

The function tcsetpgrp() makes the process group with process group ID pgrp the foreground process group on the terminal associated to fd, which must be the controlling terminal of the calling process, and still be associated with its session. Moreover, pgrp must be a (nonempty) process group belonging to the same session as the calling process.

If tcsetpgrp() is called by a member of a background process group in its session, and the calling process is not blocking or ignoring SIGTTOU, a SIGTTOU signal is sent to all members of this background process group.

So, SIGCONT itself won't do the trick, preceed it by tcsetpgrp

tcsetpgrp(fd, pid or pgrpid);
kill(pid or -pgrpid, SIGCONT);