Futex sharing between unrelated processes

How can interacting processes interact with futex?

Let's say I have unrelated processes, one of which is, say, an apache subprocess with my module, the other, for example, a background script.

I would like to set a condition variable with mutex in between using futex in order to benefit from fast code for user space code.

It seems to me that the memory in which the mutex is stored may be in the mmap file if this memory is mapped, for example. mlock 'd both processes could theoretically issue futex calls against the same address.

Alternatively, perhaps futex can be transferred from one process to another using FUTEX_FD .

Representations of low-level, high-level and dynamic languages ​​(C, C ++, Python, etc.) are presented. The robust futex API must also be supported.

Literature:

+6
source share
2 answers

Thanks to Phillip and Felix M. for pointers.

Custom Python code (file with data structures already exists, initialized with PTHREAD_PROCESS_SHARED )

 with open("/tmp/semaphore", "rb+") as f: m = mmap.mmap(f.fileno(), 0) # default: all file, share, read-write data = ffi.cast("unsigned long[3]", id(m))[2] # pointer to mapped area, 64-bit CPython lock = ffi.cast("pthread_mutex_t *", data) cond = ffi.cast("pthread_cond_t *", data + 40) @contextlib.contextmanager def locked(alock): assert not C.pthread_mutex_lock(alock) try: yield finally: assert not C.pthread_mutex_unlock(alock) 

Wait and wake up:

 if "wait" in sys.argv: with locked(lock): assert not C.pthread_cond_wait(cond, lock) elif "signal" in sys.argv: with locked(lock): assert not C.pthread_cond_signal(cond) 

Basics of setting PTHREAD_PROCESS_SHARED :

 l = ffi.new("pthread_mutexattr_t *") assert not C.pthread_mutexattr_init(l) assert not C.pthread_mutexattr_setpshared(l, 1) # PTHREAD_PROCESS_SHARED assert not C.pthread_mutex_init(lock, l) # same for condition variable 

Full code for nitpicks :-) https://github.com/dimaqq/pthread_mutexattr_init/blob/master/xsem.py based on http://linux.die.net/man/3/pthread_mutexattr_init

0
source

mutex are defined to block access to shared resources. This is not like what you want to do.

if you want both processes to collaborate, you can either use the common mmap area or the semaphore

-1
source

All Articles