You have quite a variety of problems. Ignoring the issue that you should not use printf()
in a signal handler, it turns out that the use of SIGUSR1 and SIGUSR2 is unnecessary and confusing, not least because the wrong processes are signalled. With the signal handling shown, you need to make the pid
variable global, and send signals to that, not getpid()
or getppid()
(especially as getppid()
would return the PID of the process that launches the program).
In fact, though, SIGUSR1 and SIGUSR2 are irrelevant. Here is some working code. It uses some of my code which is available in my SOQ (Stack Overflow Questions) repository on GitHub as files stderr.c
and stderr.h
in the src/libsoq sub-directory. I've used err_setlogopts()
so that the processes writing messages via err_remark()
and err_sysrem()
identify the PID and also the time to milliseconds when invoked. This is tremendously helpful. One thing it showed me was that the signal handlers were never invoked.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include "stderr.h"
enum {CHILD = 0, PARENT = 1};
static pid_t pid;
int main(void)
{
err_setarg0("sigcont43");
err_setlogopts(ERR_PID|ERR_MILLI);
pid = fork();
for (int i = 0; i < 3; i++)
{
if (CHILD == pid)
{
err_remark("Child stops to return the control to the parent\n");
err_remark("Child about to invoke raise(SIGSTOP)\n");
raise(SIGSTOP);
err_remark("Child back from raise(SIGSTOP)\n");
}
else
{
err_remark("Parent process started running\n");
err_remark("Parent stops to let child to register signal\n");
sleep(1);
err_remark("Parent returns from sleep(1)\n");
err_remark("Parent waits child to be stopped\n");
int status = 0;
int corpse = waitpid(pid, &status, WUNTRACED);
err_remark("Parent collects stopped child: corpse = %d, status = 0x%.4X\n", corpse, status);
err_remark("Parent starts child process\n");
err_remark("Parent sends SIGCONT to PID %d\n", pid);
errno = 0;
int rc = kill(pid, SIGCONT);
err_sysrem("Parent sent SIGCONT to PID %d (rc = %d): ", pid, rc);
}
}
return 0;
}
The code has kept the original messages not related to the signal handlers (as there no longer are any signal handlers), but has added more informative messages. It also traps and reports the return values from many system functions. This helped diagnose some of the problems. I also changed the infinite while
loop into a 3-iteration for
loop.
Sample output (test program name sigcont43
):
$ sigcont43
sigcont43: 2021-06-28 11:40:03.305 - pid=83078: Parent process started running
sigcont43: 2021-06-28 11:40:03.306 - pid=83078: Parent stops to let child to register signal
sigcont43: 2021-06-28 11:40:03.305 - pid=83079: Child stops to return the control to the parent
sigcont43: 2021-06-28 11:40:03.306 - pid=83079: Child about to invoke raise(SIGSTOP)
sigcont43: 2021-06-28 11:40:04.306 - pid=83078: Parent returns from sleep(1)
sigcont43: 2021-06-28 11:40:04.306 - pid=83078: Parent waits child to be stopped
sigcont43: 2021-06-28 11:40:04.306 - pid=83078: Parent collects stopped child: corpse = 83079, status = 0x117F
sigcont43: 2021-06-28 11:40:04.306 - pid=83078: Parent starts child process
sigcont43: 2021-06-28 11:40:04.306 - pid=83078: Parent sends SIGCONT to PID 83079
sigcont43: 2021-06-28 11:40:04.307 - pid=83079: Child back from raise(SIGSTOP)
sigcont43: 2021-06-28 11:40:04.307 - pid=83079: Child stops to return the control to the parent
sigcont43: 2021-06-28 11:40:04.307 - pid=83078: Parent sent SIGCONT to PID 83079 (rc = 0): error (0) Undefined error: 0
sigcont43: 2021-06-28 11:40:04.307 - pid=83078: Parent process started running
sigcont43: 2021-06-28 11:40:04.307 - pid=83078: Parent stops to let child to register signal
sigcont43: 2021-06-28 11:40:04.307 - pid=83079: Child about to invoke raise(SIGSTOP)
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent returns from sleep(1)
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent waits child to be stopped
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent collects stopped child: corpse = 83079, status = 0x117F
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent starts child process
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent sends SIGCONT to PID 83079
sigcont43: 2021-06-28 11:40:05.309 - pid=83079: Child back from raise(SIGSTOP)
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent sent SIGCONT to PID 83079 (rc = 0): error (0) Undefined error: 0
sigcont43: 2021-06-28 11:40:05.309 - pid=83079: Child stops to return the control to the parent
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent process started running
sigcont43: 2021-06-28 11:40:05.309 - pid=83079: Child about to invoke raise(SIGSTOP)
sigcont43: 2021-06-28 11:40:05.309 - pid=83078: Parent stops to let child to register signal
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent returns from sleep(1)
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent waits child to be stopped
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent collects stopped child: corpse = 83079, status = 0x117F
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent starts child process
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent sends SIGCONT to PID 83079
sigcont43: 2021-06-28 11:40:06.309 - pid=83078: Parent sent SIGCONT to PID 83079 (rc = 0): error (0) Undefined error: 0
sigcont43: 2021-06-28 11:40:06.309 - pid=83079: Child back from raise(SIGSTOP)
$