I try to deal with the processes and signals in Unix and the following problem arose.
main.c
int main(int argc, char** argv) { pid_t fork_pid = fork(); if (fork_pid < 0) { exit(EXIT_FAILURE); } if (fork_pid == 0) { printf("Child will wait\n"); sigset_t set; int sig; sigemptyset(&set); sigaddset(&set, SIGUSR1); sigwait(&set, &sig); printf("Child recive signal\n"); exit(EXIT_SUCCESS); } if (fork_pid > 0) { // Do something before child kill(fork_pid,SIGUSR1); int res; wait(&res); printf("Child exit code: %d\n", res); // Do something after child } } When you first started everything worked well, but then everything broke.
The conclusion was
Child exit code: 30 It is remarkable that the constant SIGUSR1 on my machine is 30! I am more than sure that it is connected, but there is clearly not enough knowledge to figure out how to fix it.
Thank you all in advance for the answer!
UPDATE:
It became clear that the child process may not be ready to receive a signal during the kill call. But how to fix it is still not clear.
средства синхронизации процессов: mutexes, semaphores, conditional variables ... that is all. - αλεχολυτ