Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Share futex between unrelated processes

How can unrelated processes cooperate using a futex?

Let's say I have unrelated processes, one being, say, an apache subprocess with my module, another being e.g. a background script.

I'd like to establish a condition variable with a mutex between the two using a futex, to benefit for user-space fast code path.

It seems to me that memory at which mutex is stored could be in a mmap'd file, if that memory is mapped, e.g. mlock'd the two processes could theoretically issue futex calls against same address.

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

Code submissions low-, high-level and dynamic languages are accepted (C, C++, Python, etc.). "robust futex" API must be supported too.

References:

  • https://www.kernel.org/doc/Documentation/robust-futexes.txt
  • http://locklessinc.com/articles/mutex_cv_futex/
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

mutex are defined to lock access to shared resources. This does not seem what you want to do .

if you want both process to cooperate, you may either use shared mmap area, or semaphore

over 4 years ago · Santiago Trujillo Denunciar

0

Thanks Phillip and Felix M. for pointers.

Python user code (file with data structures already exists, initialised 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 get awoken:

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda