Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda