c - Synchronize two processes using two different states -
i trying work out way synchronize 2 processes share data.
basically have 2 processes linked using shared memory. need process set data in shared memory area, process b read data , act on it.
the sequence of events looking have is:
- b blocks waiting data available signal
- a writes data
- a signals data available
- b reads data
- b blocks waiting data not available signal
- a signals data not available
- all goes beginning.
in other terms, b block until got "1" signal, data, block again until signal went "0".
i have managed emulate ok using purely shared memory, either block using while loop consumes 100% of cpu time, or use while loop nanosleep in misses of signals.
i have tried using semaphores, can find way wait zero, not one, , trying use 2 semaphores didn't work. don't think semaphores way go.
there numerous processes accessing same shared memory area, , processes need notified when shared memory has been modified.
it's trying emulate hardware data , control bus, events edge rather level triggered. it's transitions between states interested in, rather states themselves.
so, ideas or thoughts?
linux has own eventfd(2) facility can incorporate normal poll/select loop. can pass eventfd file descriptor process process through unix socket usual way, or inherit fork(2).
edit 0:
after re-reading question think 1 of options signals , process groups: start "listening" processes under same process group (setpgid(2)), signal them negative pid argument kill(2) or sigqueue(2). again, linux provides signalfd(2) polling , avoiding slow signal trampolines.
Comments
Post a Comment