Reputation: 673
http://codepad.org/rHIKj7Cd (not the whole code)
What I'm trying to accomplish, is the parent to write something in the shared memory, then the child to react accordingly, and write something back, every five seconds. I thought about using SIGUSR1-2, and maybe switching on signum in the handler, but I don't know how to code that in, because setitimer throws SIGALRM. I obviously don't want to fork in the handler, cause i only need one child and one parent, so how do i define the different behaviour? I hope my goals are clear:
Every 5 seconds:
I also know signal() is not adviced to use, this is not the point.
Upvotes: 0
Views: 1153
Reputation: 409166
The timer signal handler should do as little as possible, possible only a "post" operation on a semaphore the parent process waits for. The parent process then does it work, and in turn uses "post" on a semaphore the child waits on. The child does its work and signals back to the parent via another "post" and then goes back to waiting for the semaphore, and the parent can do something with the result from the child. Lastly the parent process goes back to wait for the semaphore from the timer signal handler.
Of course, the signaling between the processes, and from the timer signal handler to the parent process, doesn't actually have to be semaphores. There are other ways to communicate and sending "signals" between processes in a POSIX system, including reading/writing from/to pipes, setting special bits or bytes in shared memory, or message queues.
Upvotes: 1