0

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:

  • SIGALRM is thrown by the timer
  • Parent writes its calculations into shared memory (semaphores are being used)
  • Child reads from shared memory, writes back it's calculations

I also know signal() is not adviced to use, this is not the point.

Innkeeper
  • 663
  • 1
  • 10
  • 22
  • You could use something like [semaphores](http://linux.die.net/man/7/sem_overview) to synchronize the two processes. – Some programmer dude Dec 14 '12 at 11:28
  • I'm doing exactly that, what I'm confused about is where to put the parent's and the child's work. Cause when I put it where it is now, it just runs one time, if I put it in the handler function, there will be a lot more unnecessary processes. – Innkeeper Dec 14 '12 at 11:33

1 Answers1

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thank You, I'm thinking this through, although because I'm fairly new to working with processes, semaphores, shared memory, I don't think I can fully interpret Your answer. – Innkeeper Dec 14 '12 at 11:46