5

flock() is generally async-signal-safe because it is a system call. Its semantics make it hard to implement it differently. It is not in the POSIX's list of async-signal-safe functions because it is not in POSIX at all.

Is it possible to use flock() in the sigaction handler without problems?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    The Mac OS X documentation for `flock()` doesn't give any direct help. There's no reason to suppose `flock()` messes with user-space structures, so there's every reason to think it will be async-signal-safe, but that's not conclusive. The `fcntl()` function, which is the POSIX locking system call, is async-signal-safe, which supports the view that `flock()` can be implemented so it is async-signal-safe, but that's still not proof. Maybe you should upgrade to use `fcntl()` instead, and then you'll know you're safe. – Jonathan Leffler Jun 07 '13 at 08:29
  • @JonathanLeffler Cna I use `flock()` to lock and `fcntl()` to unlock? – MOHAMED Jun 07 '13 at 08:33
  • Please see my addtional answer to your initial question here: http://stackoverflow.com/a/16979601/694576 – alk Jun 07 '13 at 08:43
  • No: you can't use `fcntl()` to unlock what you locked with `flock()`. – Jonathan Leffler Jun 07 '13 at 11:54
  • Finally I get an answer in this topic: http://stackoverflow.com/questions/16988256/how-to-lock-and-unlock-pid-file-with-fcntl – MOHAMED Jun 07 '13 at 17:19

1 Answers1

0

According to @alk answer in the following topic:

We can develop our property flock() function (its name could be async_flock()). we copy in this new function the content of the origin lockf() code and then we make the following changes in order to get an async-signal-safe function:

  • replace __fcntl with fcntl: necessary to make the code compile
  • replace __set_errno(<errno-define>) with errno = <errno-define>: necessary to make the code compile

  • replace the call to memset() with appropriate assigments struct fcntl = {0}: necessary to have it become async-signal-save.

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268