如何使一个函数异步信号安全的吗? [英] How to make a function async-signal-safe?

查看:500
本文介绍了如何使一个函数异步信号安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的的sigaction 处理函数

void signal_term_handler(int sig)
{
    printf("EXIT :TERM signal Received!\n");
    int rc = flock(pid_file, LOCK_UN | LOCK_NB);
    if(rc) {
        char *piderr = "PID file unlock failed!";
        fprintf(stderr, "%s\n", piderr);
        printf(piderr);
    }
    abort();
}

有人告诉我,羊群的printf 不是异步信号安全的。我找不到在本作羊群备用异步信号安全功能的列表

Someone told me that flock and printf aren't async-signal-safe. And I could not find an alternate async-signal-safe function for flockin this list.

和根据上述链接:


  函数调用不安全的功能,这种行为是未定义

when a signal interrupts an unsafe function and the signal-catching function calls an unsafe function, the behavior is undefined

有没有一种方法,使羊群异步信号安全的吗?或者有另一种解决方案来执行羊群当我收到 TERM 信号?

Are there a way to make flock async-signal-safe? Or is there another solution to execute flock when I receive TERM signal?

推荐答案

您可以使用的的fcntl()作为替代涌向()。

You can use fcntl() as an alternative to flock().

这篇关于如何使一个函数异步信号安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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