在sigaction处理程序中使用flock() [英] Use flock() in the sigaction handler

查看:60
本文介绍了在sigaction处理程序中使用flock()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

flock()通常是异步信号安全的,因为它是系统调用.它的语义使得很难以不同的方式来实现它.它不在POSIX的异步信号安全功能的列表中,因为它根本不在POSIX中.

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.

是否可以在sigaction处理程序中使用flock()而不出现问题?

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

推荐答案

根据以下 @alk 答案. com/questions/16988256/how-to-lock-and-unlock-pid-file-with-fcntl>主题:

我们可以开发属性flock()函数(其名称可能为async_flock()).我们在此新函数中复制原始lockf()代码的内容,然后进行以下更改以获取异步信号安全函数:

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:

  • __fcntl替换为fcntl:代码编译所必需的
  • __set_errno(<errno-define>)替换为errno = <errno-define>:代码编译所必需的

  • 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

用适当的配置struct fcntl = {0}替换对memset()的调用:必须使其成为异步信号保存.

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

这篇关于在sigaction处理程序中使用flock()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆