views:

395

answers:

2

Is an IPC mechanism using shared memory and semaphores for synchronization simplex like pipes or duplex like message queues?

A: 

A semaphore works like this... proc a: "Is the resource available?" semaphore = -2 Yes. semaphore++ proc b: "Is the resource..." semaphore = -1 Yes. semaphore++ proc c: "is the resource..." semaphore = 0 No. (nothing happens)

at this point, proc c can queue (depending on your api, this might be a busy loop or it might be a callback, or you might simply spawn a waiting thread & write your own callback)

proc a: "im done" semaphore--;

proc c will notice that semaphore is available, through something probably similar to what I mentioned previously.

the reason I wrote all that out is so I could say, its both. Its like a message queue in that you can have threads waiting on a resource (shared memory controlled by a semaphore) that trigger some action, even an actual system message, when they get the resource. Or you could just busy-wait until it's done, and that would be like piping.

ryansstack
A: 

If my understanding of your question is correct, it is duplex.

With shared memory, both processes could communicate both ways, not just with one as the reader and one as the writer. Pipes only allow either reading or writing, but you can overcome this by using two pipes (although message queues are a better option).

Zifre