Hello, everyone. I'm supposed to be doing a project for school (hence why I can't share the code), but I'll try to describe my problem to the best of my abilities.
My main parent process forks several children, who will execute 2 other programs, depending on the role assigned to them. Once the parent process receives a SIGALRM, it sends a SIGTERM to all its kids, and they, already having changed their signal handlers for SIGTERM, have to do whatever is specified in their signal handler.
Let's consider child A executes program A, and child B executes program B. These 2 processes have to communicate with each other through a series of msgsnd/msgrcv (we can only use System V IPCs in this project, so the msgops don't restart once interruped by a signal). A single process of type B will communicate with several processes of type A (but not with all at the same time: once its communication is finished with one, it goes on to the other). The last msgop A does, is a msgsnd and B does a msgrcv. I'd like to also add that, between the msgrcv and msgsnd on A's part, A is also supposed to go to sleep. A's almost entire program is in a while (it never terminates unless it receives the signal from its parent), whereas B terminates successfully once it's finished communicating with all the necessary A processes.
Whether B receives the SIGTERM signal or not, it has to modify sth in a shared memory section, but what it changes, depends on whether it has finished communicating with all the processes or not.
Ideally, I would like for B to terminate successfully, even if it receives SIGTERM, after having done all it had to do, and I thought of blocking SIGTERM with sigprocmask. That said, couldn't it still receive the signal between the mast msgrcv operation and sigprocmask? Is there any way whatsoever to make a set of instructions atomic?
I considered only having a sig_atomic_t volatile variable in the signal handler, which would increment if the the sign handler is executed, but then, not only would I have to continuously check in case it changed its value, but (and this could be me being wrong, please correct me if that's the case), B could still receive SIGTERM right after the condition of the variable being 0 is checked, and before the following instruction is executed (like a nanosleep - which I'd like to avoid if SIGTERM has been received - OR before a msgrcv, leaving B to wait for a message that will never come, because A could have received the same signal and will not be sending a message).
I thought of having the signal handler check a variable and act differently based on its value, but, again, maybe B receives the signal right before said variable's value changes. I also considered having an if where the conditions would be the variable==0 && msgrv(...)!=-1, but the variable can change after its value has been verified.
Disclaimer: we haven't studied sig_atomic_t volatile variables, so my knowledge might be lacking, although I did try to read up on them and research them. Also, the parent won't be deleting the IPCs until all kids have terminated.
Am I supposed to just hope for the best here and just proceed with sigprocmask? Is there another way I'm not seeing? Am I misunderstanding something? Any help offered is invaluable, so thank you in advance. I'm happy to answer any clarifying questions if I was unclear at all in the post.