r/cprogramming • u/Avi250 • 2h ago
Semtimedop, loop with alarm and msgop, signal handler with flag variable - is it good practice or just a bad workaround?
Hello, everyone. This is a school project so I'm not allowed to share my code, but it shouldn't be a big problem since my question is more...general.
In my programs I have several semops and msgops. I had to change my SIGTERM's handler and, with what I've been told (here, too) and I've read, I only put a flag variable in it, which I'll have to check continuously on. I have managed to find a way around if my msgops get interrupted, however, the main problem would have been my processes waiting on a semop or message that would never come (signal comes right before the syscall, it changes the variable and returns, main program continues where it left off).
I figured I'd have the processes enter loops where they wait on the syscall and, if the time allocated runs out, it checks the flag variable. If it's been changed, it proceeds a certain way; otherwise, the loop continues.
It's important to say that SIGTERM will always be sent to all my processes after the right amount of time has passed.
Is this good practice? Or a bad workaround to my problem? In case it is good practice: the simulation can last for about 8 minutes, while the processes usually work with nanoseconds (sometimes seconds). Is it ok to have the processes check for said variable every 2-5 seconds? The processes will be taking note of the waiting time. My main goals here are to avoid active waiting and increase concurrency between processes.
Thank you in advance!
Edit: I'm sorry, I should have specified we're only allowed to use SystemV IPC