Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cause a Linux poll event on a shared memory file

Tags:

linux

Two Linux processes open and mmap the same /dev/shm/ shared memory file and use it as common memory. Question: what is the simplest and best way for one process to "wake up" the other process to notify that it should look in the memory? For example, can one process cause a poll() event for the other process's file descriptor? The solution doesn't need to be portable but I would like it to be simple.

like image 671
Luke Gorrie Avatar asked Jul 13 '12 12:07

Luke Gorrie


2 Answers

That's why POSIX has condition variables.

Define a shared POSIX condition variable and its associated mutex in the shared memory region.

Then have one thread wait on the condition variable and the other signal the condition variable event when it wants the other thread to look in the memory.

There's a lot of material on the web on condition variables. Here is one pretty good short one: https://computing.llnl.gov/tutorials/pthreads/#ConditionVariables

like image 87
gby Avatar answered Sep 20 '22 20:09

gby


You may please consider using a semaphore (POSIX named semaphore) also to solve this.

One simple example, using shared Memory (In the example it is in System V, but you can use it with POSIX too) and POSIX semaphore is in the link , How can 2 processes talk to each other without pipe()?

like image 34
Tanmoy Bandyopadhyay Avatar answered Sep 19 '22 20:09

Tanmoy Bandyopadhyay