• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

307
Vistas
Can Linux flock(fd, LOCK_EX|LOCK_NB) fail spuriously?

Consider a situation where two processes make concurrent attempts at placing an exclusive lock on some file using flock(fd, LOCK_EX|LOCK_NB).

As indicated, the attempts are non-blocking, so one of the two processes is supposed to fail with EWOULDBLOCK.

Here is my question: Does the (Linux) implementation of flock() guarantee that exactly one of the two processes will succeed in every such case? Or, is it possible that both end up failing with EWOULDBLOCK even when nobody else are interfering?

In short, can flock(fd, LOCK_EX|LOCK_NB) ever fail spuriously with EWOULDBLOCK?

I am mainly interested in the version of flock() offered by Linux, but information about flock() on other systems, such as OS X, is most welcome.

Also, I assume that the answer is the same regardless of whether the locks are exclusive (LOCK_EX) or shared (LOCK_SH). If not, let me know.

about 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Reading man flock(2):

EWOULDBLOCK The file is locked and the LOCK_NB flag was selected.

So getting EWOULDBLOCK means the file is already locked. If it is guaranteed that your two processes are the only ones involved, they will never get EWOULDBLOCK on the same file at the same time.

Please note that threads is a different story. Threads normally share file descriptors, so several threads within the same process can call flock() successfully on the same file.

about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda